carlessanagustin
12/27/2017 - 9:24 AM

Send email with Python

Send email with Python

#!/usr/bin/python

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

username = 'USERNAME'
password = 'PASSWORD'
msg = MIMEMultipart('mixed')

sender = 'sender@example.com'
recipient = 'recipient@example.com'

msg['Subject'] = 'Your Subject'
msg['From'] = sender
msg['To'] = recipient

text_message = MIMEText('It is a text message.', 'plain')
html_message = MIMEText('It is a html message.', 'html')
msg.attach(text_message)
msg.attach(html_message)

mailServer = smtplib.SMTP('mail.smtp2go.com', 2525) # 8025, 587 and 25 can also be used. 
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username, password)
mailServer.sendmail(sender, recipient, msg.as_string())
mailServer.close()

# from: https://www.smtp2go.com/setupguide/python/