* This article is part of the original Jobeet Tutorial, created by Fabien Potencier, for Symfony 1.4.
Yesterday, we added a read-only web service to Jobeet. Affiliates can now create an account but it needs to be activated by the administrator before it can be used. In order for the affiliate to get its token, we still need to implement the email notification. That’s what we will start doing in the coming lines.

The symfony framework comes bundled with one of the best PHP emailing solution: Swift Mailer. Of course, the library is fully integrated with symfony, with some cool features added on top of its default features. Let’s start by sending a simple email to notify the affiliate when his account has been activated and to give him the affiliate token. But first, you need to configure your environment:

For the code to work properly, you should change the address@example.com email address to a real one, along with your real password.

Do the same thing in your app/config/parameters_test.yml file.

After modifying the two files, clear the cache for both test and development environment:

Because we set the mailer transport to gmail, when you will replace the email address from “mailer_user”, you will put a google email address.

You can think of creating a Message as being similar to the steps you perform when you click the compose button in your mail client. You give it a subject, specify some recipients and write your message.

To create the message, you will:

  • call the newInstance() methond of Swift_message (refer to the Swift Mailer official documentation to learn more about this object).
  • set your sender address (From:) with setFrom() method.
  • set a subject line with setSubject() method.
  • set recipients with one of these methods: setTo(), setCc() or setBcc().
  • set a body with setBody().

Replace the activate action with the following code:

Sending the message is then as simple as calling the send() method on the mailer instance and passing the message as an argument.

For the message body, we created a new file, called email.txt.twig, that contains exactly what we want to inform the affiliate about.

Now, let’s add the mailing functionality to the batchActionActivate too, so that even if we select multiple affiliate accounts to activate, they will receive their account activation email :

The Tests

Now that we have seen how to send an email with the symfony mailer, let’s write some functional tests to ensure we did the right thing.

To test this new functionality, we need to be logged in. To log in, we will need an username and a password. That’s why we will start by creating a new fixture file, where we add the user admin:

In the tests, we will use the swiftmailer collector on the profiler to get information about the messages send on the previous requests. Now, let’s add some tests to check if the email is sent properly:

If you run the test now, you’ll get and error. To prevent this for happening, go to your config_test.yml file and make sure that the profiler is enabled in the test environment. If it’s set to false, change it to true:

Now, clear the cache, run the test command in your console and enjoy the green bar :

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.