# HG changeset patch # User Brian Neal # Date 1323742075 0 # Node ID 9e7ae8462f3f5c585305c597252639f3f66571d7 # Parent 3fd369e1197f74f26de2c652d8330b4ff2d36d91 Using celery to send mail out of the request/response cycle. diff -r 3fd369e1197f -r 9e7ae8462f3f gpp/accounts/forms.py --- a/gpp/accounts/forms.py Sun Dec 11 21:31:10 2011 +0000 +++ b/gpp/accounts/forms.py Tue Dec 13 02:07:55 2011 +0000 @@ -144,8 +144,7 @@ }) subject = 'Registration Confirmation for ' + site.name - send_mail(subject, msg, admin_email, [self.cleaned_data['email']], - expedite=True) + send_mail(subject, msg, admin_email, [self.cleaned_data['email']]) logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s', self.cleaned_data['email'], pending_user.username, self.ip) diff -r 3fd369e1197f -r 9e7ae8462f3f gpp/core/functions.py --- a/gpp/core/functions.py Sun Dec 11 21:31:10 2011 +0000 +++ b/gpp/core/functions.py Tue Dec 13 02:07:55 2011 +0000 @@ -3,34 +3,35 @@ import re import logging -import django.core.mail from django.contrib.sites.models import Site from django.conf import settings -import mailer +import core.tasks -def send_mail(subject, message, from_email, recipient_list, - fail_silently=False, auth_user=None, auth_password=None, - expedite=False): - """The main gpp send email function. - Use this function to send email from the site. It will obey debug settings and - log all emails. The expedite flag, when True, will bypass the mail queue. +def send_mail(subject, message, from_email, recipient_list, **kwargs): """ + The main send email function. Use this function to send email from the + site. All applications should use this function instead of calling + Django's directly. + If settings.GPP_SEND_EMAIL is true, the email will be sent to a Celery task + to actually send the email. Otherwise it is dropped. In any event, the + email will be logged at the DEBUG level. + + """ + # Guard against empty email addresses recipient_list = [dest for dest in recipient_list if dest] if not recipient_list: logging.warning("Empty recipient_list in send_mail") return - if settings.MAILER_ENQUEUE_MAIL and not expedite: - mailer.enqueue_mail(subject, message, from_email, recipient_list) - elif settings.GPP_SEND_EMAIL: - django.core.mail.send_mail(subject, message, from_email, recipient_list, - fail_silently, auth_user, auth_password) - logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s', from_email, str(recipient_list), subject, message) + if settings.GPP_SEND_EMAIL: + core.tasks.send_mail.delay(subject, message, from_email, recipient_list, + **kwargs) + def email_admins(subject, message): """Emails the site admins. Goes through the site send_mail function.""" diff -r 3fd369e1197f -r 9e7ae8462f3f gpp/core/tasks.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/core/tasks.py Tue Dec 13 02:07:55 2011 +0000 @@ -0,0 +1,16 @@ +""" +Celery tasks for the core application. + +""" +from celery.task import task +import django.core.mail + + +@task +def add(x, y): + return x + y + +@task +def send_mail(subject, message, from_email, recipient_list, **kwargs): + django.core.mail.send_mail(subject, message, from_email, recipient_list, + **kwargs) diff -r 3fd369e1197f -r 9e7ae8462f3f gpp/settings/base.py --- a/gpp/settings/base.py Sun Dec 11 21:31:10 2011 +0000 +++ b/gpp/settings/base.py Tue Dec 13 02:07:55 2011 +0000 @@ -219,7 +219,6 @@ ####################################################################### GPP_LOG_LEVEL = 0 GPP_SEND_EMAIL = True -MAILER_ENQUEUE_MAIL = True GPP_NO_REPLY_EMAIL = 'no_reply' AVATAR_DIR = 'avatars' MAX_AVATAR_SIZE_BYTES = 2 * 1024 * 1024