Skip to content

Commit

Permalink
Merge 867c49d into 9fba9e1
Browse files Browse the repository at this point in the history
  • Loading branch information
subramanyamVemu committed Aug 25, 2015
2 parents 9fba9e1 + 867c49d commit c6e60bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
18 changes: 11 additions & 7 deletions mailer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def get_version():

def get_priority(priority):
from mailer.models import PRIORITY_MAPPING, PRIORITY_MEDIUM

if priority is None:
priority = PRIORITY_MEDIUM

Expand All @@ -38,7 +39,7 @@ def get_priority(priority):
# replacement for django.core.mail.send_mail

def send_mail(subject, message, from_email, recipient_list, priority=None,
fail_silently=False, auth_user=None, auth_password=None):
fail_silently=False, auth_user=None, auth_password=None, connection=None):
from django.utils.encoding import force_text
from mailer.models import make_message

Expand All @@ -51,13 +52,14 @@ def send_mail(subject, message, from_email, recipient_list, priority=None,
body=message,
from_email=from_email,
to=recipient_list,
priority=priority).save()
priority=priority,
connection=connection).save()
return 1


def send_html_mail(subject, message, message_html, from_email, recipient_list,
priority=None, fail_silently=False, auth_user=None,
auth_password=None, headers={}):
auth_password=None, connection=None, headers={}):
"""
Function to queue HTML e-mails
"""
Expand All @@ -75,13 +77,15 @@ def send_html_mail(subject, message, message_html, from_email, recipient_list,
body=message,
from_email=from_email,
to=recipient_list,
priority=priority)
priority=priority,
connection=connection)
email = msg.email
email = EmailMultiAlternatives(
email.subject,
email.body,
email.from_email,
email.to,
connection=email.get_connection(),
headers=headers
)
email.attach_alternative(message_html, "text/html")
Expand All @@ -94,7 +98,7 @@ def send_mass_mail(datatuple, fail_silently=False, auth_user=None,
auth_password=None, connection=None):
num_sent = 0
for subject, message, sender, recipient in datatuple:
num_sent += send_mail(subject, message, sender, recipient)
num_sent += send_mail(subject, message, sender, recipient, connection=connection)
return num_sent


Expand All @@ -105,7 +109,7 @@ def mail_admins(subject, message, fail_silently=False, connection=None, priority
return send_mail(settings.EMAIL_SUBJECT_PREFIX + force_text(subject),
message,
settings.SERVER_EMAIL,
[a[1] for a in settings.ADMINS])
[a[1] for a in settings.ADMINS], connection=connection)


def mail_managers(subject, message, fail_silently=False, connection=None, priority=None):
Expand All @@ -115,4 +119,4 @@ def mail_managers(subject, message, fail_silently=False, connection=None, priori
return send_mail(settings.EMAIL_SUBJECT_PREFIX + force_text(subject),
message,
settings.SERVER_EMAIL,
[a[1] for a in settings.MANAGERS])
[a[1] for a in settings.MANAGERS], connection=connection)
4 changes: 2 additions & 2 deletions mailer/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ def send_all():
sent = 0

try:
connection = None
for message in prioritize():
try:
connection = message.email.get_connection()
if connection is None:
connection = get_connection(backend=EMAIL_BACKEND)
connection = get_connection(backend=EMAIL_BACKEND)
logging.info("sending message '{0}' to {1}".format(
message.subject.encode("utf-8"),
u", ".join(message.to_addresses).encode("utf-8"))
Expand Down
15 changes: 6 additions & 9 deletions mailer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

try:
from django.utils.timezone import now as datetime_now

datetime_now # workaround for pyflakes
except ImportError:
from datetime import datetime

datetime_now = datetime.now

from django.core.mail import EmailMessage
from django.db import models
from django.utils.translation import ugettext_lazy as _


PRIORITY_HIGH = "1"
PRIORITY_MEDIUM = "2"
PRIORITY_LOW = "3"
Expand All @@ -32,7 +33,6 @@


class MessageManager(models.Manager):

def high_priority(self):
"""
the high priority messages in the queue
Expand Down Expand Up @@ -101,7 +101,6 @@ def db_to_email(data):


class Message(models.Model):

# The actual data - a pickled EmailMessage
message_data = models.TextField()
when_added = models.DateTimeField(default=datetime_now)
Expand Down Expand Up @@ -169,7 +168,7 @@ def filter_recipient_list(lst):


def make_message(subject="", body="", from_email=None, to=None, bcc=None,
attachments=None, headers=None, priority=None):
attachments=None, headers=None, priority=None, connection=None):
"""
Creates a simple message for the email parameters supplied.
The 'to' and 'bcc' lists are filtered using DontSendEntry.
Expand All @@ -181,22 +180,23 @@ def make_message(subject="", body="", from_email=None, to=None, bcc=None,
"""
to = filter_recipient_list(to)
bcc = filter_recipient_list(bcc)

core_msg = EmailMessage(
subject=subject,
body=body,
from_email=from_email,
to=to,
bcc=bcc,
attachments=attachments,
headers=headers
headers=headers,
connection=connection
)
db_msg = Message(priority=priority)
db_msg.email = core_msg
return db_msg


class DontSendEntryManager(models.Manager):

def has_address(self, address):
"""
is the given address on the don't send list?
Expand All @@ -206,7 +206,6 @@ def has_address(self, address):


class DontSendEntry(models.Model):

to_address = models.EmailField(max_length=254)
when_added = models.DateTimeField()
# @@@ who added?
Expand All @@ -232,7 +231,6 @@ class Meta:


class MessageLogManager(models.Manager):

def log(self, message, result_code, log_message=""):
"""
create a log entry for an attempt to send the given message and
Expand All @@ -249,7 +247,6 @@ def log(self, message, result_code, log_message=""):


class MessageLog(models.Model):

# fields from Message
message_data = models.TextField()
when_added = models.DateTimeField(db_index=True)
Expand Down

0 comments on commit c6e60bf

Please sign in to comment.