Skip to content

Commit

Permalink
Optional integration with django_templated_email.
Browse files Browse the repository at this point in the history
  • Loading branch information
yesimon committed Apr 2, 2012
1 parent 700951f commit 2a09aca
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
17 changes: 15 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Installation
Settings
--------


``BETA_INVITE_CODE_LENGTH``
String length of the invitation_code
``BETA_ENABLE_BETA``
Expand Down Expand Up @@ -48,7 +47,21 @@ Settings
``BETA_EMAIL_INVITE_FUNCTION``
Function for sending out the invitation code

Integrating with django_social_auth
Integration with django_templated_email
---------------------------------------

If django_templated_email is installed, you can use customized
``*.email`` templates with an example setting such as::

BETA_EMAIL_TEMPLATES_DIR = 'beta'

And create the following templates::

``<project_dir>/templates/templated_email/beta/beta_confirm.email``
``<project_dir>/templates/templated_email/beta/beta_invite.email``


Integration with django_social_auth
-----------------------------------

Modify ``SOCIAL_AUTH_PIPELINE`` in settings to replace
Expand Down
64 changes: 44 additions & 20 deletions hunger/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,38 @@
from django.template import Context
from hunger.utils import setting

try:
from templated_email import send_templated_mail
templated_email_available = True
except ImportError:
templated_email_available = False

def beta_confirm(email, **kwargs):
"""
Send out email confirming that they requested an invite.
"""

templates_folder = setting('BETA_EMAIL_TEMPLATES_DIR', 'hunger')
plaintext = get_template(os.path.join(templates_folder, 'beta_confirm.txt'))
html = get_template(os.path.join(templates_folder, 'beta_confirm.html'))

from_email = setting('EMAIL_HOST_USER')
subject, to = 'You requested an invite!', email
text_content = plaintext.render(Context())
html_content = html.render(Context())
msg = EmailMultiAlternatives(subject, text_content, from_email, [to],
headers={'From': 'Mailer <%s>' % from_email})
msg.attach_alternative(html_content, "text/html")
msg.send()

context_dict = kwargs.copy()
if templated_email_available:
send_templated_mail(
template_name=os.path.join(templates_folder, 'beta_confirm'),
from_email=from_email,
recipient_list=[email],
context=context_dict,
)
else:
plaintext = get_template(os.path.join(templates_folder, 'beta_confirm.txt'))
html = get_template(os.path.join(templates_folder, 'beta_confirm.html'))
subject, to = 'You requested an invite!', email
text_content = plaintext.render(Context())
html_content = html.render(Context())
msg = EmailMultiAlternatives(subject, text_content, from_email, [to],
headers={'From': 'Mailer <%s>' % from_email})
msg.attach_alternative(html_content, "text/html")
msg.send()

def beta_invite(email, code, **kwargs):
"""
Expand All @@ -33,14 +48,23 @@ def beta_invite(email, code, **kwargs):
context = Context(context_dict)

templates_folder = setting('BETA_EMAIL_TEMPLATES_DIR', 'hunger')
plaintext = get_template(os.path.join(templates_folder, 'beta_invite.txt'))
html = get_template(os.path.join(templates_folder, 'beta_invite.html'))

from_email = setting('EMAIL_HOST_USER')
subject, to = "Here is your invite", email
text_content = plaintext.render(context)
html_content = html.render(context)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to],
headers={'From': 'Mailer <%s>' % from_email})
msg.attach_alternative(html_content, "text/html")
msg.send()

if templated_email_available:
send_templated_mail(
template_name=os.path.join(templates_folder, 'beta_invite'),
from_email=from_email,
recipient_list=[email],
context=context_dict,
)
else:
plaintext = get_template(os.path.join(templates_folder, 'beta_invite.txt'))
html = get_template(os.path.join(templates_folder, 'beta_invite.html'))

subject, to = "Here is your invite", email
text_content = plaintext.render(context)
html_content = html.render(context)
msg = EmailMultiAlternatives(subject, text_content, from_email, [to],
headers={'From': 'Mailer <%s>' % from_email})
msg.attach_alternative(html_content, "text/html")
msg.send()
1 change: 0 additions & 1 deletion hunger/templates/hunger/beta_confirm.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
We have received your request for invite to the private beta.
We'll contact you as soon as we are ready!

6 changes: 6 additions & 0 deletions hunger/templates/templated_email/hunger/beta_confirm.email
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% block subject %}You requested an invite!{% endblock %}

{% block plain %}
We have received your request for invite to the private beta.
We'll contact you as soon as we are ready!
{% endblock %}
9 changes: 9 additions & 0 deletions hunger/templates/templated_email/hunger/beta_invite.email
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% block subject %}Here is your invite{% endblock %}

{% block plain %}
Visit {{ request.get_host }}/invites/verify/{{ code }}/ to join the private beta.
{% endblock %}

{% block html %}
Visit <a href="{{ request.get_host }}/invites/verify/{{ code }}/" target="_blank">{{ request.get_host }}/invites/verify/{{ code }}/</a> to join the private beta.
{% endblock %}

0 comments on commit 2a09aca

Please sign in to comment.