Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Symfony


Symfony uses SwiftMailer to send email, read more about sending emails from Symfony(link takes you to an external page).

To get started you need to modify parameters.yml and add the following:


_14
mailer:
_14
class: sfMailer
_14
param:
_14
logging: %SF_LOGGING_ENABLED%
_14
charset: %SF_CHARSET%
_14
delivery_strategy: realtime
_14
transport:
_14
class: Swift_SmtpTransport
_14
param:
_14
host: smtp.sendgrid.net
_14
port: 587
_14
encryption: ~
_14
username: sendgridusername
_14
api_key: sendgrid_api_key

After that you should be able to send emails. The following shows an example:


_10
<?php
_10
$message = Swift_Message::newInstance()
_10
->setFrom('from@example.com')
_10
->setTo('to@example.com')
_10
->setSubject('Subject')
_10
->setBody('Body');
_10
_10
$this->getMailer()->send($message);
_10
?>


Another Option

another-option page anchor

If you want more flexibility, you can use partials to define the content of the emails. Add a class such as lib/myEmail.class.php.


_86
<?php
_86
class myEmail
_86
{
_86
/**
_86
* Library to facilitate email messages being sent out, sendMail deprecated in symfony 1.2
_86
*
_86
* @param string $partial - Array with html and text partials ie array('text'=>'textPartial', 'html'=>'htmlPartial')
_86
* @param array $parameters - Array we will pass into the partials
_86
* @param string $mailFrom - Email source
_86
* @param string $mailTo - Email destination
_86
* @param string $subject - The subject of the email message
_86
* @param array $sgHeaders - What we will be placing in the SMTPAPI header. Must be null or a non-empty array
_86
* @param array $attachments - Email contains the attachments
_86
*/
_86
_86
public static function sendEmail($partials, $parameters, $mailFrom, $mailTo, $subject, $sgHeaders = null, $attachments = null)
_86
{
_86
// verify we have username/api_key to send out emails - IMPORTANT
_86
if (!sfconfig::has('app_sendgrid_username') or !sfconfig::has('app_sendgrid_api_key')) {
_86
throw new sfException('SMTP username/api_key is required to send email out');
_86
}
_86
$text = null;
_86
$html = null;
_86
if (is_array($partials)) {
_86
// load libraries
_86
sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
_86
if (isset($partials['text'])) {
_86
$text = get_partial($partials['text'], $parameters);
_86
}
_86
if (isset($partials['html'])) {
_86
$html = get_partial($partials['html'], $parameters);
_86
}
_86
}
_86
if ($text === null &amp;&amp; $html === null) {
_86
throw new sfException('A text and/or HTML partial must be given');
_86
}
_86
_86
try {
_86
/*
_86
* Load connection for mailer
_86
*/
_86
$connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl')->setUsername(sfconfig::get('app_sendgrid_username'))->setPassword(sfconfig::get('app_sendgrid_api_key'));
_86
_86
// setup connection/content
_86
$mailer = Swift_Mailer::newInstance($connection);
_86
$message = Swift_Message::newInstance()->setSubject($subject)->setTo($mailTo);
_86
_86
if ($text &amp;&amp; $html) {
_86
$message->setBody($html, 'text/html');
_86
$message->addPart($text, 'text/plain');
_86
} else if ($text) {
_86
$message->setBody($text, 'text/plain');
_86
} else {
_86
$message->setBody($html, 'text/html');
_86
}
_86
_86
// if contains SMTPAPI header add it
_86
if (null !== $sgHeaders) {
_86
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode($sgHeaders));
_86
}
_86
_86
// update the from address line to include an actual name
_86
if (is_array($mailFrom) and count($mailFrom) == 2) {
_86
$mailFrom = array(
_86
$mailFrom['email'] => $mailFrom['name']
_86
);
_86
}
_86
_86
// add attachments to email
_86
if ($attachments !== null and is_array($attachments)) {
_86
foreach ($attachments as $attachment) {
_86
$attach = Swift_Attachment::fromPath($attachment['file'], $attachment['mime'])->setFilename($attachment['filename']);
_86
$message->attach($attach);
_86
}
_86
}
_86
_86
// Send
_86
$message->setFrom($mailFrom);
_86
$mailer->send($message);
_86
}
_86
catch (Exception $e) {
_86
throw new sfException('Error sending email out - ' . $e->getMessage());
_86
}
_86
}
_86
}
_86
?>

Then configure your credentials on apps/frontend/app.yml


_10
prod:
_10
sendgrid:
_10
username: sendgridusername
_10
password: sendgrid_api_key

Now can put your partials in a module such as apps/frontend/modules/mail. For example, to send a registration email in both text and HTML, we would have the following structure:


_10
apps/
_10
frontend/
_10
modules/
_10
mail/
_10
_registrationHTML.php
_10
_registrationTEXT.php

Add this to apps/frontend/modules/mail/_registrationTEXT.php


_10
Dear <!--?php echo $name ?-->,
_10
Thank you for registering. Please go to http://domain.com to finish your registration.

Add this to apps/frontend/modules/mail/_registrationHTML.php


_10
Dear <!--?php echo $name ?-->,
_10
Thank you for registering. Please go to <a href="http://domain.com">here</a> to finish your registration.

And send the message as follows:


_10
<?php
_10
myEmail::sendEmail(array('text'=>'mail/registrationTEXT', 'html'=>'mail/registrationHTML'), array('name'=>'Recipient Name'), 'youremail@domain.com', 'recipient@example.com', 'Registration Information');
_10
?>


Rate this page: