· 1 min read

How to send email with Python, smtplib and Postmark

Here is a quick code snippet showing how to send email via SMTP with Postmark without any dependencies. It assumes you are using Heroku and have added the addon. But if not just make sure your api keys are set as environment vars.

from os import environ as env
from email.mime.text import MIMEText
import smtplib

msg = MIMEText("Message body")
msg['To'] = "somone@example.com"
msg['Bcc'] = "anotherone@example.com"
msg['From'] ="fromme@example.com"
msg['Subject'] = "Subject line"
server = smtplib.SMTP('smtp.postmarkapp.com', 587)
server.starttls()
server.login(env.get('POSTMARK_API_KEY', '...'), env.get('POSTMARK_API_KEY', '...'))
server.sendmail(msg['From'], ["somone@example.com", "anotherone@example.com"], msg.as_string())
server.quit()

Comments

Leave a comment