Mailer Class

Supports sending automated emails to a list of recipients. The message body is formed according to a template, which allows to insert fields, which are automatically replaced by individual text for each recipient.

Example

A message may look like this:

Hi ${first_name}!

You receive this reminder email, because your are registred at the URI
http://www.example.com with the following credentials:

  Username: ${username}
  Password: ${password()}

Regards,
The Webmaster

When invoking __call__ with this string, then each recipient object passed as argument shall provide the members first_name, last_name and username. Additionally a dictionary with key ‘password‘ and an unary functor as value shall be passed as the functors argument. The functor will be executed with each recipient as argument and the result will replace the ${password()} field.

Interface

class pysk.utils.Mailer(host=None, user=None, password=None, logStream=<open file '<stderr>', mode 'w'>, verbose=1)

Write automated emails to a set of addresses

Parameters:
  • host (str) – IP or hostname of SMTP mail server. If None, no login is attempted. Defaults to None.
  • user (str) – Username for SMTP server. If None, no login is attempted. Defaults to None.
  • password (str) – Password for login to SMTP server. Defaults to None.
  • logStream (stream) – Stream object used for messages. Defaults to stderr
  • verbose (int) – Verbose mode setting. Defaults to 1.
__call__(recipients, message=None, subject='', sender=None, email=<pysk.utils.mailer.MessageElement object>)

Send messages to a number of recipients.

Parameters:
  • recipients (iterable) – Iterable of recipient objects. Each object must contain the fields specified in message.
  • message (str) – Message to send. If not specified, the message set via the constructor or a call to setMessage is used.
  • subject (str) – Subject used for all messages. Defaults to “”.
  • sender (str) – Sender’s email address. Defaults to None.
  • email (functor) –

    Functor which returns the email from each recipient object. Defaults to

    MessageElement("email", type="attribute")
    

    implying that each recipient must provide an attribute email.

Returns:

Dictionary containing Error messages

connect(hostname, username, password=None)

Connect to SMTP server

Parameters:
  • hostname (str) – Hostname of SMTP server
  • username (str) – User name for SMTP server
  • password (str) – Password used for login
getMessage(obj)

Create message for a given object

Parameters:obj (object) – Object from which to create fields. Must contain one attribute for each specified field. Attribute names and respective field names must be identical.
Returns:Message with fields replaced from data object
log(msg, verbose=0)

Print log message to logStream

Parameters:
  • msg (str) – Message to print
  • verbose (int) – Verbose mode. Message is not printed, if this value is greater than self.verbose. Defaults to 0.
msgGenerator(obj)

Iterate over all message elements

Parameters:obj (object) – Object from which to extract missing fields
setMessage(msg, functors=None)

Sets the message to send

Parameters:
  • msg (str) – String containing message. Fields to be replaced from other objects shall be marked as ${name}, where name is an attribute to be provided by each recipient object.
  • functors (dict) – Dictionary with functors. If this is provided, a field of the form ${name()} will be replaced by functors[name], which should be a unary function accepting a recipient object as parameter. Defaults to None.
Raise:
KeyError if functor fields are specified in message string, for which no matching key exists in functors.

Message Element Interface

Internally, each message is composed of several MessageElement instances, each of which represents either a literal string or an attribute to be retrieved from each recipient or an unary functor to be invoked with a recpient as argument.

class pysk.utils.mailer.MessageElement(value='', type='string')

A message consists of strings and fields.

The MessageElement represents both string and field elements

Parameters:
  • value – The message element value. Either a string, which will be returned verbatim, or an attribute, which will be extracted from an object during runtime.
  • type (str) –

    Type of the message element. Allowed values are those listed in TYPES:

    • attribute‘: attribute to be retrieved from recipient objects
    • functor‘: unary functor called with recipient as argument
    • string‘: a literal string.

    Defaults to ‘string‘.

__call__(obj=None)

Method called when message is constructed.

Parameters:obj (object) – Object from which to extract parameters. Defaults to None.
Returns:Message part as string
ATTRIBUTE_TYPE = 0

Message element represents a class attribute

FUNCTOR_TYPE = 2

Message element represents a functor

STRING_TYPE = 1

Message element represents a literal string

TYPES = {'attribute': 0, 'string': 1, 'functor': 2}

Dictionary of recognised types for a message element