Package pyzmail
[hide private]
[frames] | no frames]

Package pyzmail

source code


Version: 1.0.0

Submodules [hide private]

Classes [hide private]
  PyzMessage
Inherit from email.message.Message.
  PzMessage
Old name and interface for PyzMessage.
Functions [hide private]
tuple
compose_mail(sender, recipients, subject, default_charset, text, html=None, attachments=[], embeddeds=[], cc=[], bcc=[], message_id_string=None, date=None, headers=[])
Compose an email regarding the arguments.
source code
dict or str
send_mail(payload, mail_from, rcpt_to, smtp_host, smtp_port=25, smtp_mode='normal', smtp_login=None, smtp_password=None)
Send the message to a SMTP host.
source code
dict
send_mail2(payload, mail_from, rcpt_to, smtp_host, smtp_port=25, smtp_mode='normal', smtp_login=None, smtp_password=None)
Send the message to a SMTP host.
source code
tuple
decode_text(payload, charset, default_charset)
Try to decode text content by trying multiple charset until success.
source code
PyzMessage
message_from_string(s, *args, **kws)
Parse a string into a PyzMessage object model.
source code
PyzMessage
message_from_file(fp, *args, **kws)
Read a file and parse its contents into a PyzMessage object model.
source code
PyzMessage
message_from_bytes(s, *args, **kws)
Parse a bytes string into a PyzMessage object model.
source code
PyzMessage
message_from_binary_file(fp, *args, **kws)
Read a binary file and parse its contents into a PyzMessage object model.
source code
Variables [hide private]
  email_address_re = re.compile(r'^(?:[a-zA-Z0-9_!#\$%&\'\*\+/=\...
  __package__ = 'pyzmail'
Function Details [hide private]

compose_mail(sender, recipients, subject, default_charset, text, html=None, attachments=[], embeddeds=[], cc=[], bcc=[], message_id_string=None, date=None, headers=[])

source code 

Compose an email regarding the arguments. Call build_mail() and complete_mail() at once.

Read the parameters descriptions of both functions build_mail() and complete_mail().

Returned value is the same as for build_mail() and complete_mail(). You can pass the returned values to send_mail() or send_mail2().

Returns: tuple
(payload, mail_from, rcpt_to, msg_id)
>>> payload, mail_from, rcpt_to, msg_id=compose_mail((u'Me', 'me@foo.com'), [(u'Him', 'him@bar.com')], u'the subject', 'iso-8859-1', ('Hello world', 'us-ascii'), attachments=[('attached', 'text', 'plain', 'text.txt', 'us-ascii')])

send_mail(payload, mail_from, rcpt_to, smtp_host, smtp_port=25, smtp_mode='normal', smtp_login=None, smtp_password=None)

source code 

Send the message to a SMTP host. Handle SSL, TLS and authentication. payload, mail_from and rcpt_to can come from values returned by complete_mail(). This function call send_mail2() but catch all exceptions and return friendly error message instead.

Parameters:
  • payload (str) - the mail content.
  • mail_from (str) - the sender address, for example: 'me@domain.com'.
  • rcpt_to (list) - The list of the recipient addresses in the form [ 'a@b.com', c@d.com', ]. No names here, only email addresses.
  • smtp_host (str) - the IP address or the name of the SMTP host.
  • smtp_port (int) - the port to connect to on the SMTP host. Default is 25.
  • smtp_mode (str) - the way to connect to the SMTP host, can be: 'normal', 'ssl' or 'tls'. default is 'normal'
  • smtp_login (str or None) - If authentication is required, this is the login. Be carefull to UTF8 encode your login if it contains non us-ascii characters.
  • smtp_password (str or None) - If authentication is required, this is the password. Be carefull to UTF8 encode your password if it contains non us-ascii characters.
Returns: dict or str
This function return a dictionary of failed recipients or a string with an error message.

If all recipients have been accepted the dictionary is empty. If the returned value is a string, none of the recipients will get the message.

The dictionary is exactly of the same sort as smtplib.SMTP.sendmail() returns with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.

Example:

>>> send_mail('Subject: hello\n\nmessage', 'a@foo.com', [ 'b@bar.com', ], 'localhost') #doctest: +SKIP
{}

Here is how to use the returned value:

   if isinstance(ret, dict):
     if ret:
       print 'failed' recipients:
       for recipient, (code, msg) in ret.iteritems():
           print 'code=%d recipient=%s     error=%s' % (code, recipient, msg)
     else:
       print 'success'
   else:
     print 'Error:', ret

To use your GMail account to send your mail:

   smtp_host='smtp.gmail.com'
   smtp_port=587
   smtp_mode='tls'
   smtp_login='your.gmail.addresse@gmail.com'
   smtp_password='your.gmail.password'

Use your GMail address for the sender !

send_mail2(payload, mail_from, rcpt_to, smtp_host, smtp_port=25, smtp_mode='normal', smtp_login=None, smtp_password=None)

source code 

Send the message to a SMTP host. Look at the send_mail() documentation. send_mail() call this function and catch all exceptions to convert them into a user friendly error message. The returned value is always a dictionary. It can be empty if all recipients have been accepted.

Returns: dict
This function return the value returnd by smtplib.SMTP.sendmail() or raise the same exceptions.

This method will return normally if the mail is accepted for at least one recipient. Otherwise it will raise an exception. That is, if this method does not raise an exception, then someone should get your mail. If this method does not raise an exception, it returns a dictionary, with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.

Raises:
  • smtplib.SMTPException - Look at the standard smtplib.SMTP.sendmail() documentation.

decode_text(payload, charset, default_charset)

source code 

Try to decode text content by trying multiple charset until success. First try charset, else try default_charset finally try popular charsets in order : ascii, utf-8, utf-16, windows-1252, cp850 If all fail then use default_charset and replace wrong characters

Parameters:
  • payload (str) - the content to decode
  • charset (str or None) - the first charset to try if != None
  • default_charset (str or None) - the second charset to try if != None
Returns: tuple
a tuple of the form (payload, charset)
  • payload: this is the decoded payload if charset is not None and payload is a unicode string
  • charset: the charset that was used to decode payload If charset is None then something goes wrong: if payload is unicode then invalid characters have been replaced and the used charset is default_charset else, if payload is still byte string then nothing has been done.

message_from_string(s, *args, **kws)

source code 

Parse a string into a PyzMessage object model.

Parameters:
  • s (str) - the input string
Returns: PyzMessage
the PyzMessage object

message_from_file(fp, *args, **kws)

source code 

Read a file and parse its contents into a PyzMessage object model.

Parameters:
  • fp (text_file) - the input file (must be open in text mode if Python >= 3.0)
Returns: PyzMessage
the PyzMessage object

message_from_bytes(s, *args, **kws)

source code 

Parse a bytes string into a PyzMessage object model. (Python >= 3.2)

Parameters:
  • s (bytes) - the input bytes string
Returns: PyzMessage
the PyzMessage object

message_from_binary_file(fp, *args, **kws)

source code 

Read a binary file and parse its contents into a PyzMessage object model. (Python >= 3.2)

Parameters:
  • fp (binary_file) - the input file, must be open in binary mode
Returns: PyzMessage
the PyzMessage object

Variables Details [hide private]

email_address_re

Value:
re.compile(r'^(?:[a-zA-Z0-9_!#\$%&\'\*\+/=\?\^`\{\}~\|-]+(?:\.[a-zA-Z0\
-9_!#\$%&\'\*\+/=\?\^`\{\}~\|-]+)*|"(?:\\[^\r\n]|[^\\"])*")@(?:[a-zA-Z\
0-9_!#\$%&\'\*\+/=\?\^`\{\}~\|-]+(?:\.[a-zA-Z0-9_!#\$%&\'\*\+/=\?\^`\{\
\}~\|-]+)*|\[(?:\\\S|[!-Z\^-~])*\])$')