python筆記37-史上最好用的發郵件zmail


簡介

python發郵件之前用的是smtplib,代碼太過於復雜,學習成本大,並且很多人學不會。之前專門寫過一篇https://www.cnblogs.com/yoyoketang/p/7277259.html,無奈還是一大堆人發送郵件失敗。
今天介紹一個最簡單,最強大的發郵件的包zmail,簡單好上手,媽媽再也不用擔心我不會發郵件了!
github原文地址https://github.com/ZYunH/zmail

zmail簡介

Zmail允許您在python中盡可能發送和接收電子郵件。無需檢查服務器地址或制作您自己的MIME對象。使用zmail,您只需要關心您的郵件內容。
Zmail只在python3中運行,不需要第三方模塊。不支持python2

pip3 install zmail

特征:

  • 自動查找服務器地址及其端口。
  • 自動使用合適的協議登錄。
  • 自動將python字典轉換為MIME對象(帶附件)。
  • 自動添加郵件標題和本地名稱,以避免服務器拒絕您的郵件。
  • 輕松自定義郵件標題。
  • 支持HTML作為郵件內容。
  • 只需要python> = 3.5,您可以將其嵌入到項目中而無需其他模塊。

在使用之前,請確保:

  • 使用python3
  • 在您的郵件中打開SMTP / POP3功能(對於@ 163.com和@ gmail.com,您需要設置您的應用程序私人密碼)
    然后,您只需要導入zmail即可

快速開始

import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')

# Send mail
server.send_mail('yourfriend@example.com',{'subject':'Hello!','content_text':'By zmail.'})
# Or to a list of friends.
server.send_mail(['friend1@example.com','friend2@example.com'],{'subject':'Hello!','content_text':'By zmail.'})

# Retrieve mail
latest_mail = server.get_latest()
zmail.show(latest_mail)

案例

驗證SMTP和POP功能是否正常工作

import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')

if server.smtp_able():
    pass
    # SMTP function.
if server.pop_able():
    pass
    # POP function.

如果SMTP和POP工作正常,該函數將返回True,否則返回Fasle。

發送郵件

import zmail
mail = {
    'subject': 'Success!',  # Anything you want.
    'content_text': 'This message from zmail!',  # Anything you want.
    'attachments': ['/Users/zyh/Documents/example.zip','/root/1.jpg'],  # Absolute path will be better.
}

server = zmail.server('yourmail@example.com', 'yourpassword')

server.send_mail('yourfriend@example.com', mail)

您可以通過添加 'from':'Boss <mymail@foo.com>' 郵件來定義發件人的姓名。

收件人列表

server.send_mail([ ' yourfriend@example.com ',' 12345 @ example.com ' ],mail)

你也可以命名它們(使用元組,首先是它的名字,下一個是它的地址)

server.send_mail([('Boss','yourfriend@example.com'),'12345@example.com'], mail)

發送HTML內容

mail = {
    'subject': 'Success!',  # Anything you want.
    'content_html': ['HTML CONTENT'], 
    'attachments': '/Users/zyh/Documents/example.zip',  # Absolute path will be better.
}
server.send_mail('yourfriend@example.com',mail)

或者

with open('/Users/example.html','r') as f:
    content_html = f.read()
mail = {
    'subject': 'Success!',  # Anything you want.
    'content_html': content_html, 
    'attachments': '/Users/zyh/Documents/example.zip',  # Absolute path will be better.
}
server.send_mail('yourfriend@example.com',mail)

使用抄送

server.send_mail(['foo@163.com','foo@126.com'],mail,cc=['bar@163.com'])

同樣,你也可以命名它們(使用元組,首先是它的名字,下一個是它的地址)

server.send_mail(['foo@163.com','foo@126.com'],mail,cc=[('Boss','bar@163.com'),'bar@126.com'])

自定義您的服務器

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 zmail
server = zmail.server('yourmail@example.com‘, 'yourpassword')
mail = server.get_latest()

通過其ID檢索郵件。

mail = server.get_mail(2)

獲取郵件列表(主題,之后,之前,發件人)

mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github')

在示例中,如果'GitHub'在郵件的主題中,它將被匹配,例如'[GitHub]您的密碼已更改'

發件人是一樣的。

您還可以指定郵件范圍。

mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github',start_index=1,end_index=10)

獲取郵箱信息。

mailbox_info = server.stat()

結果是2個整數的元組:(message count, mailbox size)。

解析你的郵件

在zmail中,所有郵件都將映射到python字典,您可以通過訪問您的郵件

subject = mail['subject']

顯示郵件,使用zmail.show()

import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')
mail = server.get_latest()
zmail.show(mail)

查看郵件中的所有內容。

import zmail
server = zmail.server('yourmail@example.com', 'yourpassword')
mail = server.get_latest()
for k,v in mail.items():
	print(k,v)

github原文地址https://github.com/ZYunH/zmail


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM