SoFunction
Updated on 2024-11-17

Introduction to the python Zmail module and examples of its use

present (sb for a job etc)

Zmail makes it easier to send and receive mail in python3. You don't need to manually add server addresses, ports, and appropriate protocols, zmail does it for you. It's also more intuitive to use a python dictionary to represent the content of the message

mounting

Zmail only supports python3 and does not require any external dependencies. Python2 is not supported.

pip3 install zmail

characterization

  • Automatically find server addresses and ports
  • Automatic use of reliable linking protocols
  • Automatically map a python dictionary to a MIME object (with attachments)
  • Automatically add headers and localhostname to avoid the server rejecting your emails.
  • Easily customize your headers
  • Supports the use of HTML as email content
  • Only python>=3.5 is needed, you can embed it in your project without any other dependencies!

Instructions for use

Before using it, make sure that the

  • Using Python3
  • Make sure POP3 and SMTP are turned on for your mailbox (for @ and @ you need to set your app-specific password)

Then, all you have to do is import zmail.

usage example

Send your email

import zmail

# The content of your mail
mail_content = {
 "subject":"success!", # Subject of the e-mail
 "content_text":"This message from zmail", # Contents of the e-mail
 "attachments":r"D:\", # Mail attachments
}

# Log in to the server with your mail account name and password
server = ("XXXXXX@", "XXXXXX")

# Send mail
server.send_mail('yourfriend@', mail_content)

To send mail to multiple mailboxes, modify the send mail that is, other content as above

# Send mail

server.send_mail(['555555@','666666@'],mail_content)

Send HTML as email content

mail = {
 'subject': 'Success!', # Subject of the e-mail
 'content_html': ['HTML CONTENT'], # HTML formatted email content
 'attachments': '/Users/zyh/Documents/', # Mail attachments
}
server.send_mail('yourfriend@',mail)

or

with open('/Users/','r') as f:
 content_html = ()
mail = {
 'subject': 'Success!', 
 'content_html': content_html, 
 'attachments': '/Users/zyh/Documents/', 
}
server.send_mail('yourfriend@',mail)
  • Customize your server

If zmail doesn't work properly, you can customize the server's configuration

server = ('username','password',smtp_host='smtp.',smtp_port=994,smtp_ssl=True,pop_host='pop.',pop_port=995,pop_tls=True)

Retrieve your mail

  • Get email updates
import zmail
server = ('yourmail@', 'yourpassword')
mail = server.get_latest()
  • Retrieve mail based on id
mail = server.get_mail(2)
  • Based on (subject,after,before,sender) to retrieve a list of messages.
mail = server.get_mails(subject='163',after='2018-1-1',sender='github')

In the example, if '163' is in the subject of the email, the email will be matched, e.g. ' [163] Your password has changed'

Structure of the mail

  • content-type: the type of the message content
  • subject: Subject of the message
  • to: recipient
  • from: Sender
  • date: year-month-day time time zone
  • boundary: If the message is multiple ---- parts, you can get the boundary of the message.
  • content: text content of the message (can be parsed only in text/plain)
  • contents: the body of the message, which contains each paragraph separated by a line.
  • attachments: None or [['Attachment name; encoding','Binary content of attachment']...]
  • id: id in the mailbox

Project Address:

GitHub:/ZYunH/zmail 

Above is the introduction of python Zmail module and the use of sample details, more information about python Zmail module please pay attention to my other related articles!