introduction
I had a task to implement a newsletter system for a sf 1.4 project. It was quite simple: an anonymous user from outside can subscribe to the newsletter (storing his E-mail address in the database). The site admin can create a newsletter article (with simple properties like title and content). When the send button is pressed, this article is sent to all subscribed E-mails.
output escaping
The newsletter article content value is modified using javascript rich editor such as tinyMCE or FCK Editor, so it has HTML tags like <p>
. I forced a problem that either Symfony or Swift was escaping all those HTML tags. I was trying to find solution using methods like getRawValue, getRaw or anything like that, but it turned out that it was Swift Mailer who was the cause of the problem. Following this small tutorial, I needed to add only one line of code:
$message->setContentType("text/html");
Now all formatting was shown properly.
final code solution
I hope that some of you may find this piece of code useful:
public function executeListSend()
{
$newsletter = $this->getRoute()->getObject();
if ($newsletter->getSent())
{
$this->getUser()->setFlash('notice',
'The selected newsletter has already been sent.');
}
else
{
$address_collection = Doctrine::getTable('Address')
->getAllAddressesQuery()
->fetchArray();
$addresses = array();
foreach($address_collection as $address)
$addresses[] = $address['address'];
$message = $this->getMailer()->compose(
array('name.surname@gmail.com' => 'sitename'),
$addresses,
$newsletter->getTitle(),
$newsletter->getContent()
);
$message->setContentType("text/html");
$this->getMailer()->send($message);
$newsletter->setSent(true);
$newsletter->save();
$this->getUser()->setFlash('notice',
'The selected newsletter has been sent successfully.');
}
$this->redirect('@newsletter');
}
Implementing a newsletter with Symfony & Swift is really easy!
1k thx!
ReplyDeletejust in time for me ;-))
Thanks. Works for me. Ivan from Argentina
ReplyDelete