Built on top of core Email class.
An enhanced class to
Configure::write('Config.live'), but log them away verbosely. For testing.Configure::read('Config.adminEmail') instead. Same for cc/bcc.First, replace the use statement in the bootstrap:
//use Cake\Mailer\Email;
use Tools\Mailer\Email;
The other two lines can stay as they are:
Email::configTransport(Configure::consume('EmailTransport'));
Email::config(Configure::consume('Email'));
They will read from Configure what you defined there, e.g for sending SMTP mails.
'Email' => [
'default' => [
'from' => 'your@email.com',
],
],
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'smtp.hostname.com',
'username' => 'your@email.com',
'password' => 'yourpwd',
'tls' => true,
'port' => 587,
],
]
That's it.
Don't forget the use statement in any class you use it. Then instantiate it as
$email = new Email();
// Add the rest of the information needed
$email->to($email, $name);
$email->subject($subject);
$email->replyTo($adminEmail, $adminName); // Optional reply to header
$email->template('sometemplate'); // Optional
$email->layout('somelayout'); // Optional
$email->viewVars(compact('message', 'other'));
// Send it
if ($email->send()) {
// Success
} else {
// Error
// You can use $email->getError() and display it in debug mode or log it away
}
from is already set with your admin email by default, if you configured Config.adminEmail.
This will automatically set the contentId and mimeType of the file. This is necessary for embedded attachments.
$email->addEmbeddedAttachment('/some/file.ext', 'Some custom name');
This will automatically set the mimeType of the file (using the filename you provide).
$email->addBlobAttachment($somePngFileBlobContent, 'my_filename.png');
This will also automatically set the mimeType of the file as above. But it will set the contentId, as well. This is necessary for embedded attachments.
$email->addEmbeddedBlobAttachment($somePngFileBlobContent, 'my_filename.png');
You can use Config.xMailer config to set a custom xMailer header.
Priority and line length can also be adjusted if needed.
By default it switches to "Debug" transport in debug mode. If you don't want that set Configure value Config.live to true.