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);


8.1k

how to code the var $messageInstance in the Controller?

For example:

$message = new Swift_Message();
        $message->addFrom('xyz@xyz.com')
            ->addTo('abc@abc.com')
            ->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('xyz@xyz.com','abc@abc.om','test','test test test');


17.3k

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