Skip to content

Instantly share code, notes, and snippets.

@ragingbal
Created August 3, 2020 10:18
Show Gist options
  • Save ragingbal/2f912a922ad0dab434381ddb02db0514 to your computer and use it in GitHub Desktop.
Save ragingbal/2f912a922ad0dab434381ddb02db0514 to your computer and use it in GitHub Desktop.
import smtplib
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
import jinja2
msg = EmailMessage()
msg.set_content('test abc')
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'test 123'
msg['From'] = 'abc@xyz.com'
msg['To'] = 'abc@xyz.com'
# Send the message via our own SMTP server.
s = smtplib.SMTP('mailhog:1025')
templateLoader = jinja2.FileSystemLoader(searchpath="./")
templateEnv = jinja2.Environment(loader=templateLoader)
TEMPLATE_FILE = "test_message.jinja"
template = templateEnv.get_template(TEMPLATE_FILE)
outputText = template.render() # this is where to put args to the template renderer
asparagus_cid = make_msgid()
msg.add_alternative(outputText.format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
s.send_message(msg)
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment