python Zmail模塊簡(jiǎn)介與使用示例
Zmail 使得在python3中發(fā)送和接受郵件變得更簡(jiǎn)單。你不需要手動(dòng)添加服務(wù)器地址、端口以及適合的協(xié)議,zmail會(huì)幫你完成。此外,使用一個(gè)python字典來代表郵件內(nèi)容也更符合直覺
安裝Zmail僅支持python3,不需要任何外部依賴. 不支持python2.
pip3 install zmail特性 自動(dòng)尋找服務(wù)器地址以及端口 自動(dòng)使用可靠的鏈接協(xié)議 自動(dòng)將一個(gè)python字典映射成MIME對(duì)象(帶有附件的) 自動(dòng)添加頭文件以及l(fā)ocalhostname來避免服務(wù)器拒收你的郵件 輕松自定義你的頭文件 支持使用HTML作為郵件內(nèi)容 僅需python>=3.5,你可以將其嵌入你的項(xiàng)目而無需其他的依賴 使用須知
使用它之前,請(qǐng)保證
使用Python3 確保打開了郵箱的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('[email protected]', 'XXXXXX')# 發(fā)送郵件server.send_mail(’[email protected]’, mail_content)
給多個(gè)信箱發(fā)件,修改發(fā)送郵件 即可,其他內(nèi)容同上
# 發(fā)送郵件server.send_mail([’[email protected]’,’[email protected]’],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(’[email protected]’,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(’[email protected]’,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(’[email protected]’, ’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. IntelliJ IDEA刪除類的方法步驟2. JSP中Servlet的Request與Response的用法與區(qū)別3. Struts2獲取參數(shù)的三種方法總結(jié)4. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式5. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程6. IntelliJ IDEA導(dǎo)入jar包的方法7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. vue cli4下環(huán)境變量和模式示例詳解9. Django視圖類型總結(jié)10. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
