国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python群發郵件實例代碼

2020-02-23 05:04:42
字體:
來源:轉載
供稿:網友

直接上代碼了

代碼如下:
import smtplib
msg = MIMEMultipart()

#構造附件1
att1 = MIMEText(open('/home/a2bgeek/develop/python/hello.py', 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="hello.txt"'#這里的filename可以任意寫,寫什么名字,郵件中顯示什么名字
msg.attach(att1)

#構造附件2
#att2 = MIMEText(open('/home/a2bgeek/develop/python/mail.py', 'rb').read(), 'base64', 'gb2312')
#att2["Content-Type"] = 'application/octet-stream'
#att2["Content-Disposition"] = 'attachment; filename="123.txt"'
#msg.attach(att2)

#加郵件頭
strTo = ['XXX1@139.com', 'XXX2@163.com', 'XXX3@126.com']
msg['to']=','.join(strTo)
msg['from'] = 'YYY@163.com'
msg['subject'] = '郵件主題'
#發送郵件
try:
    server = smtplib.SMTP()
    server.connect('smtp.163.com')
    server.login('YYY@163.com','yourpasswd')
    server.sendmail(msg['from'], strTo ,msg.as_string())
    server.quit()
    print '發送成功'
except Exception, e:
    print str(e)

細心的讀者會發現代碼中有這樣一句:msg['to']=','.join(strTo),但是msg[['to']并沒有在后面被使用,這么寫明顯是不合理的,但是這就是stmplib的bug。你只有這樣寫才能群發郵件。查明原因如下:

The problem is that SMTP.sendmail and email.MIMEText need two different things.

email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)

SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.

So, what you need to do is COMBINE the two replies you received. Set msg‘To' to a single string, but pass the raw list to sendmail.

好了今天就到這里。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘泉县| 台南县| 吴桥县| 中牟县| 金山区| 辽中县| 宁安市| 清涧县| 安图县| 山东省| 萨嘎县| 保定市| 遵化市| 南宫市| 新密市| 宝应县| 车致| 浠水县| 乾安县| 富川| 沁源县| 剑河县| 咸阳市| 双城市| 临安市| 凤山县| 施秉县| 仲巴县| 武冈市| 康保县| 开远市| 神农架林区| 界首市| 通州市| 乌苏市| 宜都市| 新乡市| 项城市| 炉霍县| 潮州市| 始兴县|