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

Swiftmaler in CLI task throws exception

Hi together,

in my cli.php i set the mail for my di like this:

 $di->set('mail', function () {
     return new Mail(); 
 });

In my task, I'ld like to send an email like this:

 $this->getDI()->get('mail')->sendMail(

             $this->di->get('config')->mail,

             [
                 $customerEmail => $customerEmail
             ],

             $this->di->get('translations')->_('foobar'),

             'mailTemplate',

             [
                 'foo' => 'bar'
             ]

         );

When executing the task, I receive this exception:

Catchable fatal error: Argument 1 passed to Swift_Mime_SimpleMessage::__construct() must be an instance of Swift_Mime_HeaderSet, none given

Any help?

Thank you so much

Christian



145.0k
Accepted
answer
edited Dec '15

What is mail new Mail () ? Your service for sending mails should like like this:

$di->set('mailer', function () use ($config) {
    include APPLICATION_PATH.'/../lib/swift/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 somewhere in task:

$this->di->get('mailer')->send($message)

Where $message is Swift_Message::newInstance.

Most of the time in creating message, rendering volt, and sending email is this last thing. Im creating message and rednering volt in request, and then i put this prepared message to queue and then in cli i just send it.

Thanks a lot! I was missing the require DIR . '/../vendor/swiftmailer/swiftmailer/lib/swift_required.php';

THANKS!