Symfony World blog is not maintained anymore. Check new sys.exit() programming blog.

Symfony newsletter using Swift mailer

Scene from "Postman Pat" by John Cunliffe & Ivor Wood


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:

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())
      'The selected newsletter has already been sent.');
    $address_collection = Doctrine::getTable('Address')
    $addresses = array();
    foreach($address_collection as $address)
      $addresses[] = $address['address'];
    $message = $this->getMailer()->compose(
      array('' => 'sitename'),
      'The selected newsletter has been sent successfully.');
Implementing a newsletter with Symfony & Swift is really easy!


  1. 1k thx!
    just in time for me ;-))

  2. Thanks. Works for me. Ivan from Argentina