How to set up a local email server with Python. Who needs MailChimp?

I needed to send a lot of emails. I didn’t want to deal with an email sending company like MailChimp. I knew Python had some libraries that handled SMTP. I thought I’d check it out to see if I can build a localized email server and just send emails from my PC.

Python’s smtplib library does exactly that. Whew, that was quick. Just have to read the docs and check a tutorial or two. Check the source below to set it up for yourself.

import smtplib, ssl

host = ''
port = ''
local_host = 'localhost'
email= ''
password = ''
receivers = [''] # receivers must be defined as a List. Get over it.

smtpObj = smtplib.SMTP_SSL(host, port)
smtpObj.login(sender, password)

message = """
From: From Peter Krysik <peter@peter.com>
To: Peter Krysik <peter@peter.com>
MIME-Version: 1.0
Content-type: text/html
Subject: 

<p>This is the content of the email. It works with HTML and CSS.</p>

<p><span style="float: left; width: 0.8em; font-size: 600%; font-family: algerian, courier; line-height: 80%;">S</span>ee?</p>
 
"""
smtpObj.sendmail(email, receivers, message) # send the email.
with smtplib.SMTP_SSL(host, port, context=context) as server:
    server.login(email, password)
    server.sendmail(email, receivers, message) # send the email 2: this works, too.
Leave your thoughts below. Follow on Twitter