Skip to content

Commit

Permalink
Allow for programmatically overriding 'from' email
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Mar 16, 2016
1 parent 3814105 commit c3e83db
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ def format_email_subject(self, subject):
prefix = "[{name}] ".format(name=site.name)
return prefix + force_text(subject)

def get_from_email(self):
"""
This is a hook that can be overridden to programatically
set the 'from' email address for sending emails
"""
return settings.DEFAULT_FROM_EMAIL

def render_mail(self, template_prefix, email, context):
"""
Renders an e-mail to `email`. `template_prefix` identifies the
Expand All @@ -100,6 +107,8 @@ def render_mail(self, template_prefix, email, context):
subject = " ".join(subject.splitlines()).strip()
subject = self.format_email_subject(subject)

from_email = self.get_from_email()

bodies = {}
for ext in ['html', 'txt']:
try:
Expand All @@ -113,14 +122,14 @@ def render_mail(self, template_prefix, email, context):
if 'txt' in bodies:
msg = EmailMultiAlternatives(subject,
bodies['txt'],
settings.DEFAULT_FROM_EMAIL,
from_email,
[email])
if 'html' in bodies:
msg.attach_alternative(bodies['html'], 'text/html')
else:
msg = EmailMessage(subject,
bodies['html'],
settings.DEFAULT_FROM_EMAIL,
from_email,
[email])
msg.content_subtype = 'html' # Main content is now text/html
return msg
Expand Down

0 comments on commit c3e83db

Please sign in to comment.