import smtplib from email.mime.text import MIMEText as text def test_sendmail(): to_address ='"PersonA"<a.b@gmail.com>' body = "This is the email body!" subject="Hello World" sendmail(to_address, subject, body) def sendmail(to_address, subject, body): from_address='"This Is Me"<a.b@gmail.com>' smtp_server = 'smtp.gmail.com' smtp_port= 587 smtp_user="a.b@gmail.com" smtp_password="abcdefgh" #---------compose------- msg = text(body) msg['Subject'] = subject msg['From'] = from_address msg['To'] = to_address #---------send------- server = smtplib.SMTP(smtp_server, smtp_port) server.ehlo() server.starttls() server.login(smtp_user, smtp_password) server.sendmail(from_address, to_address, msg.as_string()) server.quit() print "done" def help(): print """ remember to turn on "Access for less secure apps" in GMail via Link beforehand https://www.google.com/settings/security/lesssecureapps """ if __name__ == '__main__': help() test_sendmail()
References
1. http://stackoverflow.com/questions/34953139/sending-from-gmail-with-smtplib