Skip to content
Harmen Janssen edited this page Jan 19, 2016 · 2 revisions

Garp has a handy Garp_Mailer class that looks for some sensible defaults in config files.
Using Garp_Mailer in your project makes sure every mail

  • is sent from the same address
  • uses Amazon SES if available, but falls back to SMTP
  • uses the same email template
  • uses the same attachments for every mail (such as your site logo)

Usage:

$mailer = new Garp_Mailer();
$mailer->send(array(
  'to' => '[email protected]',
  'subject' => 'hello there',
  'message' => '...'
));

Configuration

If you have an Amazon SES account, configure as such:

amazon.ses.accessKey = "abcdef"
amazon.ses.privateKey = "123456"
amazon.ses.region = "eu-west-1"

It will automatically be picked up.
For historical reasons, the following config keys are searched for a from address:

amazon.ses.fromAddress
mailer.fromAddress

Obviously, amazon.ses.fromAddress is only searched when Amazon SES is configured.

You can configure other defaults, for attachments and templates:

mailer.attachments.sitelogo = APPLICATION_PATH "/../public/media/images/logo.png"
mailer.template = "email/email.phtml"

Send options

A full example:

$sendOptions = array(
  'to' => '[email protected]',
  'subject' => 'hello there',
  'message' => 'Hello there!',
  'htmlMessage' => '<p><b>Hello</b> there!</p>',
  'cc' => '[email protected]',
  'bcc' => '[email protected]',
  'replyTo' => '[email protected]'
);
// $viewParams get sent to the view template
$viewParams = array(
  'foo' => 'bar',
  '...'
);
// $attachments should be an assoc array where you can use the key in the mail template
$attachments = array(
  'sitelogo' => APPLICATION_PATH . "/../public/media/images/logo.png"
);

$mailer->send($sendOptions, $viewParams, $attachments);

Note, attachments are currently embedded inline. This means you can use them in the template like this:

<img src="cid:sitelogo">

Runtime configuration

The complete public API:

$mailer->addAttachment('sitelogo', APPLICATION_PATH . "/../public/media/images/logo.png");
$mailer->addAttachments($array);
$mailer->getDefaultAttachments();

$mailer->getDefaultFromAddress();
$mailer->getFromAddress();
$mailer->setFromAddress('[email protected]');

$mailer->getDefaultHtmlTemplate();
$mailer->getHtmlTemplate();
$mailer->setHtmlTemplate('email/email.phtml');

$mailer->getDefaultTransport();
$mailer->getTransport();
$mailer->setTransport(new Zend_Mail_Transport_File());

$mailer->getCharacterEncoding();
$mailer->setCharacterEncoding('utf-8');

Disable mails

If you want to disable all mails, for instance in your testing environment, you can configure

mailer.sendMail = false

Or this when Amazon SES is used:

amazon.ses.sendMail = false

This will swap the underlying mail transport class to Zend_Mail_Transport_File.

Clone this wiki locally