Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #255 from ryanpitts/development
Browse files Browse the repository at this point in the history
add test email task
  • Loading branch information
ryanpitts committed May 1, 2014
2 parents ea4bbc3 + f02fa82 commit a11cdf8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
52 changes: 52 additions & 0 deletions source/jobs/management/commands/test_email_send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
Testing the Amazon email hookup.
'''
import logging
from datetime import datetime
from time import sleep

from django.conf import settings
from django.core.management.base import BaseCommand
from django.template.loader import render_to_string

from source.utils.email import send_multipart_email

logging.basicConfig(filename='email_job_reminders.log', filemode='w', level=logging.INFO)


class Command(BaseCommand):
help = 'Testing email via Amazon SMTP.'
def handle(self, *args, **options):
logging.info('Started job: %s' % datetime.now())

# add context for rendering personalized emails
subject = '[Source] Testing email via Amazon SMTP'
email_context = {}

# render text and html versions of email body
text_content = render_to_string(
'jobs/emails/job_post_reminder.txt',
email_context,
)
html_content = render_to_string(
'jobs/emails/job_post_reminder.html',
email_context
)

# send email to each recipient
email_recipients = ['ryan.a.pitts@gmail.com', 'john.p.schneider@gmail.com']
for recipient in email_recipients:
send_multipart_email(
subject = subject,
from_email = settings.DEFAULT_FROM_EMAIL,
to = recipient,
text_content = text_content,
html_content = html_content
)

# avoid rate limit
sleep(1)


logging.info('Finished job: %s' % datetime.now())

3 changes: 3 additions & 0 deletions source/jobs/templates/jobs/emails/job_post_reminder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>HTML version</h1>

<p>Testing the Amazon email setup. HTML email here.</p>
1 change: 1 addition & 0 deletions source/jobs/templates/jobs/emails/job_post_reminder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Testing the Amazon email setup. Text-only email here.
11 changes: 11 additions & 0 deletions source/utils/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.core.mail import EmailMultiAlternatives
from django.utils.encoding import smart_str


def send_multipart_email(subject, from_email, to, text_content, html_content):
text_content = smart_str(text_content)
html_content = smart_str(html_content)

msg = EmailMultiAlternatives(subject, text_content, from_email, [to,])
msg.attach_alternative(html_content, "text/html")
msg.send()

0 comments on commit a11cdf8

Please sign in to comment.