Python send simple HTML email with SMTP

Hi, below are the basic setting for sending email with SMTP in Python:

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart('alternative')
msg['From'] = ''
msg['To'] = ''
msg['Subject'] = 'Basic SMTP email with Python'
text = "Test body email"

html = """\
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <head>Test simple HTML email with Python!</head>

html += "<p>Test body email with Python</p>"

html += """

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part2) mailserver = smtplib.SMTP('',587) 

# identify ourselves to smtp gmail client 
# re-identify ourselves as an encrypted connection 
mailserver.login('', 'your_email_password_here') 

You can download my python project at

Credit to :


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s