CodingGo Technology Community
Free programming learning platform
\
Sending emails is a simple requirement, but there are still a lot of pitfalls in the real world, so zmail was created to make sending emails in Python as easy as possible.
GitHub:https://github.com/ZYunH/zmail
Copy the code
Disadvantages of other wheels:
Server rejection: The first problem. Many other wheels need To construct their own MIME and headers (often optimizing the process), but are often rejected because they do not define headers correctly. Headers such as From and To are checked slightly differently by each service when SMTP is used. And some platform differences (win10 localhost garble lead to send reject letter), even in your send to different address check will also have a difference (domestic send foreign need to verify some additional information to prevent spam).
Construction is too cumbersome: especially when adding attachments, some of which require additional custom types, and the experience is poor.
Sending too cumbersome: you need to find the SMTP address of your service provider, the port number, some service providers are not the default port (@163.com), some only SSL authentication (@http://qq.com), some…
Parsing mail is complicated: Bytes pit, boundary, various format garbled error
Introducing too many external packages: Some wheels introduce multiple packages, which can cause packages to conflict with the original version of the program (see packages like Requests), and bugs that don’t get timely documentation support. After I tried and failed, USING Django’s email module successfully sent emails, but that’s clearly not what Django was designed for, and using such a large framework for such a small set of requirements just doesn’t make sense (and the construction process is still complicated).
Zmail’s advantages:
- Auto-populate most headers that cause server rejects (From To LocalHost, etc.)
- Mapping a dictionary to email, constructing a letter is as simple as constructing a dictionary
- Automatically find the mail service provider port number and address, automatically select the appropriate protocol (authenticated)
- Rely only on python3 and embed other projects without worry
You can use Zmail to:
- As a monitoring script send and receive mail module, timely transmission of information
- Embed mail functionality in existing projects
- Customize the mail sending process, such as on-time delivery, scheduled delivery, and automatic monitoring
The installation
$ pip3 install zmail
Copy the code
PIP can also be used instead of PIP3
Note: Zmail only supports python3, but not python2. Before using zmail, all mailboxes must be enabled with SMTP function. Email addresses @163.com and @http://gmail.com need to set an additional SMTP password.
Send E-mail
Import zmail # mail_content = {'subject': 'Success! ', # fill in 'Content ': 'This message from zmail! Server = zmail.server('[email protected], ') 'your QQ email password ') # send mail server.send_mail('[email protected]', mail)Copy the code
- Add attachments to your email, modify the content of your email, and do the same for the rest
# mail_content = {'subject': 'Success! ', # fill in 'Content ': 'This message from zmail! '# literally fill in' attachments' : '/ Users/zyh/Documents/example. Zip', # it is best to use absolute paths, if your computer doesn't have this file can cause errors}Copy the code
- To send mail to multiple mailboxes, modify to send mail, other content is the same as above
Send_mail (['[email protected]','[email protected]'], mail)Copy the code
- Add additional header information directly to mail_content
Retrieve your mail
- Get the latest mail
import zmail
server = zmail.server('[email protected], 'yourpassword')
mail = server.get_latest()
Copy the code
- Retrieve the message by ID
mail = server.get_mail(2)
Copy the code
- Basis (subject, after, before, sender) to retrieve a list of the mail
mail = server.get_mails(subject='GitHub',after='2018-1-1',sender='github')
Copy the code
In the example, if ‘GitHub’ is in the subject of the message, the message will be matched, for example ‘[GitHub] Your password has changed’
Same thing for sender
- Get header information for all messages. A list of dictionaries, each containing all the header files that can be extracted.
mail_info = server.get_info()
Copy the code
- Get email information
mailbox_info = server.stat()
Copy the code
The result is a tuple of two integers: (number of messages, size of mailbox).
Parse your email
In ZMail, incoming messages are mapped to a dictionary, and you can access your messages in the form of a Python dictionary. All keys in the dictionary are listed in the message structure below, for example
subject = mail['subject']
Copy the code
To display your mail, use zmail.show()
import zmail
server = zmail.server('[email protected], 'yourpassword')
mail = server.get_latest()
zmail.show(mail)
Copy the code
Output:
content-type multipart/mixed subject Success! to zmail_user from zmail<[email protected]> date 2018-2-3 01:42:29 +0800 boundary ===============9196441298519098157== content ['This message from zmail!'] contents [[b'Content-Type: text/plain; charset="utf-8"', b'MIME-Version: 1.0 ', b 'Content - Transfer - Encoding: base64', b ', 'b' VGhpcyBtZXNzYWdlIGZyb20gem1haWwh ', 'b']] attachments None id 5Copy the code
Structure of an email
- Content-type: indicates the type of the email content
- Subject: indicates the email subject
- To: recipient
- From: sender
- Date: year-month-day Time zone
- Boundary: If the mail is multiple — parts, you can get the boundary
- Content: The text content of the message (parsed only in text/plain)
- Contents: The body of the message, which contains each paragraph separated by the dividing line
- Attachments: None or [[‘ attachments; encoding mode ‘,’ binary attachments ‘]…]
- Id: indicates the ID in the mailbox
Get the attachment
import zmail
server = zmail.server('[email protected], 'yourpassword')
mail = server.get_latest()
zmail.get_attachment(mail)
Copy the code
You can rename your attachments to use
zmail.get_attachment(mail,'example.zip')
Copy the code
The mail service providers in the list of supported mail service providers have been tested to work properly
If your email is not listed, go to Github to submit an issue.
In this paper, the author
❈
ZYunH, the best or nothing
Blog: zhihu.com/people/zhang-yun-hao-14
❈
Click below to read the original article and join the programming learning community