Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 SwiftMimeSimpleMessage::_construct() must be an instance of SwiftMime_HeaderSet, none given

Any help?

Thank you so much

Christian



99.6k
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!