Package pyzmail :: Module parse :: Class PyzMessage
[hide private]
[frames] | no frames]

Class PyzMessage

source code

email.message.Message --+
                        |
                       PyzMessage
Known Subclasses:

Inherit from email.message.Message. Combine get_mail_parts(), get_mail_addresses() and decode_mail_header() into a convenient object to access mail contents and attributes. This class also sanitize part filenames.


Note: Sample:

>>> raw='''Content-Type: text/plain; charset="us-ascii"
... MIME-Version: 1.0
... Content-Transfer-Encoding: 7bit
... Subject: The subject
... From: Me <me@foo.com>
... To: A <a@foo.com>, B <b@foo.com>
...  
... The text.
... '''
>>> msg=PyzMessage.factory(raw)
>>> print 'Subject: %r' % (msg.get_subject(), )
Subject: u'The subject'
>>> print 'From: %r' % (msg.get_address('from'), )
From: (u'Me', 'me@foo.com')
>>> print 'To: %r' % (msg.get_addresses('to'), )
To: [(u'A', 'a@foo.com'), (u'B', 'b@foo.com')]
>>> print 'Cc: %r' % (msg.get_addresses('cc'), )
Cc: []
>>> for mailpart in msg.mailparts:
...   print '    %sfilename=%r sanitized_filename=%r type=%s charset=%s desc=%s size=%d' % ('*'if mailpart.is_body else ' ', mailpart.filename, mailpart.sanitized_filename, mailpart.type, mailpart.charset, mailpart.part.get('Content-Description'), 0 if mailpart.get_payload()==None else len(mailpart.get_payload()))
...   if mailpart.is_body=='text/plain':
...     payload, used_charset=decode_text(mailpart.get_payload(), mailpart.charset, None) 
...     print '        >', payload.split('\n')[0]
...
    *filename=None sanitized_filename='text.txt' type=text/plain charset=us-ascii desc=None size=10
        > The text.

Instance Methods [hide private]
 
__init__(self, message)
Initialize the object with data coming from input.
source code
tuple
get_addresses(self, name)
return the name header value as an list of addresses tuple as returned by get_mail_addresses()
source code
list of tuple
get_address(self, name)
return the name header value as an address tuple as returned by get_mail_addresses()
source code
unicode
get_subject(self, default='')
return the RFC2047 decoded subject.
source code
unicode
get_decoded_header(self, name, default='')
return decoded header name using RFC2047.
source code

Inherited from email.message.Message: __contains__, __delitem__, __getitem__, __len__, __setitem__, __str__, add_header, as_string, attach, del_param, get, get_all, get_boundary, get_charset, get_charsets, get_content_charset, get_content_maintype, get_content_subtype, get_content_type, get_default_type, get_filename, get_param, get_params, get_payload, get_unixfrom, has_key, is_multipart, items, keys, replace_header, set_boundary, set_charset, set_default_type, set_param, set_payload, set_type, set_unixfrom, values, walk

Inherited from email.message.Message (private): _get_params_preserve

Static Methods [hide private]
email.message.Message
smart_parser(input)
Use the appropriate parser and return a email.message.Message object (this is not a PyzMessage object)
source code
PyzMessage
factory(input)
Use the appropriate parser and return a PyzMessage object see smart_parser
source code
Instance Variables [hide private]
MailPart or None html_part
the MailPart object that contains the HTML version of the message, None if the mail has not HTML content.
list of MailPart mailparts
list of MailPart objects composing the email, text_part and html_part are part of this list as are other attachements and embedded contents.
MailPart or None text_part
the MailPart object that contains the text version of the message, None if the mail has not text content.
Method Details [hide private]

smart_parser(input)
Static Method

source code 

Use the appropriate parser and return a email.message.Message object (this is not a PyzMessage object)

Parameters:
  • input (string, file, bytes, binary_file or email.message.Message) - the source of the message
Returns: email.message.Message
the message

factory(input)
Static Method

source code 

Use the appropriate parser and return a PyzMessage object see smart_parser

Parameters:
  • input (string, file, bytes, binary_file or email.message.Message) - the source of the message
Returns: PyzMessage
the PyzMessage message

__init__(self, message)
(Constructor)

source code 

Initialize the object with data coming from input.

Parameters:
  • message (inherit email.message.Message) - The message
Overrides: email.message.Message.__init__

get_addresses(self, name)

source code 

return the name header value as an list of addresses tuple as returned by get_mail_addresses()

Parameters:
  • name (str) - the name of the header to read value from: 'to', 'cc' are valid name here.
Returns: tuple
a tuple of the form ('Sender Name', 'sender.address@domain.com') or ('', '') if no header match that name.

get_address(self, name)

source code 

return the name header value as an address tuple as returned by get_mail_addresses()

Parameters:
  • name (str) - the name of the header to read value from: : 'from' can be used to return the sender address.
Returns: list of tuple
a list of tuple of the form [('Recipient Name', 'recipient.address@domain.com'), ...] or an empty list if no header match that name.

get_subject(self, default='')

source code 

return the RFC2047 decoded subject.

Parameters:
  • default (any) - The value to return if the message has no Subject
Returns: unicode
the subject or default

get_decoded_header(self, name, default='')

source code 

return decoded header name using RFC2047. Always use this function to access header, because any header can contain invalid characters and this function sanitize the string and avoid unicode exception later in your program. EVEN for date, I already saw a "Center box bar horizontal" instead of a minus character.

Parameters:
  • name (str) - the name of the header to read value from.
  • default (any) - The value to return if the name field don't exist in this message.
Returns: unicode
the value of the header having that name or default if no header have that name.