Ejemplo n.º 1
0
def send_mail(subject, body, from_email, recipient_list, message_id=None):
    msg = SafeMIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_email
    msg['To'] = ', '.join(recipient_list)
    msg['Date'] = formatdate()
    msg['Message-Id'] = '<%s>' % (message_id or create_message_id())

    server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
    try:
        server.sendmail(from_email, recipient_list, msg.as_string())
    finally:
        server.quit()
Ejemplo n.º 2
0
def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, extra_headers=None):
    """
    Given a datatuple of (subject, message, from_email, recipient_list), sends
    each message to each recipient list. Returns the number of e-mails sent.

    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
    If auth_user and auth_password are set, they're used to log in.
    """
    try:
        server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
        if auth_user and auth_password:
            server.login(auth_user, auth_password)
    except:
        if fail_silently:
            return
        raise
    num_sent = 0
    for subject, message, from_email, recipient_list in datatuple:
        if not recipient_list:
            continue
        from_email = from_email or settings.DEFAULT_FROM_EMAIL
        msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET)
        msg['Subject'] = subject
        msg['From'] = from_email
        msg['To'] = ', '.join(recipient_list)
        msg['Date'] = rfc822.formatdate()
	msg['Message-ID'] = '<*****@*****.**>' % \
			(datetime.datetime.now().isoformat('T').replace(':', '.'),
                         os.getpid(), random.randrange(0, sys.maxint))
	if extra_headers:
            for h in extra_headers:
                msg[h] = extra_headers[h]
        try:
            server.sendmail(from_email, recipient_list, msg.as_string())
            num_sent += 1
        except:
            if not fail_silently:
                raise
    try:
        server.quit()
    except:
        if fail_silently:
            return
        raise
    return num_sent
Ejemplo n.º 3
0
def send_mass_mail(datatuple, extra={}, fail_silently=False, auth_user=settings.EMAIL_HOST_USER,
        auth_password=settings.EMAIL_HOST_PASSWORD, tls=getattr(settings, 'EMAIL_TLS', False),
        encoding=settings.DEFAULT_CHARSET):
    """Sends a message to each receipient in list.
    
    Given a datatuple of (subject, message, from_email, recipient_list), sends
    each message to each recipient list. Returns the number of e-mails sent.
    
    If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
    If auth_user and auth_password are set, they're used to log in.
    Note that the message parameter can be either text or one of the
    SafeMIMExxx methods listed above.
    """
    try:
        SMTP = smtplib.SMTP
        if settings.EMAIL_DEBUG:
            SMTP = STMPMock
        server = SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
        server.ehlo()
        server.esmtp_features["auth"] = "LOGIN PLAIN"
        if tls:
            server.starttls()
            server.ehlo()
        if auth_user and auth_password:
            server.login(auth_user, auth_password)
    except:
        if fail_silently:
            return
        raise
    num_sent = 0

    for subject, message, from_email, recipient_list, cc_list in datatuple:
        if not recipient_list:
            continue
        from_email = from_email or settings.DEFAULT_FROM_EMAIL
        #################################################
        msg = None
        if isinstance(message, SafeMIMEText) or isinstance(message, SafeMIMEMultipart):
            ## Change below is important!
            ## msg does not act as a proper dictionary... msg['key'] = value does not
            ## reset the value for msg['key'], but adds to it!
            msg = copy.deepcopy(message)
        else:
            msg = SafeMIMEText(message.encode(encoding), 'plain', encoding)
        #################################################
        # TODO: we should encode header fields that aren't pure ASCII, see:
        # http://maxischenko.in.ua/blog/entries/103/python-emails-i18n/
        msg['Subject'] = Header(subject, encoding)
        msg['From'] = from_email
        msg['To'] = ', '.join(recipient_list)
        msg['Date'] = rfc822.formatdate()
        if cc_list:
            msg['Cc'] = ', '.join(cc_list)
            recipient_list.extend(cc_list)
        if extra:
            for key in extra.keys():
                msg[key] = extra[key]
        try:
            server.sendmail(from_email, recipient_list, msg.as_string())
            num_sent += 1
        except:
            if not fail_silently:
                raise
    try:
        server.quit()
    except:
        if fail_silently:
            return
        raise
    return num_sent