Add html email
This commit is contained in:
11
README.md
11
README.md
@@ -20,13 +20,20 @@
|
||||
* setup a proper fake smtp server
|
||||
- a few names: Mailcatcher, Maildev, Mailhog
|
||||
- Mailhog: free software, easy to install (go, docker)
|
||||
- docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
|
||||
+ wget https://github.com/mailhog/MailHog/releases/download/v1.0.1/MailHog_linux_amd64
|
||||
+ docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
|
||||
|
||||
## Other problems with sending mail
|
||||
|
||||
* in dev/testing env, we don't really want to send to the actual recipients
|
||||
- but we still want to craft the email for them
|
||||
*
|
||||
|
||||
## Other benefits
|
||||
|
||||
* can test email displays as wanted
|
||||
* can check attachments (receipt, invoice...)
|
||||
* API -> can be used in unit tests / CI
|
||||
* Test deliverability issues with Jim: MailHog's Chaos Monkey
|
||||
|
||||
|
||||
## Links
|
||||
|
||||
51
app.py
51
app.py
@@ -2,6 +2,50 @@
|
||||
|
||||
from bottle import get, post, run, template
|
||||
import smtplib
|
||||
from email.mime.text import MIMEText
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
|
||||
def craft_email(from_address, to_address, subject):
|
||||
return f"""\
|
||||
From: {from_address}
|
||||
To: {to_address}
|
||||
Subject: {subject}
|
||||
|
||||
You're just one click away from SuperBuzz bliss! Click this confirmation link:
|
||||
[confirmation link].
|
||||
"""
|
||||
|
||||
def craft_fancy_email(from_address, to_address, subject):
|
||||
message = MIMEMultipart("alternative")
|
||||
message["Subject"] = "multipart test"
|
||||
message["From"] = from_address
|
||||
message["To"] = to_address
|
||||
|
||||
# Create the plain-text and HTML version of your message
|
||||
text = """\
|
||||
You're just one click away from SuperBuzz bliss! Click this confirmation link:
|
||||
[confirmation link]."""
|
||||
html = """\
|
||||
<html>
|
||||
<body>
|
||||
<p>Tou're just one click away from SuperBuzz bliss!<br />
|
||||
Click this confirmation link:<br />
|
||||
<a href="https://media.giphy.com/media/9g8PH1MbwTy4o/giphy.gif">Confirm SuperBuzz</a>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
# Turn these into plain/html MIMEText objects
|
||||
part1 = MIMEText(text, "plain")
|
||||
part2 = MIMEText(html, "html")
|
||||
|
||||
# Add HTML/plain-text parts to MIMEMultipart message
|
||||
# The email client will try to render the last part first
|
||||
message.attach(part1)
|
||||
message.attach(part2)
|
||||
|
||||
return message.as_string()
|
||||
|
||||
|
||||
def send_confirm_email():
|
||||
@@ -11,11 +55,8 @@ def send_confirm_email():
|
||||
server = smtplib.SMTP(host, port)
|
||||
FROM = "welcome@superbuzz.com"
|
||||
TO = "you@example.com"
|
||||
MSG = """Subject: Welcome to SuperBuzz!
|
||||
|
||||
You're just one click away from SuperBuzz bliss! Click this confirmation link:
|
||||
[confirmation link].
|
||||
"""
|
||||
#MSG = craft_email(FROM, TO, 'Welcome to SuperBuzz!')
|
||||
MSG = craft_fancy_email(FROM, TO, 'Welcome to SuperBuzz!')
|
||||
server.sendmail(FROM, TO, MSG)
|
||||
server.quit()
|
||||
print('Confimration email sent.')
|
||||
|
||||
Reference in New Issue
Block a user