Usage examples of the mailer wrapper over SwiftMailer for Phalcon:
$config = [
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 465,
'encryption' => 'ssl',
'username' => '[email protected]',
'password' => 'your_password',
'from' => [
'email' => '[email protected]',
'name' => 'YOUR FROM NAME',
],
];
$config = [
'driver' => 'sendmail',
'sendmail' => '/usr/sbin/sendmail -bs',
'from' => [
'email' => '[email protected]',
'name' => 'YOUR FROM NAME',
],
];
$mailer = new \Phalcon\Incubator\Mailer\Manager($config);
$message = $mailer->createMessage()
->to('[email protected]', 'OPTIONAL NAME')
->subject('Hello world!')
->content('Hello world!');
// Set the Cc addresses of this message.
$message->cc('[email protected]');
// Set the Bcc addresses of this message.
$message->bcc('[email protected]');
// Send message
$message->send();
/**
To create message with View, you need to define in the DI the component simple View.
*/
$this->di->set(
'simple',
function () {
$view = new Phalcon\Mvc\View\Simple();
$view->setViewsDir($config->application->viewsDir);
return $view;
},
true
);
$this->di->setShared('view', function () {
$view = new Phalcon\Mvc\View();
$view->setDI($this);
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines([
'.volt' => function ($view) {
$volt = new Phalcon\Mvc\View\Engine\Volt($view, $this);
$volt->setOptions([
'path' => $config->application->cacheDir,
'separator' => '_'
]);
return $volt;
},
'.phtml' => Phalcon\Mvc\View\Engine\Php::class
]);
return $view;
});
$mailer = new \Phalcon\Incubator\Mailer\Manager($config);
// view relative to the folder viewsDir (REQUIRED)
$viewPath = 'email/example_message';
// Set variables to views (OPTIONAL)
$params = [
'var1' => 'VAR VALUE 1',
'var2' => 'VAR VALUE 2',
// ...
'varN' => 'VAR VALUE N',
];
$message = $mailer->createMessageFromView($viewPath, $params)
->to('[email protected]', 'OPTIONAL NAME')
->subject('Hello world!');
// Set the Cc addresses of this message.
$message->cc('[email protected]');
// Set the Bcc addresses of this message.
$message->bcc('[email protected]');
// Send message
$message->send();
mailer:beforeCreateMessage
mailer:afterCreateMessage
mailer:beforeSend
mailer:afterSend
mailer:beforeAttachFile
mailer:afterAttachFile