We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to use SwiftMailer in Phalcon?

By this thread: https://forum.phalcon.io/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);


31.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');


59.9k

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