Skip to content

EmailLib

Mark edited this page Apr 17, 2014 · 18 revisions

The EmailLib extends the CakeEmail class and acts as a more intelligent wrapper class. It adds some more usefulness and makes debugging/testing easier.

Goodies

  • Auto-set "from" as admin email (no need to do that in the code unless needs overwriting).
  • Enable easier attachment adding (and also from blob): addAttachment($file, $name = null, $fileInfo = array()) addBlobAttachment($content, $name, $mimeType = null, $fileInfo = array())
  • Enable embedded images in html mails: addEmbeddedAttachment($file, $name = null, $contentId = null, $options = array()) addEmbeddedBlobAttachment($content, $name, $mimeType = null, $contentId = null, $options = array())
  • Auto mimetype detection for attachments (inline or not).
  • Allow wrapLength to be adjusted: wrapLength($length = null).
  • Configure::read('Config.xMailer') can modify the x-mailer header.
  • Basic validation supported.
  • Allow priority to be set (1 to 5): priority($priority = null).
  • Quick way to send system emails/reports: App::uses() + EmailLib::systemEmail($subject, $message).
  • Extensive logging and error tracing as well as debugging using getDebug()/getError().
  • Don't send emails without Configure::write('Email.live'), but log them away verbosely. For testing.
  • Security measure: Don't send emails to actual addresses in debug mode, they will be sent to Configure::read('Config.adminEmail') instead. Same for cc/bcc.

Setup

It is easiest to extend the Tools plugin BaseEmailConfig class. It provides a wrapper to set many of the above on its own. It also helps to have settings in configs_private.php files instead of actually committing them into the email.php class (which might want to be kept in version control in a stubbed way.

// Config/email.php
App::uses('BaseEmailConfig', 'Tools.Config');

class EmailConfig extends BaseEmailConfig {

	public $default = array(
		'host' => 'example.de',
		'port' => 465,
		'username' => '[email protected]',
		'password' => '', // Will be set via Configure and configs_private
		'template' => 'custom',
		'layout' => 'custom',
		'transport' => 'Smtp',
		'trace' => true, // Detailed trace log
		'log' => true // Report log entry
	);

	public $mandrill = array(
		'transport' => 'Mailchimp.Mandrill',
	);

}
// Config/configs_private.php
Configure::write('Email.Pwd.default', 'mypwd');

default maps to the attribute public $default here.

If want to use some special SMTP setting, overwrite them all in your private config. Let's say for live we need a different SMTP setting then for stage.

Configure::write('Mail.smtpHost', 'example.de');
Configure::write('Mail.smtpUsername', '[email protected]');
Configure::write('Mail.smtpPassword', 'somepwd');
// Same for smtpPort, smtpTimeout, smtpTls

This is mainly for the Transport class being used.

For the actual EmailLib you will need some Configure keys, as well:

// Main system email, will be used as default "from"
Configure::write('Config.systemName', 'Our webseite');
Configure::write('Config.systemEmail', '[email protected]');

// Main admin email, will be used as fallback if no systemEmail is set/needed
Configure::write('Config.adminName', 'dereuromark');
Configure::write('Config.adminEmail', '[email protected]');

// I also set and use this for some Emails (but you need to use those manually)
Configure::write('Config.noReplyName', 'dereuromark');
Configure::write('Config.noReplyEmail', '[email protected]');

For EmailLib::systemEmail() messages "systemEmail" will sent to "adminEmail". For all others "systemEmail" will send to the actual addresses if debug mode is off - and Configure::write('Email.live') is true.

Clone this wiki locally