python发送邮件

#!/usr/bin/python
#coding:utf-8

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
mail_host = "smtp.163.com"
mailto_list = "[email protected]"
mail_user = "[email protected]"
mail_pass = "password"

message = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: SMTP e-mail test

This is a test e-mail message.
"""
def SendMail(sub,message):
    msg = MIMEMultipart()
    msg['From'] = mail_user
    msg['To'] = mailto_list
    msg['Subject'] = sub
    msg.attach(MIMEText(message, _charset="utf-8"))

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(mail_user,mailto_list, msg.as_string())
        print "Successfully sent email"
    except Exception, e:
        print e
        print "Error: unable to send email"
if __name__ == "__main__":
    SendMail("邮件标题", "邮件内容")


你可能感兴趣的:(python发送邮件)