Mercurial > public > sg101
comparison core/functions.py @ 892:79a71b9d0a2a
Use Reply-To header when sending mail from other users.
See issue #81.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 16 Feb 2015 20:30:48 -0600 |
parents | 97f8fab9b1a3 |
children | 51a2051588f5 |
comparison
equal
deleted
inserted
replaced
891:24fc302f9076 | 892:79a71b9d0a2a |
---|---|
21 finally: | 21 finally: |
22 fp.close() | 22 fp.close() |
23 os.remove(path) | 23 os.remove(path) |
24 | 24 |
25 | 25 |
26 def send_mail(subject, message, from_email, recipient_list, defer=True, **kwargs): | 26 def send_mail(subject, message, from_email, recipient_list, reply_to=None, defer=True): |
27 """ | 27 """ |
28 The main send email function. Use this function to send email from the | 28 The main send email function. Use this function to send email from the |
29 site. All applications should use this function instead of calling | 29 site. All applications should use this function instead of calling |
30 Django's directly. | 30 Django's directly. |
31 If defer is True, the email will be sent to a Celery task to actually send | 31 If defer is True, the email will be sent to a Celery task to actually send |
37 recipient_list = [dest for dest in recipient_list if dest] | 37 recipient_list = [dest for dest in recipient_list if dest] |
38 if not recipient_list: | 38 if not recipient_list: |
39 logging.warning("Empty recipient_list in send_mail") | 39 logging.warning("Empty recipient_list in send_mail") |
40 return | 40 return |
41 | 41 |
42 logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s', | 42 logging.debug('EMAIL:\nFrom: %s\nTo: %s\nReply-To: %s\nSubject: %s\nMessage:\n%s', |
43 from_email, str(recipient_list), subject, message) | 43 from_email, str(recipient_list), reply_to, subject, message) |
44 | |
45 headers = {'Reply-To': reply_to} if reply_to else None | |
46 msg_kwargs = { | |
47 'subject': subject, | |
48 'body': message, | |
49 'from_email': from_email, | |
50 'to': recipient_list, | |
51 'headers': headers, | |
52 } | |
44 | 53 |
45 if defer: | 54 if defer: |
46 core.tasks.send_mail.delay(subject, message, from_email, recipient_list, | 55 core.tasks.send_mail.delay(**msg_kwargs) |
47 **kwargs) | |
48 else: | 56 else: |
49 django.core.mail.send_mail(subject, message, from_email, recipient_list, | 57 msg = django.core.mail.EmailMessage(**msg_kwargs) |
50 **kwargs) | 58 msg.send() |
51 | 59 |
52 | 60 |
53 def email_admins(subject, message): | 61 def email_admins(subject, message): |
54 """Emails the site admins. Goes through the site send_mail function.""" | 62 """Emails the site admins. Goes through the site send_mail function.""" |
55 site = Site.objects.get_current() | 63 site = Site.objects.get_current() |