Package turbomail :: Module message :: Class Message
[hide private]
[frames] | no frames]

Class Message

source code

object --+
         |
        Message
Known Subclasses:
KIDMessage

Simple e-mail message class.

Message provides a means to easily create e-mail messages to be sent through the Dispatch mechanism or MailPool. Message provides various helper functions to correctly format plain text, dual plain text and rich text MIME encoded messages, as well as handle embedded and external attachments.

All properties can be set from the constructor.

Example usage:
       import turbomail
       message = turbomail.Message(
                       "from@host.com",
                       "to@host.com",
                       "Subject",
                       plain="This is a plain message."
               )
E-mail addresses can be represented as any of the following: Encoding can be overridden on a per-message basis, but note that 'utf-8-qp' modifies the default 'utf-8' behaviour to output quoted-printable, and you will have to change it back yourself if you want base64 encoding.

Instance Methods [hide private]
 
__init__(self, sender=None, recipient=None, subject=None, **kw)
Instantiate a new Message object.
source code
 
attach(self, file, name=None)
Attach an on-disk file to this message.
source code
 
embed(self, file, name)
Attach an on-disk image file and prepare for HTML embedding.
source code
 
_normalize(self, addresslist)
A utility function to return a list of addresses as a string.
source code
 
_process(self)
Produce the final MIME message.
source code
 
__setattr__(self, name, value)
Set the dirty flag as properties are updated.
source code
tuple
__call__(self)
Produce a valid MIME-encoded message and return valid input for the Dispatch class to process.
source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __str__

Instance Variables [hide private]
bool _dirty
Has there been changes since the MIME message was last generated?
bool _processed
Has the MIME-encoded message been generated?
list attachments
A list of MIME-encoded attachments.
  bcc
The BCC header.
  cc
The CC header.
string date
The Date header.
  disposition
The Disposition-Notification-To header.
list embedded
A list of MIME-encoded embedded obejects for use in the text/html part.
string encoding
Content encoding.
  headers
A list of additional headers.
string organization
The Organization header.
string plain
The plain text content of the message.
int priority
The X-Priority header, a number ranging from 1-5.
  recipient
The To header.
  replyto
The X-Reply-To header.
string rich
The rich text (HTML) content of the message.
  sender
The From header.
  smtpfrom
The envelope address, if different than the sender.
string subject
The Subject header.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, sender=None, recipient=None, subject=None, **kw)
(Constructor)

source code 

Instantiate a new Message object.

No arguments are required, as everything can be set using class properties. Alternatively, everything can be set using the constructor, using named arguments. The first three positional arguments can be used to quickly prepare a simple message.

An instance of Message is callable.
Parameters:
  • sender (string) - The e-mail address of the sender. This is encoded as the "From:" SMTP header.
  • recipient (string) - The recipient of the message. This gets encoded as the "To:" SMTP header.
  • subject (string) - The subject of the message. This gets encoded as the "Subject:" SMTP header.
Overrides: object.__init__

attach(self, file, name=None)

source code 
Attach an on-disk file to this message.
Parameters:
  • file - The path to the file you wish to attach, or an instance of a file-like object.
  • name (string) - You can optionally override the filename of the attached file. This name will appear in the recipient's mail viewer. Optional if passing an on-disk path. Required if passing a file-like object.

embed(self, file, name)

source code 

Attach an on-disk image file and prepare for HTML embedding.

This method should only be used to embed images.
Parameters:
  • file - The path to the file you wish to attach, or an instance of a file-like object.
  • name (string) - You can optionally override the filename of the attached file. This name will appear in the recipient's mail viewer. Optional if passing an on-disk path. Required if passing a file-like object.

_process(self)

source code 

Produce the final MIME message.

Additinoally, if only a rich text part exits, strip the HTML to produce the plain text part. (This produces identical output as KID, although lacks reverse entity conversion -- &, etc.)

__setattr__(self, name, value)

source code 
Set the dirty flag as properties are updated.
Overrides: object.__setattr__

__call__(self)
(Call operator)

source code 
Produce a valid MIME-encoded message and return valid input for the Dispatch class to process.
Returns: tuple
Returns a tuple containing sender and recipient e-mail addresses and the string output of MIMEMultipart.

Instance Variable Details [hide private]

bcc

The BCC header. As per the recipient property. Optional.

cc

The CC header. As per the recipient property. Optional.

date

The Date header. Must be correctly formatted.
Type:
string

disposition

The Disposition-Notification-To header. A string or 2-tuple. Optional.

encoding

Content encoding. Pulled from mail.encoding, defaults to 'us-ascii'.
Type:
string

headers

A list of additional headers. Can be added in a wide variety of formats: a list of strings, list of tuples, a dictionary, etc. Look at the code.

organization

The Organization header. Optional.
Type:
string

priority

The X-Priority header, a number ranging from 1-5. Optional. Default: 3
Type:
int

recipient

The To header. A string, 2-tuple, or list of strings or 2-tuples.

replyto

The X-Reply-To header. A string or 2-tuple. Optional.

rich

The rich text (HTML) content of the message. Plain text content must be available as well.
Type:
string

sender

The From header. A string or 2-tuple.