Version 3 end of life
This version of Silverstripe CMS will not recieve any additional bug fixes or documentation updates. Go to documentation for the most recent stable version.


Creating and sending email in SilverStripe is done through the Email and Mailer classes. This document covers how to create an Email instance, customise it with a HTML template, then send it through a custom Mailer.


Out of the box, SilverStripe will use the built-in PHP mail() command. If you are not running an SMTP server, you will need to either configure PHP's SMTP settings (see PHP documentation to include your mail server configuration or use one of the third party SMTP services like SparkPost and Postmark.


Sending plain text only

	$email = new Email($from, $to, $subject, $body);

By default, emails are sent in both HTML and Plaintext format. A plaintext representation is automatically generated from the system by stripping HTML markup, or transforming it where possible (e.g. <strong>text</strong> is converted to *text*).

	$email = new Email($from, $to, $subject, $body);

The default HTML template for emails is named GenericEmail and is located in framework/templates/email/. To customise this template, copy it to the mysite/templates/Email/ folder or use setTemplate when you create the Email instance. [/info]


HTML emails can use custom templates using the same template language as your website template. You can also pass the email object additional information using the populateTemplate method.


	<h1>Hi $Member.FirstName</h1>
	<p>You can go to $Link.</p>
	$email = new Email();
		->populateTemplate(new ArrayData(array(
			'Member' => Member::currentUser(),
			'Link' => $link


As we've added a new template file (MyCustomEmail) make sure you clear the SilverStripe cache for your changes to take affect. [/alert]

Sub classing

To keep your application code clean and your internal API clear, a better approach to generating an email is to create a new subclass of Email which takes the required dependencies and handles setting the properties itself.



	class MyEmail extends Email {
		protected $ss_template = "MyEmail";

		public function __construct($member) {
			$from = '';
			$to = $member->Email;
			$subject = "Welcome to our site.";
			$link = Director::absoluteBaseUrl();

			parent::__construct($from, $to, $subject);

			$this->populateTemplate(new ArrayData(array(
				'Member' => $member->Email,
				'Link' => $link
	$member = Member::currentUser();

	$email = new MyEmail($member);

Administrator Emails

You can set the default sender address of emails through the Email.admin_email configuration setting.



[alert] Remember, setting a from address that doesn't come from your domain (such as the users email) will likely see your email marked as spam. If you want to send from another address think about using the setReplyTo method. [/alert]

Redirecting Emails

There are several other configuration settings to manipulate the email server.

  • Email.send_all_emails_to will redirect all emails sent to the given address. This is useful for testing and staging servers where you do not wish to send emails out.
  • Email.cc_all_emails_to and Email.bcc_all_emails_to will add an additional recipient in the BCC / CC header. These are good for monitoring system-generated correspondence on the live systems.

Configuration of those properties looks like the following:


	if(Director::isLive()) {
		Config::inst()->update('Email', 'bcc_all_emails_to', "");
	} else {
		Config::inst()->update('Email', 'send_all_emails_to', "");

For email messages that should have an email address which is replied to that actually differs from the original "from" email, do the following. This is encouraged especially when the domain responsible for sending the message isn't necessarily the same which should be used for return correspondence and should help prevent your message from being marked as spam.

	$email = new Email(..);

For email headers which do not have getters or setters (like setTo(), setFrom()) you can use addCustomHeader($header, $value)

	$email = new Email(...);
	$email->addCustomHeader('HeaderName', 'HeaderValue');

See this Wikipedia entry for a list of header names. [/info]


The newsletter module provides a UI and logic to send batch emails.

Custom Mailers

SilverStripe supports changing out the underlying web server SMTP mailer service through the Email::set_mailer() function. A Mailer subclass will commonly override the sendPlain and sendHTML methods to send emails through curl or some other process that isn't the built in mail() command.

[info] There are a number of custom mailer add-ons available like Mandrill and Postmark. [/info]

In this example, LocalMailer will take any email's going while the site is in Development mode and save it to the assets folder instead.



	class LocalMailer extends Mailer {

		function sendHTML($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, $plainContent = false, $inlineImages = false) {
			$file = ASSETS_PATH . '/_mail_'. urlencode(sprintf("%s_%s", $subject, $to));

			file_put_contents($file, $htmlContent);
		function sendPlain($to, $from, $subject, $htmlContent, $attachedFiles = false, $customheaders = false, $plainContent = false, $inlineImages = false) {
			$file = ASSETS_PATH . '/_mail_'. urlencode(sprintf("%s_%s", $subject, $to));

			file_put_contents($file, $htmlContent);
	if(Director::isLive()) {
		Email::set_mailer(new PostmarkMailer());
	} else {
		Email::set_mailer(new LocalMailer());

Setting bounce handler

A bounce handler email can be specified one of a few ways:

  • Via config by setting the Mailer.default_bounce_email config to the desired email address.
  • Via _ss_environment.php by setting the BOUNCE_EMAIL definition.
  • Via PHP by calling Email::mailer()->setBounceEmail('');

API Documentation