This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from ryanpitts/development
add test email task
- Loading branch information
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Testing the Amazon email setup. Text-only email here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |