How to use SwiftMailer in Phalcon?

By this thread: https://forum.phalconphp.com/discussion/9243/which-mailer-and-how-to-install, I set up $di like this:

$di->set('swiftmailer',function() use ($config) {
   include 'path to swift_required.php';
    $transport = Swift_SmtpTransport::newInstance($config->smtp->address,$config->smtp->port)
        ->setUsername($config->smtp->username)
        ->setPassword($config->smtp->password);
    $mailer = Swift_Mailer::newInstance($transport);
    return $mailer;
});

And then, How to send a mail in the Controllers?

edited Apr '16
$this->di->get('swiftmailer)->send($messageInstance);


9.3k

how to code the var $messageInstance in the Controller?

For example:

$message = new Swift_Message();
        $message->addFrom('[email protected]')
            ->addTo('[email protected]')
            ->setSubject('test')
            ->setBody('test test test');

I recommend creating factory like:

class MailFactory()
{
    public static function createMessage($from, $to, $subject, $body)
    {
        return new Swift_Message()
        ->addFrom($from)
        ->addTo($to)
        ->setSubject($subject)
        ->setBody($body);
    }
}

And then in your controller:

$message = MailFactory::createMessage('[email protected]','[email protected]','test','test test test');


19.5k

Hi Wojciech Ślawski,

that is also my problem, i have never get run Swiftmailer. Do you have a complete example of your MailFactory?

Rgds

Stefan