# HG changeset patch # User Brian Neal # Date 1424140248 21600 # Node ID 79a71b9d0a2a60fa6c5dece620766a708cb9a526 # Parent 24fc302f9076ce869aa8d9cfce34ffdc7b68064f Use Reply-To header when sending mail from other users. See issue #81. diff -r 24fc302f9076 -r 79a71b9d0a2a accounts/forms.py --- a/accounts/forms.py Mon Feb 16 18:58:49 2015 -0600 +++ b/accounts/forms.py Mon Feb 16 20:30:48 2015 -0600 @@ -183,7 +183,7 @@ # Send the confirmation email site = Site.objects.get_current() - admin_email = settings.ADMINS[0][1] + admin_email = settings.DEFAULT_FROM_EMAIL activation_link = 'http://%s%s' % (site.domain, reverse('accounts.views.register_confirm', kwargs = {'username' : pending_user.username, 'key' : pending_user.key})) @@ -199,8 +199,7 @@ }) subject = 'Registration Confirmation for ' + site.name - send_mail(subject, msg, admin_email, [self.cleaned_data['email']], - defer=False) + send_mail(subject, msg, admin_email, [self.cleaned_data['email']]) logger.info('Accounts/registration conf. email sent to %s for user %s; IP = %s', self.cleaned_data['email'], pending_user.username, self.ip) @@ -226,7 +225,7 @@ return site = Site.objects.get_current() - admin_email = settings.ADMINS[0][1] + admin_email = settings.DEFAULT_FROM_EMAIL subject = 'Forgotten username for %s' % site.name msg = render_to_string('accounts/forgot_user_email.txt', { @@ -234,7 +233,7 @@ 'site': site, 'admin_email': admin_email, }) - send_mail(subject, msg, admin_email, [email], defer=False) + send_mail(subject, msg, admin_email, [email]) logger.info('Forgotten username email sent to {} <{}>'.format( user.username, email)) diff -r 24fc302f9076 -r 79a71b9d0a2a contact/forms.py --- a/contact/forms.py Mon Feb 16 18:58:49 2015 -0600 +++ b/contact/forms.py Mon Feb 16 20:30:48 2015 -0600 @@ -38,8 +38,11 @@ 'site_name': site.name, 'user_name': self.cleaned_data['name'], 'user_email': self.cleaned_data['email'], + 'subject': self.cleaned_data['subject'], 'message': self.cleaned_data['message'], }) subject = site.name + ' Feedback: ' + self.cleaned_data['subject'] - send_mail(subject, msg, self.cleaned_data['email'], self.recipient_list) + from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain + send_mail(subject, msg, from_email, self.recipient_list, + reply_to=self.cleaned_data['email']) diff -r 24fc302f9076 -r 79a71b9d0a2a contact/tests/test_views.py --- a/contact/tests/test_views.py Mon Feb 16 18:58:49 2015 -0600 +++ b/contact/tests/test_views.py Mon Feb 16 20:30:48 2015 -0600 @@ -27,7 +27,8 @@ self.assertEqual(len(mail.outbox), 1) email = mail.outbox[0] self.assertEqual(len(email.recipients()), 1) - self.assertEqual(email.from_email, post_data['email']) + self.assertEqual(email.extra_headers['Reply-To'], post_data['email']) + self.assertEqual(email.from_email, 'no_reply@example.com') self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com') self.assertTrue(post_data['subject'] in email.subject) msg = email.message().as_string() diff -r 24fc302f9076 -r 79a71b9d0a2a core/functions.py --- a/core/functions.py Mon Feb 16 18:58:49 2015 -0600 +++ b/core/functions.py Mon Feb 16 20:30:48 2015 -0600 @@ -23,7 +23,7 @@ os.remove(path) -def send_mail(subject, message, from_email, recipient_list, defer=True, **kwargs): +def send_mail(subject, message, from_email, recipient_list, reply_to=None, defer=True): """ The main send email function. Use this function to send email from the site. All applications should use this function instead of calling @@ -39,15 +39,23 @@ logging.warning("Empty recipient_list in send_mail") return - logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s', - from_email, str(recipient_list), subject, message) + logging.debug('EMAIL:\nFrom: %s\nTo: %s\nReply-To: %s\nSubject: %s\nMessage:\n%s', + from_email, str(recipient_list), reply_to, subject, message) + + headers = {'Reply-To': reply_to} if reply_to else None + msg_kwargs = { + 'subject': subject, + 'body': message, + 'from_email': from_email, + 'to': recipient_list, + 'headers': headers, + } if defer: - core.tasks.send_mail.delay(subject, message, from_email, recipient_list, - **kwargs) + core.tasks.send_mail.delay(**msg_kwargs) else: - django.core.mail.send_mail(subject, message, from_email, recipient_list, - **kwargs) + msg = django.core.mail.EmailMessage(**msg_kwargs) + msg.send() def email_admins(subject, message): diff -r 24fc302f9076 -r 79a71b9d0a2a core/tasks.py --- a/core/tasks.py Mon Feb 16 18:58:49 2015 -0600 +++ b/core/tasks.py Mon Feb 16 20:30:48 2015 -0600 @@ -4,20 +4,22 @@ """ from __future__ import absolute_import +import django.core.mail from celery import shared_task -import django.core.mail import core.whos_online @shared_task -def send_mail(subject, message, from_email, recipient_list, **kwargs): +def send_mail(**kwargs): """ A task to send mail via Django. + kwargs must be a dict of keyword arguments for an EmailMessage. + """ - django.core.mail.send_mail(subject, message, from_email, recipient_list, - **kwargs) + msg = django.core.mail.EmailMessage(**kwargs) + msg.send() @shared_task diff -r 24fc302f9076 -r 79a71b9d0a2a messages/views.py --- a/messages/views.py Mon Feb 16 18:58:49 2015 -0600 +++ b/messages/views.py Mon Feb 16 20:30:48 2015 -0600 @@ -399,7 +399,7 @@ 'msgs': msgs, 'admin_email': admin_email, }) - send_mail(subject, msg_body, from_email, [email_addr], defer=False) + send_mail(subject, msg_body, from_email, [email_addr]) msg = '{} message{} sent to email.'.format(count, '' if count == 1 else 's') if count > 0: diff -r 24fc302f9076 -r 79a71b9d0a2a news/views.py --- a/news/views.py Mon Feb 16 18:58:49 2015 -0600 +++ b/news/views.py Mon Feb 16 20:30:48 2015 -0600 @@ -3,6 +3,7 @@ """ import datetime +from django.conf import settings from django.shortcuts import render_to_response from django.template import RequestContext from django.template.loader import render_to_string @@ -201,11 +202,11 @@ if request.method == 'POST': send_form = SendStoryForm(request.POST) if send_form.is_valid(): + site = Site.objects.get_current() to_name = send_form.name() to_email = send_form.email() from_name = get_full_name(request.user) - from_email = request.user.email - site = Site.objects.get_current() + from_email = settings.GPP_NO_REPLY_EMAIL + '@' + site.domain msg = render_to_string('news/send_story_email.txt', { @@ -218,7 +219,7 @@ }) subject = 'Interesting Story at ' + site.name - send_mail(subject, msg, from_email, [to_email]) + send_mail(subject, msg, from_email, [to_email], reply_to=request.user.email) return HttpResponseRedirect(reverse('news.views.email_thanks')) else: send_form = SendStoryForm() diff -r 24fc302f9076 -r 79a71b9d0a2a sg101/templates/contact/contact_email.txt --- a/sg101/templates/contact/contact_email.txt Mon Feb 16 18:58:49 2015 -0600 +++ b/sg101/templates/contact/contact_email.txt Mon Feb 16 20:30:48 2015 -0600 @@ -2,6 +2,7 @@ Sender's Name: {{ user_name }} Sender's Email: {{ user_email }} +Subject: {{ subject }} Message: {{ message|safe }}