Skip to contentSkip to navigationSkip to topbar
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:

1
mailer:
2
class: sfMailer
3
param:
4
logging: %SF_LOGGING_ENABLED%
5
charset: %SF_CHARSET%
6
delivery_strategy: realtime
7
transport:
8
class: Swift_SmtpTransport
9
param:
10
host: smtp.sendgrid.net
11
port: 587
12
encryption: ~
13
username: sendgridusername
14
api_key: sendgrid_api_key

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

1
<?php
2
$message = Swift_Message::newInstance()
3
->setFrom('from@example.com')
4
->setTo('to@example.com')
5
->setSubject('Subject')
6
->setBody('Body');
7
8
$this->getMailer()->send($message);
9
?>

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.

1
<?php
2
class myEmail
3
{
4
/**
5
* Library to facilitate email messages being sent out, sendMail deprecated in symfony 1.2
6
*
7
* @param string $partial - Array with html and text partials ie array('text'=>'textPartial', 'html'=>'htmlPartial')
8
* @param array $parameters - Array we will pass into the partials
9
* @param string $mailFrom - Email source
10
* @param string $mailTo - Email destination
11
* @param string $subject - The subject of the email message
12
* @param array $sgHeaders - What we will be placing in the SMTPAPI header. Must be null or a non-empty array
13
* @param array $attachments - Email contains the attachments
14
*/
15
16
public static function sendEmail($partials, $parameters, $mailFrom, $mailTo, $subject, $sgHeaders = null, $attachments = null)
17
{
18
// verify we have username/api_key to send out emails - IMPORTANT
19
if (!sfconfig::has('app_sendgrid_username') or !sfconfig::has('app_sendgrid_api_key')) {
20
throw new sfException('SMTP username/api_key is required to send email out');
21
}
22
$text = null;
23
$html = null;
24
if (is_array($partials)) {
25
// load libraries
26
sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
27
if (isset($partials['text'])) {
28
$text = get_partial($partials['text'], $parameters);
29
}
30
if (isset($partials['html'])) {
31
$html = get_partial($partials['html'], $parameters);
32
}
33
}
34
if ($text === null &amp;&amp; $html === null) {
35
throw new sfException('A text and/or HTML partial must be given');
36
}
37
38
try {
39
/*
40
* Load connection for mailer
41
*/
42
$connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl')->setUsername(sfconfig::get('app_sendgrid_username'))->setPassword(sfconfig::get('app_sendgrid_api_key'));
43
44
// setup connection/content
45
$mailer = Swift_Mailer::newInstance($connection);
46
$message = Swift_Message::newInstance()->setSubject($subject)->setTo($mailTo);
47
48
if ($text &amp;&amp; $html) {
49
$message->setBody($html, 'text/html');
50
$message->addPart($text, 'text/plain');
51
} else if ($text) {
52
$message->setBody($text, 'text/plain');
53
} else {
54
$message->setBody($html, 'text/html');
55
}
56
57
// if contains SMTPAPI header add it
58
if (null !== $sgHeaders) {
59
$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode($sgHeaders));
60
}
61
62
// update the from address line to include an actual name
63
if (is_array($mailFrom) and count($mailFrom) == 2) {
64
$mailFrom = array(
65
$mailFrom['email'] => $mailFrom['name']
66
);
67
}
68
69
// add attachments to email
70
if ($attachments !== null and is_array($attachments)) {
71
foreach ($attachments as $attachment) {
72
$attach = Swift_Attachment::fromPath($attachment['file'], $attachment['mime'])->setFilename($attachment['filename']);
73
$message->attach($attach);
74
}
75
}
76
77
// Send
78
$message->setFrom($mailFrom);
79
$mailer->send($message);
80
}
81
catch (Exception $e) {
82
throw new sfException('Error sending email out - ' . $e->getMessage());
83
}
84
}
85
}
86
?>

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

1
prod:
2
sendgrid:
3
username: sendgridusername
4
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:

1
apps/
2
frontend/
3
modules/
4
mail/
5
_registrationHTML.php
6
_registrationTEXT.php

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

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

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

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

And send the message as follows:

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

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.