Pages

Thursday, 22 August 2019

基于python3的email工具-Zmail

Zmail makes it easier to send and retrieve emails in python3.
PyPI platform license

Introduction

Zmail makes it easier to send and retrieve emails in python3. There's no need to manually add—server address, port, suitable protocol, and so on—Zmail will do it for you. Besides, use a python dict as a mail is also more intuitive.

Installation

Zmail only running in python3 without third-party modules required. Do not support python2.
$ pip3 install zmail

Features

  • Automatic looks for server address and it's port.
  • Automatic uses 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.

Usage

Before using it, please ensure:
  • Using python3
  • Open SMTP/POP3 functions 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.

QuickStart

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)

Demos

Examples

Verify SMTP and POP function working correctly

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.

Send your mail

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)
You can define sender's name by add 'from':'Boss ' in your mail.
  • To a list of recipients
server.send_mail(['yourfriend@example.com','12345@example.com'], mail)
you can also name them (use tuple, first is its name, next is its address).
server.send_mail([('Boss','yourfriend@example.com'),'12345@example.com'], mail)
  • Send HTML content
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)
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)
  • Use carbon copy
server.send_mail(['foo@163.com','foo@126.com'],mail,cc=['bar@163.com'])
Again, you can also name them (use tuple, first is its name, next is its address).
server.send_mail(['foo@163.com','foo@126.com'],mail,cc=[('Boss','bar@163.com'),'bar@126.com'])
  • Customize your server
If zmail is not working correctly, you can customize your server config yourself.
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)

Get your mails

  • Get the latest mail.
import zmail
server = zmail.server('yourmail@example.com‘, 'yourpassword')
mail = server.get_latest()
  • Retrieve mail by its id.
mail = server.get_mail(2)
  • Get a list of mails by its (subject,after,before,sender)
mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github')
In this example, if 'GitHub' in mail's subject, it will be matched, as in '[GitHub] Your password has changed'
Sender is the same way.
You can also assign the range of mails.
mail = server.get_mails(subject='GitHub',start_time='2018-1-1',sender='github',start_index=1,end_index=10)
  • Get mailbox info.
mailbox_info = server.stat()
The result is a tuple of 2 integers: (message count, mailbox size).

Parse your mail

In zmail, each mail will be mapped to a python dictionary, you can access your mail by
subject = mail['subject']
Show your mail's base information, use zmail.show()
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')
mail = server.get_latest()
zmail.show(mail)
See all contents in mail.
import zmail
server = zmail.server('yourmail@example.com’, 'yourpassword')
mail = server.get_latest()
for k,v in mail.items():
 print(k,v)
from https://github.com/ZYunH/zmail

No comments:

Post a Comment