2019-11-18 05:58:33 +01:00
---
title: Email
2021-06-30 11:48:52 +02:00
summary: Send HTML and plain text email from your Silverstripe CMS application.
2019-11-18 05:58:33 +01:00
icon: envelope-open
---
2014-09-21 02:07:58 +02:00
2011-02-07 07:48:44 +01:00
# Email
2021-08-18 02:16:45 +02:00
Creating and sending email in Silverstripe CMS is done through the [Email ](api:SilverStripe\Control\Email\Email ) and [Mailer ](api:SilverStripe\Control\Email\Mailer ) classes. This document covers how to create an `Email` instance, customise it with a HTML template, then send it through a custom `Mailer` .
2011-02-07 07:48:44 +01:00
## Configuration
2021-08-18 02:16:45 +02:00
Silverstripe CMS provides an API over the top of the [SwiftMailer ](http://swiftmailer.org/ ) PHP library which comes with an extensive list of "transports" for sending mail via different services.
2017-01-13 02:48:46 +01:00
2021-08-18 02:16:45 +02:00
For legacy reasons, Silverstripe CMS 4 defaults to using the built-in PHP `mail()` command via a deprecated class `Swift_MailTransport` . However, using this layer is less secure and is strongly discouraged.
It's highly recommended you upgrade to a more robust transport for additional security. The Sendmail transport is the most common one. The `sendmail` binary is widely available across most Linux/Unix servers.
You can use any of the Transport classes provided natively by SwiftMailer. There are also countless PHP libraries offering custom Transports to integrate with third party mailing service:
- read the [SwiftMailer Transport Types documentation ](https://swiftmailer.symfony.com/docs/sending.html#transport-types ) for a full list of native Transport
- search [Packagist for SwiftMailer Transport ](https://packagist.org/?query=SwiftMailer+Transport ) to discover additional third party integrations
To swap out the transport used by the `Mailer` , create a file `app/_config/email.yml`
To use a `sendmail` binary:
2017-01-13 02:48:46 +01:00
```yml
2021-08-18 02:16:45 +02:00
---
Name: myemailconfig
After:
- '#emailconfig'
---
2017-01-13 02:48:46 +01:00
SilverStripe\Core\Injector\Injector:
2021-08-18 02:16:45 +02:00
Swift_Transport:
class: Swift_SendmailTransport
2017-01-13 02:48:46 +01:00
```
2011-02-07 07:48:44 +01:00
2021-08-18 02:16:45 +02:00
To use SMTP:
2017-12-05 18:18:46 +01:00
```yml
2017-12-14 14:07:37 +01:00
---
Name: myemailconfig
After:
- '#emailconfig'
---
2017-12-05 18:18:46 +01:00
SilverStripe\Core\Injector\Injector:
Swift_Transport:
class: Swift_SmtpTransport
properties:
Host: smtp.host.com
Port: < port >
Encryption: tls
calls:
2019-04-28 23:53:22 +02:00
Username: [ setUsername, ['`APP_SMTP_USERNAME`'] ]
Password: [ setPassword, ['`APP_SMTP_PASSWORD`'] ]
2017-12-05 18:18:46 +01:00
AuthMode: [ setAuthMode, ['login'] ]
```
2019-04-28 23:53:22 +02:00
Note the usage of backticks to designate environment variables for the credentials - ensure you set these in your `.env` file or in your webserver configuration.
2021-08-18 02:16:45 +02:00
### Mailer Configuration for dev environments
You may wish to use a different mailer configuration in your development environment. This can be used to suppress outgoing messages or to capture them for debugging purposes in a service like [MailCatcher ](https://mailcatcher.me/ ).
You can suppress all emails by using the [`Swift_Transport_NullTransport` ](https://github.com/swiftmailer/swiftmailer/blob/master/lib/classes/Swift/Transport/NullTransport.php ).
```yml
---
Name: mydevemailconfig
After:
- '#emailconfig'
Only:
environment: dev
---
SilverStripe\Core\Injector\Injector:
Swift_Transport:
class: Swift_Transport_NullTransport
```
If you're using MailCatcher, or a similar tool, you can tell `Swift_SendmailTransport` to use a different binary.
```yml
---
Name: mydevemailconfig
After:
- '#emailconfig'
Only:
environment: dev
---
SilverStripe\Core\Injector\Injector:
Swift_Transport:
class: Swift_SendmailTransport
constructor:
0: '/usr/bin/env catchmail -t'
```
2011-02-07 07:48:44 +01:00
## Usage
2014-09-28 10:39:59 +02:00
### Sending plain text only
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Control\Email\Email;
2017-10-26 02:22:02 +02:00
2017-10-27 04:38:27 +02:00
$email = new Email($from, $to, $subject, $body);
$email->sendPlain();
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2014-09-28 10:39:59 +02:00
### Sending combined HTML and plain text
2011-02-07 07:48:44 +01:00
2014-09-28 10:39:59 +02:00
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*` ).
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$email = new Email($from, $to, $subject, $body);
$email->send();
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2019-11-18 05:58:33 +01:00
[info]
2017-10-06 17:00:32 +02:00
The default HTML template for emails is named `GenericEmail` and is located in `vendor/silverstripe/framework/templates/SilverStripe/Email/` .
2018-06-25 00:39:53 +02:00
To customise this template, copy it to the `app/templates/Email/` folder or use `setHTMLTemplate` when you create the
2014-09-28 10:39:59 +02:00
`Email` instance.
2019-11-18 05:58:33 +01:00
[/info]
2011-02-07 07:48:44 +01:00
2014-09-28 10:39:59 +02:00
### Templates
2012-12-04 08:00:09 +01:00
2014-09-28 10:39:59 +02:00
HTML emails can use custom templates using the same template language as your website template. You can also pass the
2017-01-13 02:48:46 +01:00
email object additional information using the `setData` and `addData` methods.
2014-09-28 10:39:59 +02:00
2018-06-25 00:39:53 +02:00
**app/templates/Email/MyCustomEmail.ss**
2014-09-21 02:07:58 +02:00
2017-08-03 02:51:32 +02:00
```ss
2017-10-27 04:38:27 +02:00
< h1 > Hi $Member.FirstName< / h1 >
< p > You can go to $Link.< / p >
2017-08-03 02:51:32 +02:00
```
2014-09-21 02:07:58 +02:00
2014-09-28 10:39:59 +02:00
The PHP Logic..
2014-09-21 02:07:58 +02:00
2017-01-13 02:48:46 +01:00
```php
$email = SilverStripe\Control\Email\Email::create()
2017-01-13 17:12:25 +01:00
->setHTMLTemplate('Email\\MyCustomEmail')
2017-08-03 05:35:09 +02:00
->setData([
2017-05-20 06:32:25 +02:00
'Member' => Security::getCurrentUser(),
2017-01-13 02:48:46 +01:00
'Link'=> $link,
2017-08-03 05:35:09 +02:00
])
2017-01-13 02:48:46 +01:00
->setFrom($from)
->setTo($to)
->setSubject($subject);
if ($email->send()) {
//email sent successfully
} else {
// there may have been 1 or more failures
}
2017-08-03 05:35:09 +02:00
2017-01-13 02:48:46 +01:00
```
2011-02-07 07:48:44 +01:00
2019-11-18 05:58:33 +01:00
[alert]
2021-06-30 11:48:52 +02:00
As we've added a new template file (`MyCustomEmail`) make sure you clear the Silverstripe CMS cache for your changes to
2014-09-28 10:39:59 +02:00
take affect.
2019-11-18 05:58:33 +01:00
[/alert]
2011-02-07 07:48:44 +01:00
2017-01-13 17:12:25 +01:00
#### Custom plain templates
2021-06-30 11:48:52 +02:00
By default Silverstripe CMS will generate a plain text representation of the email from the HTML body. However if you'd like
2017-01-13 17:12:25 +01:00
to specify your own own plaintext version/template you can use `$email->setPlainTemplate()` to render a custom view of
the plain email:
```php
2017-07-03 03:22:12 +02:00
$email = new SilverStripe\Control\Email\Email();
2017-01-13 17:12:25 +01:00
$email->setPlainTemplate('MyPlanTemplate');
$this->send();
```
2014-09-28 10:39:59 +02:00
## Administrator Emails
You can set the default sender address of emails through the `Email.admin_email` [configuration setting ](/developer_guides/configuration ).
2018-06-25 00:39:53 +02:00
**app/_config/app.yml**
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```yaml
2017-10-27 04:38:27 +02:00
SilverStripe\Control\Email\Email:
2018-06-26 00:13:32 +02:00
admin_email: support@example.com
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2018-08-29 01:42:12 +02:00
To add a display name, set `admin_email` as follow.
```yaml
SilverStripe\Control\Email\Email:
admin_email:
support@example.com: 'Support team'
```
2019-11-18 05:58:33 +01:00
[alert]
2014-09-28 10:39:59 +02:00
Remember, setting a `from` address that doesn't come from your domain (such as the users email) will likely see your
2016-07-08 22:03:30 +02:00
email marked as spam. If you want to send from another address think about using the `setReplyTo` method.
2019-11-18 05:58:33 +01:00
[/alert]
2011-02-07 07:48:44 +01:00
2014-09-28 10:39:59 +02:00
## Redirecting Emails
2011-02-07 07:48:44 +01:00
2014-09-28 10:39:59 +02:00
There are several other [configuration settings ](/developer_guides/configuration ) to manipulate the email server.
2011-02-07 07:48:44 +01:00
2017-01-13 02:48:46 +01:00
* `SilverStripe\Control\Email\Email.send_all_emails_to` will redirect all emails sent to the given address.
All recipients will be removed (including CC and BCC addresses). This is useful for testing and staging servers where
you do not wish to send emails out. For debugging the original addresses are added as `X-Original-*` headers on the email.
* `SilverStripe\Control\Email\Email.cc_all_emails_to` and `SilverStripe\Control\Email\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.
2013-03-21 19:48:54 +01:00
2014-09-28 10:39:59 +02:00
Configuration of those properties looks like the following:
2018-06-25 00:39:53 +02:00
**app/_config.php**
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2018-02-22 20:35:01 +01:00
use SilverStripe\Control\Email\Email;
2017-10-27 04:38:27 +02:00
use SilverStripe\Core\Config\Config;
if(Director::isLive()) {
2018-06-26 00:13:32 +02:00
Config::modify()->set(Email::class, 'bcc_all_emails_to', "client@example.com");
2017-10-27 04:38:27 +02:00
} else {
2018-06-26 00:13:32 +02:00
Config::modify()->set(Email::class, 'send_all_emails_to', "developer@example.com");
2017-10-27 04:38:27 +02:00
}
2017-08-03 02:51:32 +02:00
```
2014-09-28 10:39:59 +02:00
2016-07-08 22:03:30 +02:00
### Setting custom "Reply To" email address.
2017-01-13 02:48:46 +01:00
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
2017-08-03 02:51:32 +02:00
marked as spam.
2017-10-27 04:38:27 +02:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$email = new Email(..);
2018-06-26 00:13:32 +02:00
$email->setReplyTo('reply@example.com');
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
### Setting Custom Headers
2017-01-13 02:48:46 +01:00
For email headers which do not have getters or setters (like setTo(), setFrom()) you can manipulate the underlying
`Swift_Message` that we provide a wrapper for.
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$email = new Email(...);
$email->getSwiftMessage()->getHeaders()->addTextHeader('HeaderName', 'HeaderValue');
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2019-11-18 05:58:33 +01:00
[info]
2014-09-28 10:39:59 +02:00
See this [Wikipedia ](http://en.wikipedia.org/wiki/E-mail#Message_header ) entry for a list of header names.
2019-11-18 05:58:33 +01:00
[/info]
2011-02-07 07:48:44 +01:00
2019-02-13 09:37:34 +01:00
## Disabling Emails
If required, you can also disable email sending entirely. This is useful for testing and staging servers where
you do not wish to send emails out.
```yaml
---
Name: myemailconfig
Only:
Environment: dev
---
SilverStripe\Core\Injector\Injector:
Swift_Transport:
class: Swift_NullTransport
```
2017-01-13 02:48:46 +01:00
## SwiftMailer Documentation
2014-09-25 06:04:56 +02:00
2017-01-13 02:48:46 +01:00
For further information on SwiftMailer, consult their docs: http://swiftmailer.org/docs/introduction.html
2014-09-25 06:04:56 +02:00
2021-08-18 02:16:45 +02:00
2011-02-07 07:48:44 +01:00
## API Documentation
2017-07-03 03:22:12 +02:00
* [Email ](api:SilverStripe\Control\Email\Email )