Django

Code

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

File django-mail-tls-patch.3.txt, 3.2 kB (added by mail@ping13.net, 1 year ago)

modified and updated patch against rev 4815, works here with a hosted Google account

Line 
1 Index: django/conf/global_settings.py
2 ===================================================================
3 --- django/conf/global_settings.py      (revision 4815)
4 +++ django/conf/global_settings.py      (working copy)
5 @@ -118,6 +118,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 4815)
16 +++ django/core/mail.py (working copy)
17 @@ -34,21 +34,24 @@
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=None, auth_password=None):
22 +def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None,tls=None):
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      If auth_user is None, the EMAIL_HOST_USER setting is used.
28      If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
29 +    If tls is None, the EMAIL_TLS setting is used.
30      """
31      if auth_user is None:
32          auth_user = settings.EMAIL_HOST_USER
33      if auth_password is None:
34          auth_password = settings.EMAIL_HOST_PASSWORD
35 -    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
36 +    if tls is None:
37 +        tls=getattr(settings, 'EMAIL_TLS', False)
38 +    return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password,tls)
39  
40 -def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None):
41 +def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None,tls= None):
42      """
43      Given a datatuple of (subject, message, from_email, recipient_list), sends
44      each message to each recipient list. Returns the number of e-mails sent.
45 @@ -57,13 +60,20 @@
46      If auth_user and auth_password are set, they're used to log in.
47      If auth_user is None, the EMAIL_HOST_USER setting is used.
48      If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
49 +    If tls is None, the EMAIL_TLS setting is used.
50      """
51      if auth_user is None:
52          auth_user = settings.EMAIL_HOST_USER
53      if auth_password is None:
54          auth_password = settings.EMAIL_HOST_PASSWORD
55 +    if tls is None:
56 +        tls=getattr(settings, 'EMAIL_TLS', False)
57      try:
58          server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
59 +        if tls:
60 +            server.ehlo(settings.EMAIL_HOST)
61 +            server.starttls()
62 +            server.ehlo(settings.EMAIL_HOST)
63          if auth_user and auth_password:
64              server.login(auth_user, auth_password)
65      except:
66 @@ -92,7 +102,7 @@
67              if not fail_silently:
68                  raise
69      try:
70 -        server.quit()
71 +        server.close() # with .quit(), at least smtp.gmail.com complains
72      except:
73          if fail_silently:
74              return