If you had used Symfony 1.2 with Swift 3 before, you probably sent emails like this:
try
{
$mailer = new Swift(new Swift_Connection_NativeMail());
$message = new Swift_Message('The subject', 'The body html', 'text/html');
$mailer->send($message, 'to@user.com', 'noreply@company.com');
$mailer->disconnect();
}
catch (Exception $e)
{
$mailer->disconnect();
}
However, due to API changes in Swift 4, that would have thrown an error:
Fatal error: Cannot instantiate abstract class Swift in /[...]/apps/frontend/modules/[...]/actions/actions.class.php on line 40
It turns out that now the class Swift is defined as abstract, which means it can’t be instantiated. Now, classes that you used to instantiate directly, like Swift_Message, have been added factory methods called newInstance
require_once('lib/vendor/swift/swift_init.php'); # needed due to symfony autoloader
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
$message = Swift_Message::newInstance('The subject')
->setFrom(array('noreply@company.com' => 'Mailer Name'))
->setTo(array('email@email.com' => 'Name Lastname'))
->setBody('The body html', 'text/html');
$mailer->send($message);
Remember that it’s good practise to get the body HTML from a partial, instead of hardcoding it into the action.
$mailBody = $this->getPartial('emailPartial', array(/* parameters */));
// ...
->setBody($mailBody, 'text/html');
The code has become a lot more readable, easier to customize and write.
More information here and here.
9 Comments
Thanks a lot for this advice
I read this same article somwhere else but I’m not sure it was a planet or a repost.
Anyway, I added the include thingy in the projectConfiguration file (or a plugin one, depending of the app architecture) to avoid this in action/task/lib.
With swiftmailer in the lib/vendors and this, it works like a charm.
It’s definitely a good library
Thanks works perferctly!
This thread also came in handy for setting up:
http://forum.symfony-project.org/index.php/m/77609/
Thanks Guillermo! I landed here after a search for “sending emails symfony” and it was the first hit (and appropriately so). Worked great. I must have missed the memo about this change
I’m new to using both Symfony and Swift 4, but I think Swift made some changes because I had errors until I changed “require_once(‘lib/vendor/swift/swift_init.php’)” to “require_once(‘lib/vendor/swift/swift_required.php’)”.
Just to let you know, I got a blank page sending my last comment.
Cheers!
Hi,
Thanks you for your tuto.
I desperatly tries to send email via task with zend_mail. It’s says evrything is ok but no mail receiving.
It is possible for you to show how to send email via task with swift please
Thanks
Hello swantosad
http://www.dxbline.com/ – cheap propecia
Some experts also prefer it over Minoxidil.
buy propecia
It is a generic drug and is available under two brands names in the USA, Proscar and Propecia.
finasteride online
But experts say that it works better if administered at the early stages of hair loss.
Hey! Just wanted to say Hi!
________
http://windowserrorcodes.net