1. 程式人生 > >python 簡單的傳送email方法

python 簡單的傳送email方法

# -*- coding: UTF-8 -*-
'''
傳送txt文字郵件
小五義:http://www.cnblogs.com/xiaowuyi
'''
import smtplib
from email.mime.text import MIMEText
mailto_list=["xxxx.com"]
mail_host="smtp.xxxx.com"  #設定伺服器
mail_user="xxxx"    #使用者名稱
mail_pass="xxxx"   #口令
mail_postfix="xxxx.com"  #發件箱的字尾

def send_mail(to_list,sub,content):
    me="hello"+"<"+mail_user+"@"+mail_postfix+">"
    msg = MIMEText(content,_subtype='plain',_charset='UTF-8')
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    try:
        server = smtplib.SMTP()
        server.connect(mail_host)
        server.login(mail_user,mail_pass)
        server.sendmail(me, to_list, msg.as_string())
        server.close()
        return True
    except Exception, e:
        print str(e)
        return False
if __name__ == '__main__':
    if send_mail(mailto_list,"hello","hello world!"):
        print "傳送成功"
    else:
        print "傳送失敗"