Postbox
It makes sending mail easier. The main features:
- It allows you specify message headers by keyword arguments.
- Support interactive prompt for username or password.
- Support optional with statement.
Examples
It is an example which sends a mail from Python shell.
>>> from postbox import Postbox, Gmail >>> gmail = Postbox(host='smtp.google.com:587') # or gmail = Gmail() username? mosky.bot@gmail.com password? >>> gmail.send( ... to = ['mosky.tw@gmail.com', 'mosky.liu@pinkoi.com'], ... bcc = 'mosky@ubuntu-tw.org', ... subject = 'Test from Python Shell', ... body = 'It is sent by postbox. :)' ... ) ... >>> gmail.close() >>>
You can find more examples here.
Usage
Postbox
The Postbox (or Gmail) accepts the following keyword arguments:
-
host: the hostname of your SMTP server. ex. 'smtp.google.com' or 'smtp.google.com:587' -
port: the port number of your SMTP server. -
user: the username. -
password: the password. -
tls: use tls or not. -
prompt_user: prompt string if you don't specifyuser. -
prompt_password: prompt string if you don't specifypassword. -
debuglevel: the debuglevel. -
dry_run: don't send the mail out.
postbox.send
The all keyword arguments to send will be translated into message headers,
except the body is the body of this mail. The common headers list:
-
to: It is also used as the to_addrs, so you must to specify it. -
from_: It is also used as the from_addr. If you don't specify it, it takes theuserfrom Postbox instance as default. subjectccbccreply_to
If a value is iterable but not a string, it will be joined into a string by comma.