How to send an Email
Edit this pageWarning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.
Read the updated version of this page for Symfony 6.3 (the current stable version).
How to send an Email
Sending emails is a classic task for any web application and one that has
special complications and potential pitfalls. Instead of recreating the wheel,
one solution to send emails is to use the SwiftmailerBundle
, which leverages
the power of the Swiftmailer library.
Note
Don't forget to enable the bundle in your kernel before using it:
1 2 3 4 5 6 7 8 9 10
public function registerBundles()
{
$bundles = array(
// ...
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
);
// ...
}
Configuration
Before using Swiftmailer, be sure to include its configuration. The only
mandatory configuration parameter is transport
:
1 2 3 4 5 6 7 8
# app/config/config.yml
swiftmailer:
transport: smtp
encryption: ssl
auth_mode: login
host: smtp.gmail.com
username: your_username
password: your_password
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!-- app/config/config.xml -->
<!--
xmlns:swiftmailer="http://symfony.com/schema/dic/swiftmailer"
http://symfony.com/schema/dic/swiftmailer http://symfony.com/schema/dic/swiftmailer/swiftmailer-1.0.xsd
-->
<swiftmailer:config
transport="smtp"
encryption="ssl"
auth-mode="login"
host="smtp.gmail.com"
username="your_username"
password="your_password" />
1 2 3 4 5 6 7 8 9
// app/config/config.php
$container->loadFromExtension('swiftmailer', array(
'transport' => "smtp",
'encryption' => "ssl",
'auth_mode' => "login",
'host' => "smtp.gmail.com",
'username' => "your_username",
'password' => "your_password",
));
The majority of the Swiftmailer configuration deals with how the messages themselves should be delivered.
The following configuration attributes are available:
transport
(smtp
,mail
,sendmail
, orgmail
)username
password
host
port
encryption
(tls
, orssl
)auth_mode
(plain
,login
, orcram-md5
)spool
type
(how to queue the messages, onlyfile
is supported currently)path
(where to store the messages)
delivery_address
(an email address where to send ALL emails)disable_delivery
(set to true to disable delivery completely)
Sending Emails
The Swiftmailer library works by creating, configuring and then sending
Swift_Message
objects. The "mailer" is responsible for the actual delivery
of the message and is accessible via the mailer
service. Overall, sending
an email is pretty straightforward:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public function indexAction($name)
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send@example.com')
->setTo('recipient@example.com')
->setBody(
$this->renderView(
'HelloBundle:Hello:email.txt.twig',
array('name' => $name)
)
)
;
$this->get('mailer')->send($message);
return $this->render(...);
}
To keep things decoupled, the email body has been stored in a template and
rendered with the renderView()
method.
The $message
object supports many more options, such as including attachments,
adding HTML content, and much more. Fortunately, Swiftmailer covers the topic
of Creating Messages in great detail in its documentation.
Tip
Several other cookbook articles are available related to sending emails in Symfony2: