25 | | def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD): |
26 | | """ |
27 | | Easy wrapper for sending a single message to a recipient list. All members |
28 | | of the recipient list will see the other recipients in the 'To' field. |
29 | | """ |
30 | | return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password) |
31 | | |
32 | | def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD): |
33 | | """ |
34 | | Given a datatuple of (subject, message, from_email, recipient_list), sends |
35 | | each message to each recipient list. Returns the number of e-mails sent. |
36 | | |
37 | | If from_email is None, the DEFAULT_FROM_EMAIL setting is used. |
38 | | If auth_user and auth_password are set, they're used to log in. |
39 | | """ |
40 | | try: |
41 | | server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT) |
42 | | if auth_user and auth_password: |
43 | | server.login(auth_user, auth_password) |
44 | | except: |
45 | | if fail_silently: |
46 | | return |
47 | | raise |
48 | | num_sent = 0 |
49 | | for subject, message, from_email, recipient_list in datatuple: |
50 | | if not recipient_list: |
51 | | continue |
52 | | from_email = from_email or settings.DEFAULT_FROM_EMAIL |
53 | | msg = SafeMIMEText(message, 'plain', settings.DEFAULT_CHARSET) |
54 | | msg['Subject'] = subject |
55 | | msg['From'] = from_email |
56 | | msg['To'] = ', '.join(recipient_list) |
| 27 | class EmailConnection(object): |
| 28 | """An SMTP connection.""" |
| 29 | |
| 30 | def __init__(self, host=settings.EMAIL_HOST, port=settings.EMAIL_PORT, |
| 31 | username=settings.EMAIL_HOST_USER, |
| 32 | password=settings.EMAIL_HOST_PASSWORD, fail_silently=False): |
| 33 | self.host = host |
| 34 | self.port = port |
| 35 | self.username = username |
| 36 | self.password = password |
| 37 | self.fail_silently = fail_silently |
| 38 | self.connection = None |
| 39 | |
| 40 | def open(self): |
| 41 | """Open the connection to the email server.""" |
| 42 | |
| 43 | # Nothing to do if the connection is already open. |
| 44 | if self.connection: |
| 45 | return |
| 46 | try: |
| 47 | self.connection = smtplib.SMTP(self.host, self.port) |
| 48 | if self.username and self.password: |
| 49 | self.connection.login(self.username, self.password) |
| 50 | except: |
| 51 | if self.fail_silently: |
| 52 | return |
| 53 | raise |
| 54 | |
| 55 | def close(self): |
| 56 | """Close the connection to the email server.""" |
| 57 | |
| 58 | # Python versions < 2.5 do not allow try...except...finally, |
| 59 | # so we nest a try...except in a try...finally here for backwards |
| 60 | # compatibility. |
| 61 | try: |
| 62 | try: |
| 63 | self.connection.quit() |
| 64 | except: |
| 65 | if self.fail_silently: |
| 66 | return |
| 67 | raise |
| 68 | finally: |
| 69 | self.connection = None |
| 70 | |
| 71 | def send_messages(self, email_messages): |
| 72 | """ |
| 73 | Send one or more EmailMessage objects and return the number of email |
| 74 | messages sent. |
| 75 | """ |
| 76 | |
| 77 | if not email_messages: |
| 78 | return |
| 79 | self.open() |
| 80 | num_sent = 0 |
| 81 | for message in email_messages: |
| 82 | sent = self._send(message) |
| 83 | if sent: |
| 84 | num_sent += 1 |
| 85 | self.close() |
| 86 | return num_sent |
| 87 | |
| 88 | def _send(self, email_message): |
| 89 | """A helper method that does the actual sending.""" |
| 90 | |
| 91 | if not email_message.to: |
| 92 | return False |
| 93 | msg = SafeMIMEText(email_message.body, 'plain', |
| 94 | settings.DEFAULT_CHARSET) |
| 95 | msg['Subject'] = email_message.subject |
| 96 | msg['From'] = email_message.from_email |
| 97 | msg['To'] = ', '.join(email_message.to) |
62 | | msg['Message-ID'] = "<%d.%s@%s>" % (time.time(), random_bits, DNS_NAME) |
63 | | try: |
64 | | server.sendmail(from_email, recipient_list, msg.as_string()) |
65 | | num_sent += 1 |
66 | | except: |
67 | | if not fail_silently: |
68 | | raise |
69 | | try: |
70 | | server.quit() |
71 | | except: |
72 | | if fail_silently: |
73 | | return |
74 | | raise |
75 | | return num_sent |
| 116 | return "<%d.%s@%s>" % (time.time(), random_bits, DNS_NAME) |
| 117 | |
| 118 | class EmailMessage(object): |
| 119 | """ |
| 120 | A container for email information. |
| 121 | """ |
| 122 | |
| 123 | def __init__(self, to=None, from_email=settings.DEFAULT_FROM_EMAIL, |
| 124 | subject="", body="", connection=None): |
| 125 | self.to = to or [] |
| 126 | self.from_email = from_email |
| 127 | self.subject = subject |
| 128 | self.body = body |
| 129 | |
| 130 | def send(self, connection=None, fail_silently=False): |
| 131 | """Send the email message.""" |
| 132 | |
| 133 | connection = connection or EmailConnection(fail_silently=fail_silently) |
| 134 | connection.send_messages([self]) |