Lithium mailer is a plugin for sending email messages from your li3 application.
- Mailers
When subclassing the `Mailer` the `$_messages` property may be used to set configuration for every message, or specific messages like:
Several options may be configured for creating (like `'from'`, `'to'`, etc.), rendering (like `'data'`, `'layout'`, etc.) and delivering (like `'delivery'`, adapter specific transport options, etc.) the message.
- Delivery
As with other `Adaptable`-based configurations, each delivery configuration is defined by a name, and an array of options for creating the transport adapter. The [Delivery](li3_mailer/net/mail/Delivery) also supports environment-based configuration like:
- Built-in transport adapters
- [ **Simple**](li3_mailer/net/mail/transport/adapter/Simple): sends email messages with `PHP`'s built-in function `mail`. - [ **Swift**](li3_mailer/net/mail/transport/adapter/Swift): depends on the [ SwiftMailer](http://swiftmailer.org/) library for sending email messages. - [ **Mailgun**](li3_mailer/net/mail/transport/adapter/Mailgun): uses the [ Mailgun](http://www.mailgun.com/) hosted service to send email messages (depends on cURL). - [ **Debug**](li3_mailer/net/mail/transport/adapter/Debug): instead of sending email messages it logs them to a given file or directory.
- Messages
- in delivery config - in mailer's `$_messages[0]` - in mailer's `$_messages['message_name']` - explicit options (e.g. `$options` arguments for `Mailer::deliver()`)
A message has one special attribute, the `$baseURL` property. It is easier to conceive the purpose of this value with understanding where it is needed and how it is used:
- When creating embedded attachments (see later) for example the message may need to generate a Content-ID. For generating this unique id the [ RFC](http://tools.ietf.org/html/rfc2822) recommends the form `timestamp@host`. - When generated URLs in templates (see later) should be absolute with scheme.
Furthermore as sending emails from cron scripts is a common use-case the plugin can not depend on having a web environment, where such a value can be determined. For this purpose the message has this property, which can be configured as seen already (e.g. in the delivery, the mailer or explicit). To generate correct URLs the `$baseURL` should have the format `scheme://host/base` (e.g. `http://example.com/my/app`). In addition when the message is initialized it will try to autodetect this setting (so it is possible to not specify this option when sending email messages only from web environment).
- Attachments
The attachment paths may be relative to mail asset path (`app/mails/_assets` by default, determined with `Media`, see later). Apart from regular files, attaching remote files (if `allow_url_fopen` is enabled) and explicit content are also possible:
- Templates
The default base path for email templates is `/app/mails` (instead of `/app/views`) to better separate from view templates, however this can be configured with the [Media](li3_mailer/net/mail/Media) class. The exact template will be determined by the message's name and the mailer by default, so a command like `Mailer::deliver('test')` would use `/app/mails/test.html.php` for rendering html content, while `FooMailer::deliver('test')` would first look for `/app/mails/foo/test.html.php` and fallback to the former only if the latter can not be found. Similarly the former command would use the `/app/mails/test.text.php` template for rendering text (plain) content. The default base paths for layouts and elements are `/app/mails/layouts' and `/app/mails/elements` respectively.
As with rendering from a controller these defaults may be overriden when sending mail messages:
- Helpers and handlers
The `Html` helper's [image](li3_mailer\template\helper\mail\Html::image()) method may be used to embed images:
This will place an element into the message with its source attribute set to the `Content-ID` that belongs to the attachment.
Creating or replacing a mail template helper is done exactly like regular view helpers with the exception that mail helpers are namespaced as `helper\mail\Name` (e.g. should be placed in `app/extensions/helper/mail/Name.php`).
Handlers are very similar to helpers and there is two commonly used handler that behaves a bit different in mail templates compared to view templates:
- The `url` handler will generate absolute URLs by default. - The `path` handler will generate absolute URLs by default and `'cid:ID'` style values for embeds.
- Referencing the message
Please note that when the message has multiple types, these methods should be placed in only one template, as calling them multiple times will lead to unexpected behavior: for example while the last value will override the formers when calling `subject` (rendering order is determined by the message's type order), attaching the same file from multiple templates will result in multiple attachments of the same file.