Zmail allows you to send and get emails as possible as it can be in python.There is no need to check server address or make your own MIME object.With zmail, you only need to care about your mail content.
Zmail only running in python3 without third-party modules required. Do not support python2.
$ pip3 install zmail
or
$ pip install zmail
If that means your pip is also working for python3.
You can download the master branch of zmail,unzip it ,and do
$ python3 setup.py install
- Automatic looks for server address and it's port.
- Automatic use suitable protocol to login.
- Automatic converts a python dictionary to MIME object(with attachments).
- Automatic add mail header and local name to avoid server reject your mail.
- Easily custom your mail header.
- Support HTML as mail content.
- Only require python >= 3.5 , you can embed it in your project without other module required.
Before using it, please ensure:
- Using python3
- Open SMTP/POP3 function in your mail (For @163.com and @gmail.com you need to set your app private password)
Then, all you need to do is just import zmail.
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')
if server.smtp_able():
pass
# SMTP function.
if server.pop_able():
pass
# POP function.
If SMTP and POP are working correctly,the function will return True,else return Fasle.
import zmail
mail = {
'subject': 'Success!', # Anything you want.
'content': 'This message from zmail!', # Anything you want.
'attachments': '/Users/zyh/Documents/example.zip', # Absolute path will be better.
}
server = zmail.server('yourmail@example.com‘, 'yourpassword')
server.send_mail('yourfriend@example.com', mail)
server.send_mail(['yourfriend@example.com','12345@example.com'], mail)
mail = {
'subject': 'Success!', # Anything you want.
'content_html': zmail.get_html('/Users/example.html'), # Absolute path will be better.
'attachments': '/Users/zyh/Documents/example.zip', # Absolute path will be better.
}
server.send_mail('yourfriend@example.com',mail)
OR
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)
import zmail
server = zmail.server('yourmail@example.com‘, 'yourpassword')
mail = server.get_latest()
mail = server.get_mail(2)
mail = server.get_mails(subject='GitHub',after='2018-1-1',sender='github')
In the example, if 'GitHub' is in mail's subject, it will be matched, such as ' [GitHub] Your password has changed'
sender is the same way.
mail_info = server.get_info()
mailbox_info = server.stat()
The result is a tuple of 2 integers: (message count, mailbox size)
.
In zmail, all mails will be mapped to a python dictionary, you can access your mail by
subject = mail['subject']
Show you mail, use zmail.show()
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')
mail = server.get_latest()
zmail.show(mail)
Output, example :
content-type multipart/mixed
subject Success!
to zmail_user
from zmail<zmail@126.com>
date 2018-2-3 01:42:29 +0800
boundary ===============9196441298519098157==
content ['This message from zmail!']
content_html ['<HTML EXAMPLE>']
raw [[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 5
- content-type: Mail content type
- subject: Mail subject
- to
- from
- date: year-month-day time TimeZone
- boundary: If mail is multiple parts, you can get the boundary
- content: Mail content as text/plain
- content_html: Mail content as text/html
- raw: raw mail as bytes
- attachments: None or [['attachment-name;Encoding','ATTACHMENT-DATA']...]
- id: Mailbox id
import zmail
server = zmail.server('yourmail@example.com‘, 'yourpassword')
mail = server.get_latest()
zmail.get_attachment(mail)
you can rename your attachment file, by
zmail.get_attachment(mail,'example.zip')
import zmail
server = zmail.server('yourmail@example.com‘, 'yourpassword')
mail = server.get_latest()
zmail.save_eml(mail)
you can rename your mail or define the path, by
zmail.save_eml(mail,name='hello.eml',path='/usr/home')
import zmail
mail_as_raw = zmail.read_eml('/usr/home/hello.eml') # Abspath will be better
you can convert the raw mail to zmail format
mail = zmail.decode(mail_as_raw)
The mail server in this list has been tested and approved.
If your mail server not in it, don't worry, zmail will handle it automatically.If there any problems in use, pls tell me in the Github.
Server address | Send mail | Retrieve mail | Remarks |
---|---|---|---|
@163.com | ✓ | ✓ | Need app private password |
@qq.com | ✓ | ✓ | POP3 need app private password |
@126.com | ✓ | ✓ | |
@yeah.net | ✓ | ✓ | |
@gmail.com | ✓ | ✓ | Need app private password |
@sina.com | ✓ | ✓ | |
@outlook | ✓ | ✓ |
- Can not send or retrieve
- ensure your smtp&pop3 function is open
- according to smtp or pop protocol provided by your mail server to define zmail.server
- SMTP:server = zmail.server('user','psw',smtp_host = 'xxx',smtp_port = 'yyyyy',smtp_ssl=True)
- POP3:server = zmail.server('user','psw',pop_host = 'xxx',pop_port = 'yyyyy',pop_ssl=True)
server = zmail.server('user@example','password')
-
server.smtp_able()
-
server.send_mail([recipient,], mail)
-
server.pop_able()
-
server.get_mail(which)
-
server.get_mails(subject, sender, after, before)
-
server.get_latest()
-
server.get_info()
-
server.stat()
- server.get_attachment(mail)
- subject
- content
- content_html
- from
- to
- zmail.show()