created amo.utils.send_mail with email blacklist checking
This commit is contained in:
Родитель
6062b35381
Коммит
accf69945d
|
@ -0,0 +1,31 @@
|
|||
from django import test
|
||||
from django.conf import settings
|
||||
from django.core import mail
|
||||
|
||||
from nose.tools import eq_
|
||||
|
||||
from amo.utils import send_mail
|
||||
|
||||
|
||||
class SendMailTest(test.TestCase):
|
||||
|
||||
def test_blacklist(self):
|
||||
to = 'nobody@mozilla.org'
|
||||
settings.EMAIL_BLACKLIST = (to,)
|
||||
success = send_mail('test subject', 'test body',
|
||||
recipient_list=[to], fail_silently=False)
|
||||
|
||||
assert success
|
||||
eq_(len(mail.outbox), 0)
|
||||
|
||||
def test_success(self):
|
||||
to = 'nobody@mozilla.org'
|
||||
settings.EMAIL_BLACKLIST = ()
|
||||
success = send_mail('test subject', 'test body',
|
||||
recipient_list=[to], fail_silently=False)
|
||||
|
||||
assert success
|
||||
eq_(len(mail.outbox), 1)
|
||||
assert mail.outbox[0].subject.find('test subject') == 0
|
||||
assert mail.outbox[0].body.find('test body') == 0
|
||||
|
|
@ -1,7 +1,10 @@
|
|||
import functools
|
||||
import urllib
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import paginator
|
||||
from django.core.mail import send_mail as django_send_mail
|
||||
|
||||
|
||||
def paginate(request, queryset, per_page=20):
|
||||
|
@ -23,3 +26,40 @@ def paginate(request, queryset, per_page=20):
|
|||
base = request.build_absolute_uri(request.path)
|
||||
paginated.url = u'%s?%s' % (base, urllib.urlencode(request.GET.items()))
|
||||
return paginated
|
||||
|
||||
|
||||
def send_mail(subject, message, from_email=None, recipient_list=None,
|
||||
fail_silently=False):
|
||||
"""
|
||||
A wrapper around django.core.mail.send_mail.
|
||||
|
||||
Adds blacklist checking and error logging.
|
||||
"""
|
||||
log = logging.getLogger('z.amo')
|
||||
|
||||
if not recipient_list:
|
||||
return True
|
||||
|
||||
if not from_email:
|
||||
from_email = settings.EMAIL_FROM_DEFAULT
|
||||
|
||||
# Prune blacklisted emails.
|
||||
white_list = []
|
||||
for email in recipient_list:
|
||||
if email.lower() in settings.EMAIL_BLACKLIST:
|
||||
log.debug('Blacklisted email removed from list: %s' % email)
|
||||
else:
|
||||
white_list.append(email)
|
||||
try:
|
||||
if white_list:
|
||||
result = django_send_mail(subject, message, from_email, white_list,
|
||||
fail_silently=False)
|
||||
else:
|
||||
result = True
|
||||
except Exception as e:
|
||||
result = False
|
||||
log.error('send_mail failed with error: %s' % e)
|
||||
if not fail_silently:
|
||||
raise
|
||||
|
||||
return result
|
||||
|
|
|
@ -337,3 +337,11 @@ REDIRECT_SECRET_KEY = ''
|
|||
# Legacy Settings
|
||||
# used by old-style CSRF token
|
||||
CAKE_SESSION_TIMEOUT = 8640
|
||||
|
||||
# Email settings
|
||||
EMAIL_FROM_DEFAULT = 'nobody@mozilla.org'
|
||||
|
||||
# Please use all lowercase for the blacklist.
|
||||
EMAIL_BLACKLIST = (
|
||||
'nobody@mozilla.org',
|
||||
)
|
||||
|
|
Загрузка…
Ссылка в новой задаче