Django

Code

Ticket #2897: django-mail-tls-patch.2.txt

File django-mail-tls-patch.2.txt, 2.3 kB (added by bjorn@exoweb.net, 2 years ago)

New version w/ default config variable global_settings

Line 
1 Index: django/conf/global_settings.py
2 ===================================================================
3 --- django/conf/global_settings.py      (revision 4908)
4 +++ django/conf/global_settings.py      (working copy)
5 @@ -108,6 +108,7 @@
6  # Optional SMTP authentication information for EMAIL_HOST.
7  EMAIL_HOST_USER = ''
8  EMAIL_HOST_PASSWORD = ''
9 +EMAIL_TLS = False   # encrypt the connection using TLS/SSL?
10  
11  # List of strings representing installed apps.
12  INSTALLED_APPS = ()
13 Index: django/core/mail.py
14 ===================================================================
15 --- django/core/mail.py (revision 4908)
16 +++ django/core/mail.py (working copy)
17 @@ -17,14 +17,14 @@
18              val = Header(val, settings.DEFAULT_CHARSET)
19          MIMEText.__setitem__(self, name, val)
20  
21 -def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
22 +def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, tls=getattr(settings, 'EMAIL_TLS', False)):
23      """
24      Easy wrapper for sending a single message to a recipient list. All members
25      of the recipient list will see the other recipients in the 'To' field.
26      """
27 -    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
28 +    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password, tls)
29  
30 -def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
31 +def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, tls=getattr(settings, 'EMAIL_TLS', False)):
32      """
33      Given a datatuple of (subject, message, from_email, recipient_list), sends
34      each message to each recipient list. Returns the number of e-mails sent.
35 @@ -34,6 +34,9 @@
36      """
37      try:
38          server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
39 +        if tls:
40 +            server.starttls()
41 +            server.ehlo()
42          if auth_user and auth_password:
43              server.login(auth_user, auth_password)
44      except: