python傳送郵件例項 - 使用smtplib模組

pythontab發表於2013-03-21
# 匯入 smtplib 和 MIMEText 
import smtplib 
from email.mime.text import MIMEText 
   
# 定義傳送列表 
mailto_list=["root@pythontab.com","10118157@qq.com"] 
   
# 設定伺服器名稱、使用者名稱、密碼以及郵件字尾 
mail_host = "smtp.163.com"
mail_user = "xx@163.com"
mail_pass = "xx"
mail_postfix="163.com"
   
# 傳送郵件函式 
def send_mail(to_list, sub, context): 
    '''''
    to_list: 傳送給誰
    sub: 主題
    context: 內容
    send_mail("xxx@126.com","sub","context")
    '''
    me = mail_user + "<"+mail_user+"@"+mail_postfix+">"    msg = MIMEText(context)     msg['Subject'] = 'python email test'    msg['From'] = sub    msg['To'] = ";".join(to_list)     try:         send_smtp = smtplib.SMTP()         send_smtp.connect(mail_host)         send_smtp.login(mail_user, mail_pass)         send_smtp.sendmail(me, to_list, msg.as_string())         send_smtp.close()         print 'success'        return True    except (Exception, e):         print(str(e))         print 'false'  send_mail(mailto_list,"test mail","你好")


相關文章