\org_openpsa_mail_template

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", while "__KEY_SUBKEY" accesses a specific property. Be aware that you need to implement the toString method if you want to output the entire object

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->get("mail_newreservation"); $mail->to = $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__
__RESERVATION__

Summary

Methods
Properties
Constants
__construct()
parse()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
_replace_callback()
_format_array()
prepare_value()
$_parameters
$_patterns
N/A

Properties

$_parameters

$_parameters : array

The parameters to use for the Mail template.

Type

array

$_patterns

$_patterns : 

Type

Methods

__construct()

__construct(array  $parameters) 

Constructs the template engine and parses the passed parameters

Parameters

array $parameters

The parameters to replace

parse()

parse(string  $input) : string

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.

Parameters

string $input

The string to parse

Returns

string —

The parsed string

_replace_callback()

_replace_callback(  $matches) 

Parameters

$matches

_format_array()

_format_array(array  $array) : string

Convert an array into a string representation

Uses word wrapping and skips recursive Arrays or objects.

Parameters

array $array

The array to be dumped.

Returns

string —

String representation.

prepare_value()

prepare_value(  $key,   $value) 

Parameters

$key
$value