python Zmail模塊簡(jiǎn)介與使用示例
Zmail 使得在python3中發(fā)送和接受郵件變得更簡(jiǎn)單。你不需要手動(dòng)添加服務(wù)器地址、端口以及適合的協(xié)議,zmail會(huì)幫你完成。此外,使用一個(gè)python字典來(lái)代表郵件內(nèi)容也更符合直覺(jué)
安裝Zmail僅支持python3,不需要任何外部依賴. 不支持python2.
pip3 install zmail特性 自動(dòng)尋找服務(wù)器地址以及端口 自動(dòng)使用可靠的鏈接協(xié)議 自動(dòng)將一個(gè)python字典映射成MIME對(duì)象(帶有附件的) 自動(dòng)添加頭文件以及l(fā)ocalhostname來(lái)避免服務(wù)器拒收你的郵件 輕松自定義你的頭文件 支持使用HTML作為郵件內(nèi)容 僅需python>=3.5,你可以將其嵌入你的項(xiàng)目而無(wú)需其他的依賴 使用須知
使用它之前,請(qǐng)保證
使用Python3 確保打開(kāi)了郵箱的POP3和SMTP功能 (對(duì)于 @163.com 和 @gmail.com 你需要設(shè)置你的應(yīng)用專用密碼)然后,剩下你需要做的就是import zmail即可
使用示例發(fā)送你的郵件import zmail# 你的郵件內(nèi)容mail_content = { 'subject':'success!', # 郵件主題 'content_text':'This message from zmail', # 郵件內(nèi)容 'attachments':r'D:test.docx', # 郵件附件}# 使用你的郵件賬戶名和密碼登錄服務(wù)器server = zmail.server('XXXXXX@163.com', 'XXXXXX')# 發(fā)送郵件server.send_mail(’yourfriend@example.com’, mail_content)
給多個(gè)信箱發(fā)件,修改發(fā)送郵件 即可,其他內(nèi)容同上
# 發(fā)送郵件server.send_mail([’555555@qq.com’,’666666@qq.com’],mail_content)
發(fā)送HTML作為郵件內(nèi)容
mail = { ’subject’: ’Success!’, # 郵件主題 ’content_html’: [’HTML CONTENT’], # HTML格式的郵件內(nèi)容 ’attachments’: ’/Users/zyh/Documents/example.zip’, # 郵件附件}server.send_mail(’yourfriend@example.com’,mail)
或者
with open(’/Users/example.html’,’r’) as f: content_html = f.read()mail = { ’subject’: ’Success!’, ’content_html’: content_html, ’attachments’: ’/Users/zyh/Documents/example.zip’, }server.send_mail(’yourfriend@example.com’,mail) 自定義你的server
如果zmail不能正常工作,你可以自定義server的配置
server = zmail.server(’username’,’password’,smtp_host=’smtp.163.com’,smtp_port=994,smtp_ssl=True,pop_host=’pop.163.com’,pop_port=995,pop_tls=True)取回你的郵件 取得最新的郵件
import zmailserver = zmail.server(’yourmail@example.com’, ’yourpassword’)mail = server.get_latest() 依據(jù)id取回郵件
mail = server.get_mail(2) 依據(jù) (subject,after,before,sender)取回一個(gè)列表的郵件
mail = server.get_mails(subject=’163’,after=’2018-1-1’,sender=’github’)
示例中, 如果 ’163’ 在郵件的主題中,這封郵件將會(huì)被匹配, 例如’ [163] Your password has changed’
郵件的結(jié)構(gòu) content-type: 郵件內(nèi)容的類型 subject: 郵件主題 to:收件人 from:寄件人 date: 年-月-日 時(shí)間 時(shí)區(qū) boundary: 如果郵件為multiple - - - parts,你可以得到其分界線 content: 郵件的文本內(nèi)容(僅在text/plain時(shí)可以被解析) contents: 郵件的body,里面包含著由分界線分割的每一個(gè)段落 attachments: None 或者 [[’附件名稱;編碼方式’,’附件的二進(jìn)制內(nèi)容’]...] id: 在郵箱中的id項(xiàng)目地址:GitHub:https://github.com/ZYunH/zmail
以上就是python Zmail模塊簡(jiǎn)介與使用示例的詳細(xì)內(nèi)容,更多關(guān)于python Zmail模塊的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP編碼必備的8條原則2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. 一文帶你搞懂JavaScript中的進(jìn)制與進(jìn)制轉(zhuǎn)換4. 匹配模式 - XSL教程 - 45. 詳解JS前端使用迭代器和生成器原理及示例6. 得到XML文檔大小的方法7. 詳解CSS偽元素的妙用單標(biāo)簽之美8. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)9. ASP基礎(chǔ)知識(shí)Command對(duì)象講解10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
