How to use SwiftMailer in Phalcon?

By this thread:, 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)
    $mailer = Swift_Mailer::newInstance($transport);
    return $mailer;

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

edited Apr '16


how to code the var $messageInstance in the Controller?

For example:

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

I recommend creating factory like:

class MailFactory()
    public static function createMessage($from, $to, $subject, $body)
        return new Swift_Message()

And then in your controller:

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


Hi Wojciech Ślawski,

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