Python群發(fā)郵件實例代碼
直接上代碼了
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ì)心的讀者會發(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定時簡單爬取網(wǎng)頁新聞存入數(shù)據(jù)庫并發(fā)送郵件
- python基于exchange函數(shù)發(fā)送郵件過程詳解
- 如何使用Python自動生成報表并以郵件發(fā)送
- Python SMTP發(fā)送電子郵件的示例
- python 利用zmail庫發(fā)送郵件
- Python發(fā)送郵件實現(xiàn)基礎(chǔ)解析
- python 七種郵件內(nèi)容發(fā)送方法實例
- python發(fā)送郵件的實例代碼(支持html、圖片、附件)
- python中使用smtplib和email模塊發(fā)送郵件實例
- 詳細(xì)講解用Python發(fā)送SMTP郵件的教程
- Python實現(xiàn)給qq郵箱發(fā)送郵件的方法
- python 發(fā)送郵件的四種方法匯總
相關(guān)文章
解決Jupyter Notebook開始菜單欄Anaconda下消失的問題
這篇文章主要介紹了解決Jupyter Notebook開始菜單欄Anaconda下消失的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04快速進(jìn)修Python指南之迭代器Iterator與生成器
這篇文章主要為大家介紹了Java開發(fā)者快速進(jìn)修Python指南之迭代器Iterator與生成器示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12python+opencv+caffe+攝像頭做目標(biāo)檢測的實例代碼
今天小編就為大家分享一篇python+opencv+caffe+攝像頭做目標(biāo)檢測的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼
這篇文章主要介紹了使用python-cv2實現(xiàn)視頻的分解與合成的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常
這篇文章主要為大家介紹了一個關(guān)于pyinstaller的?pathex?參數(shù)所引發(fā)的打包執(zhí)行報no?module?name的異常錯誤解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python監(jiān)控日志中的報錯并進(jìn)行郵件報警
這篇文章主要為大家介紹了python監(jiān)控日志中的報錯并進(jìn)行郵件報警的實現(xiàn)實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06