Python群發(fā)郵件實(shí)例代碼
直接上代碼了
import smtplib
msg = MIMEMultipart()
#構(gòu)造附件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)
#構(gòu)造附件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'] = '郵件主題'
#發(fā)送郵件
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 '發(fā)送成功'
except Exception, e:
print str(e)
細(xì)心的讀者會(huì)發(fā)現(xiàn)代碼中有這樣一句:msg['to']=','.join(strTo),但是msg[['to']并沒有在后面被使用,這么寫明顯是不合理的,但是這就是stmplib的bug。你只有這樣寫才能群發(fā)郵件。查明原因如下:
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.
好了今天就到這里。
- 詳解python定時(shí)簡(jiǎn)單爬取網(wǎng)頁新聞存入數(shù)據(jù)庫并發(fā)送郵件
- python基于exchange函數(shù)發(fā)送郵件過程詳解
- 如何使用Python自動(dòng)生成報(bào)表并以郵件發(fā)送
- Python SMTP發(fā)送電子郵件的示例
- python 利用zmail庫發(fā)送郵件
- Python發(fā)送郵件實(shí)現(xiàn)基礎(chǔ)解析
- python 七種郵件內(nèi)容發(fā)送方法實(shí)例
- python發(fā)送郵件的實(shí)例代碼(支持html、圖片、附件)
- python中使用smtplib和email模塊發(fā)送郵件實(shí)例
- 詳細(xì)講解用Python發(fā)送SMTP郵件的教程
- Python實(shí)現(xiàn)給qq郵箱發(fā)送郵件的方法
- python 發(fā)送郵件的四種方法匯總
相關(guān)文章
TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家詳細(xì)介紹了TensorFlow實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Python獲取服務(wù)器信息的最簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Python獲取服務(wù)器信息的最簡(jiǎn)單實(shí)現(xiàn)方法,涉及Python中urllib2庫的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
基于Google的Python編碼規(guī)范標(biāo)準(zhǔn)
這篇文章主要介紹了基于Google的Python編碼規(guī)范標(biāo)準(zhǔn),其中包含了分號(hào),行長(zhǎng)度,括號(hào),縮進(jìn),空行,空格等基本符號(hào)的使用規(guī)則,有需要的朋友可以參考下2021-08-08
Python 循環(huán)函數(shù)詳細(xì)介紹
循環(huán)用于重復(fù)執(zhí)行一些程序塊。從上一講的選擇結(jié)構(gòu),我們已經(jīng)看到了如何用縮進(jìn)來表示程序塊的隸屬關(guān)系。循環(huán)也會(huì)用到類似的寫法。感興趣得小伙伴請(qǐng)參考下面文字得具體內(nèi)容2021-09-09
Python的Django框架中使用SQLAlchemy操作數(shù)據(jù)庫的教程
SQLAlchemy是Python一個(gè)專門的數(shù)據(jù)庫管理工具,如果對(duì)Django ORM覺得有些生疏的話完全可以結(jié)合SQLAlchemy,這里我們就來總結(jié)一下Python的Django框架中使用SQLAlchemy操作數(shù)據(jù)庫的教程2016-06-06
Python數(shù)據(jù)庫反向生成Model最優(yōu)方案示例
這篇文章主要介紹了Python數(shù)據(jù)庫反向生成Model最優(yōu)方案的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Python實(shí)現(xiàn)Restful API的例子
今天小編就為大家分享一篇Python實(shí)現(xiàn)Restful API的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08

