国产探花免费观看_亚洲丰满少妇自慰呻吟_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.

好了今天就到這里。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 启东市| 泾阳县| 瑞金市| 梧州市| 黄大仙区| 崇州市| 珠海市| 隆昌县| 兰西县| 观塘区| 漯河市| 遂川县| 巴林右旗| 绥宁县| 阳江市| 苍南县| 商河县| 龙南县| 塔城市| 固镇县| 沈阳市| 承德县| 临朐县| 武定县| 永年县| 舞钢市| 弋阳县| 京山县| 揭西县| 安阳县| 汾阳市| 萝北县| 新巴尔虎左旗| 孟连| 安国市| 鹤山市| 德化县| 永春县| 诸城市| 无棣县| 锦屏县|