This class contains an email template engine.

It can take a template and fill it in with the parameters that have been passed.

E-Mail template language

Three types of variables can be inserted into the email's subject or body. Every value has an associated key, which is searched as array key in the parameter array. Key names are matched case sensitive.

  1. String values

They are identified by "KEY" and are inserted directly.

  1. Associative arrays

If you want to pass an array as parameter, ensure that both key and value are convertible to a string by PHP implicitly. Ideally, you have only strings, of course. In the following example, "KEY" refers to the key of the array within the parameter array, and "SUBKEY" refers to the key of a value within the actual array.

Again, you can access the (whole) array using "KEY". In that case you will get a formatted output of all keys and values, consisting of "SUBKEY: VALUE" entries. The value gets word-wrapped and indented automatically at about 76 chars to keep the output easily readable.

If you want to access a specific value from this array, you have to use "KEY_SUBKEY" to identify it. This syntax is treated like a string value.

  1. Generic objects

You can pass any object as a value. In this case, the same semantic as with an Array can be used to access the object: "KEY" will give you a complete dump, while "KEY_SUBKEY" accesses a specific property.

The complete dump will omit all properties that are prefixed with an "_"; according to the MidCOM namespace conventions, these are private members of a class and should not be touched. You can still access them with the direct index, though this is strongly discouraged within a MidCOM context. Also note that variables with more then one underscore as a prefix might cause trouble with the regular expression used to parse the template.

Example usage code

$mail = new org_openpsa_mail();
$parameters = array
    "RESOURCE" => $this->_resource,
    "RESERVATION" => $this->reservation,
    "ISOSTART" => $this->dm->data["start"]["strfulldate"],
    "ISOEND" => $this->dm->data["end"]["strfulldate"],
    "LOCALSTART" => $this->dm->data["start"]["local_strfulldate"],
    "LOCALEND" => $this->dm->data["end"]["local_strfulldate"],
$mail->parameters = $parameters;
$mail->body = $this->_config_dm->data["mail_newreservation"];
$mail->to = $this->dm->data["email"];

if (!$mail->send())
    debug_add("Email could not be sent: " . $mail->get_error_string(), MIDCOM_LOG_WARN);

This code could for example use a Template subject / body like this:

Subject: New Reservation for __RESOURCE_name__

Your reservation has been received, you will receive a confirmation E-Mail shortly:

Start: __ISOSTART__
End: __ISOEND__
package org.openpsa.mail


__construct (array $parameters)

Constructs the template engine and parses the passed parameters



arrayThe parameters to replace

parse (string $input)

Parses the template and generates the message body and subject.

Internally, it relies heavily on Perl Regular Expressions to replace the template parameters with their values.



stringThe string to parse


stringThe parsed string

_format_array (array $array)

Helper function to convert an array into a string representation

Uses word wrapping and skips recursive Arrays or objects.



arrayThe array to be dumped.


stringString representation.

_format_object (object $obj)

Helper function to convert an object into a string representation.

Uses word wrapping and skips members beginning with an underscore (which are private per definition). Relies on reflection to parse the object.



objectAny PHP object that can be parsed with get_object_vars().


stringString representation.



array $_parameters

The parameters to use for the Mail template.

mixed $_patterns


mixed $_replacements