Amazon SES Email Transport
Aws.AmazonSesApi sends email with the SendEmail operation of the Amazon SES API v2. Set it as the transport of an email profile in config/email.php (legacy layout: app/Config/email.php):
class EmailConfig
{
/**
* Default email profile values.
*
* @var array
*/
public $default = [
'transport' => 'Aws.AmazonSesApi',
'from' => 'example@example.com',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
];
}The SES client is created with the Aws configuration. These keys of the email profile override it:
| Key | Description |
|---|---|
region | AWS region of SES. |
version | API version. |
key, secret | Access key ID and secret access key. When they are not set, the credentials of the Aws configuration or the default credential provider chain are used. |
token | Session token, for temporary key and secret. |
endpoint | Endpoint URL of SES, e.g. for a local emulator. |
The transport sends the text and HTML parts, the To, Cc, Bcc and Reply-To addresses, and the attachments. CakeEmail::send() returns the SES message ID as id:
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('default');
$result = $email->to('user@example.com')
->subject('Welcome')
->send('Hello!');
$messageId = $result['id'];The sender address (from) must be verified in SES.
Mailer DSN
With pieceofcake2/cakephp 2.12.7 or later, which supports the 'dsn' key of email profiles, the transport can be set with a DSN in the format of Symfony's Amazon Mailer bridge, for example from the MAILER_DSN environment variable. Load the plugin with its bootstrap, which registers the ses schemes with CakeEmail::setDsnClassMap() (and also the S3 stream wrapper):
// config/bootstrap.php
CakePlugin::load('Aws', ['bootstrap' => true]);class EmailConfig
{
/**
* Default email profile values.
*
* @var array
*/
public $default = [
'transport' => 'Mail',
'from' => 'example@example.com',
'dsn' => null,
];
/**
* Reads the transport from MAILER_DSN.
*/
public function __construct()
{
$this->default['dsn'] = env('MAILER_DSN');
}
}| DSN | Configuration |
|---|---|
ses+api://ACCESS_KEY:SECRET_KEY@default?region=eu-west-1 | Aws.AmazonSesApi, key, secret, region |
ses+https://ACCESS_KEY:SECRET_KEY@default, ses://... | the same (Aws.AmazonSesApi) |
ses+api://default?region=eu-west-1 | Aws.AmazonSesApi with the credentials of the Aws configuration or the default credential provider chain |
ses+smtp://USERNAME:PASSWORD@default?region=eu-west-1, ses+smtps://... | the core Smtp transport to ssl://email-smtp.eu-west-1.amazonaws.com, port 465, with the SES SMTP credentials |
?session_token=...setstoken, the session token of temporary credentials.- A host other than
defaultis the endpoint (ses+api://KEY:SECRET@localhost:4566useshttps://localhost:4566), or the SMTP host forses+smtp. - When the DSN has no
region, theregionof the profile, theregionof theAwsconfiguration,AWS_REGIONorAWS_DEFAULT_REGIONis used, andeu-west-1(the default of Symfony) when none is set. - The
/and+of AWS secret keys may stay as they are; encode%as%25and?as%3F. ses+apiandses+httpsboth use the SES API v2SendEmailoperation of this transport; Symfony's distinction (simple vs. raw message) does not apply.ping_thresholdofses+smtpis ignored.- The keys parsed from the DSN replace the same keys of the profile; the other keys (
from,charset, ...) are kept. An unsetMAILER_DSNis ignored. - With a pieceofcake2/cakephp release without DSN support, the bootstrap skips the registration and the
'dsn'key is ignored.
