From 130e1c7f8c43261d0e85e5ff9a354f037bb7cd24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Mon, 18 Nov 2013 10:25:24 +0100 Subject: [PATCH] Send mail with better from/reply-to headers --- wtem/management/commands/__init__.py | 16 ++++++++++++++++ wtem/management/commands/wtem_email_teachers.py | 10 ++++------ wtem/management/commands/wtem_send_keys.py | 10 ++++------ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/wtem/management/commands/__init__.py b/wtem/management/commands/__init__.py index e69de29..494edb6 100644 --- a/wtem/management/commands/__init__.py +++ b/wtem/management/commands/__init__.py @@ -0,0 +1,16 @@ +from django.core.mail import EmailMessage +from django.conf import settings + +def send_mail(subject, body, to): + if not isinstance(to, list): + to = [to] + + reply_to = getattr(settings, 'WTEM_REPLY_TO', None) + headers = dict() + if reply_to: + headers['Reply-To'] = reply_to + + email = EmailMessage(subject, body, + getattr(settings, 'WTEM_FROM', 'edukacjamedialna@nowoczesnapolska.org.pl'), + to, headers = headers) + email.send(fail_silently = False) \ No newline at end of file diff --git a/wtem/management/commands/wtem_email_teachers.py b/wtem/management/commands/wtem_email_teachers.py index 3aba80d..e0986f8 100644 --- a/wtem/management/commands/wtem_email_teachers.py +++ b/wtem/management/commands/wtem_email_teachers.py @@ -5,7 +5,7 @@ from optparse import make_option from django.core.management.base import BaseCommand, CommandError from django.conf import settings -from django.core.mail import send_mail +from wtem.management.commands import send_mail from django.template.loader import render_to_string from contact.models import Contact @@ -40,10 +40,8 @@ class Command(BaseCommand): def send_message(self, message, subject, email): self.stdout.write('>>> sending to %s' % email) send_mail( - subject, - message, - getattr(settings, 'WTEM_CONTACT_EMAIL', 'no-reply@edukacjamedialna.edu.pl'), - [email], - fail_silently=False + subject = subject, + body = message, + to = [email] ) diff --git a/wtem/management/commands/wtem_send_keys.py b/wtem/management/commands/wtem_send_keys.py index cff7bf3..05ffcbb 100644 --- a/wtem/management/commands/wtem_send_keys.py +++ b/wtem/management/commands/wtem_send_keys.py @@ -5,7 +5,7 @@ from optparse import make_option from django.core.management.base import BaseCommand, CommandError from django.conf import settings -from django.core.mail import send_mail +from wtem.management.commands import send_mail from django.template.loader import render_to_string from wtem.models import Submission, DEBUG_KEY @@ -63,9 +63,7 @@ class Command(BaseCommand): def send_key(self, submission): self.stdout.write('>>> sending to ' + submission.email) send_mail( - "WTEM - Twój link do zadań", - render_to_string('wtem/email_key.txt', dict(submission = submission)), - getattr(settings, 'WTEM_CONTACT_EMAIL', 'no-reply@edukacjamedialna.edu.pl'), - [submission.email], - fail_silently=False + subject = "WTEM - Twój link do zadań", + body = render_to_string('wtem/email_key.txt', dict(submission = submission)), + to = [submission.email] ) \ No newline at end of file -- 2.20.1