2011-02-07 07:48:44 +01:00
|
|
|
# Email
|
|
|
|
|
|
|
|
SilverStripe has emailing functionality using the built-in mail() function in PHP.
|
|
|
|
Features include sending plaintext- and HTML emails, sending bulk emails, subscription, handling bounced back emails.
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
Your PHP configuration needs to include the SMTP module for sending emails.
|
|
|
|
If you are not running an SMTP server together with your webserver, you might need to setup PHP with the credentials for
|
|
|
|
an external SMTP server (see [PHP documentation for mail()](http://php.net/mail)).
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
### Sending combined HTML and Plaintext
|
|
|
|
|
|
|
|
By default, emails are sent in both HTML and Plaintext format.
|
|
|
|
A plaintext representation is automatically generated from the system
|
2011-03-08 22:05:51 +01:00
|
|
|
by stripping HTML markup, or transforming it where possible
|
2011-02-07 07:48:44 +01:00
|
|
|
(e.g. `<strong>text</strong>` is converted to `*text*`).
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$email = new Email($from, $to, $subject, $body);
|
|
|
|
$email->send();
|
|
|
|
|
|
|
|
|
2012-03-24 22:16:59 +01:00
|
|
|
The default HTML template is located in `framework/templates/email/GenericEmail.ss`.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Sending Plaintext only
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$email = new Email($from, $to, $subject, $body);
|
|
|
|
$email->sendPlain();
|
|
|
|
|
|
|
|
### Templates
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
* Create a SS-template file called, in this example we will use 'MyEmail.ss' inside `mysite/templates/email`.
|
2012-06-26 17:32:46 +02:00
|
|
|
* Fill this out with the body text for your email. You can use any [SS-template syntax](/topics/templates) (e.g. `<% loop %>`,
|
2011-02-07 07:48:44 +01:00
|
|
|
`<% if %>`, $FirstName etc)
|
|
|
|
* Choose your template with **setTemplate()**
|
|
|
|
* Populate any custom data into the template before sending with **populateTemplate()**
|
|
|
|
|
2012-12-04 08:00:09 +01:00
|
|
|
Example:
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
:::php
|
|
|
|
$email = new Email($from, $to, $subject, $body);
|
|
|
|
$email->setTemplate('MyEmail');
|
|
|
|
|
|
|
|
// You can call this multiple times or bundle everything into an array, including DataSetObjects
|
|
|
|
$email->populateTemplate(Member::currentUser());
|
|
|
|
|
2012-12-04 07:56:36 +01:00
|
|
|
$welcomeMsg = 'Thank you for joining on '.date('Y-m-d'.'!');
|
2012-12-04 08:00:09 +01:00
|
|
|
|
2012-12-04 07:56:36 +01:00
|
|
|
$email->populateTemplate(array(
|
|
|
|
'WelcomeMessage' => $welcomeMsg, // Accessible in template via $WelcomeMessage
|
|
|
|
));
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
$email->send();
|
|
|
|
|
|
|
|
|
|
|
|
### Subclassing
|
|
|
|
|
|
|
|
Class definition:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
<?php
|
|
|
|
class MyEmail extends Email{
|
|
|
|
protected
|
|
|
|
$to = '$Email', // Be sure to encase this in single-quotes, as it is evaluated later by the template parser
|
|
|
|
$from = 'email@email.com',
|
|
|
|
$ss_template = 'MyEmail';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
<?php
|
|
|
|
$email = new MyEmail();
|
|
|
|
$email->populateTemplate(Member::currentUser()); // This will populate the template, $to, $from etc variables if they exist
|
|
|
|
$email->send(); // Will immediately send an HTML email with appropriate plain-text content
|
|
|
|
|
|
|
|
|
|
|
|
### Administrator Emails
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
You can influence the default sender address of emails through the `Email.admin_email`
|
|
|
|
[configuration setting](/topics/configuration). This address is used if the `from` field is empty.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Redirecting Emails
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
Further [configuration settings](/topics/configuration) relating to email rewriting:
|
|
|
|
|
|
|
|
* `Email.send_all_emails_to` will redirect all emails sent to the given address. Handy for testing!
|
|
|
|
* `Email.cc_all_emails_to` and `Email.bcc_all_emails_to` will keep the email going to its original recipients, but
|
2011-03-08 22:05:51 +01:00
|
|
|
add an additional recipient in the BCC/CC header. Good for monitoring system-generated correspondence on the live
|
2011-02-07 07:48:44 +01:00
|
|
|
systems.
|
|
|
|
|
|
|
|
:::php
|
2013-03-21 19:48:54 +01:00
|
|
|
if(Director::isLive()) Config::inst()->update('Email', 'bcc_all_emails_to', "client@example.com");
|
|
|
|
else Config::inst()->update('Email', 'send_all_emails_to', "developer@example.com");
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
### Setting Custom Headers
|
|
|
|
|
|
|
|
For email headers which do not have getters or setters (like setTo(), setFrom()) you can use **addCustomHeader($header,
|
|
|
|
$value)**
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$email = new Email(...);
|
|
|
|
$email->addCustomHeader('HeaderName', 'HeaderValue');
|
|
|
|
..
|
|
|
|
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
See [Wikipedia E-mail Message header](http://en.wikipedia.org/wiki/E-mail#Message_header) for a list of header names.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Newsletters
|
|
|
|
|
|
|
|
The [newsletter module](http://silverstripe.org/newsletter-module) provides a UI and logic to send batch emails.
|
|
|
|
|
2014-09-25 06:04:56 +02:00
|
|
|
### 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('bounce@mycompany.com');`
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
## API Documentation
|
|
|
|
|
2012-03-24 22:16:59 +01:00
|
|
|
`[api:Email]`
|