cciw-website / cciw / officers / email_utils.py

from django.core.mail import EmailMessage, get_connection
"""
Utilities for sending email with attachments
"""


def send_mail_with_attachments(subject, message, from_email,
                               recipient_list, fail_silently=False,
                               auth_user=None, auth_password=None,
                               attachments=None):
    connection = get_connection(username=auth_user, password=auth_password,
                                fail_silently=fail_silently)
    return EmailMessage(subject=subject, body=message, from_email=from_email,
                        to=recipient_list, connection=connection, attachments=attachments).send()


def formatted_email(user):
    """
    Get the email address plus name of the user, formatted for
    use in sending an email, or 'None' if no email address available
    """
    name = (u"%s %s" % (user.first_name, user.last_name)).strip().replace(u'"', u'')
    email = user.email.strip()
    if len(email) == 0:
        return None
    elif len(name) > 0:
        return u'"%s" <%s>' % (name, email)
    else:
        return email
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.