Symfony uses SwiftMailer to send email, read more about sending emails from Symfony.
To get started you need to modify parameters.yml and add the following:
1mailer:2class: sfMailer3param:4logging: %SF_LOGGING_ENABLED%5charset: %SF_CHARSET%6delivery_strategy: realtime7transport:8class: Swift_SmtpTransport9param:10host: smtp.sendgrid.net11port: 58712encryption: ~13username: sendgridusername14api_key: sendgrid_api_key
After that you should be able to send emails. The following shows an example:
1<?php2$message = Swift_Message::newInstance()3->setFrom('from@example.com')4->setTo('to@example.com')5->setSubject('Subject')6->setBody('Body');78$this->getMailer()->send($message);9?>
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<?php2class myEmail3{4/**5* Library to facilitate email messages being sent out, sendMail deprecated in symfony 1.26*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 partials9* @param string $mailFrom - Email source10* @param string $mailTo - Email destination11* @param string $subject - The subject of the email message12* @param array $sgHeaders - What we will be placing in the SMTPAPI header. Must be null or a non-empty array13* @param array $attachments - Email contains the attachments14*/1516public static function sendEmail($partials, $parameters, $mailFrom, $mailTo, $subject, $sgHeaders = null, $attachments = null)17{18// verify we have username/api_key to send out emails - IMPORTANT19if (!sfconfig::has('app_sendgrid_username') or !sfconfig::has('app_sendgrid_api_key')) {20throw new sfException('SMTP username/api_key is required to send email out');21}22$text = null;23$html = null;24if (is_array($partials)) {25// load libraries26sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');27if (isset($partials['text'])) {28$text = get_partial($partials['text'], $parameters);29}30if (isset($partials['html'])) {31$html = get_partial($partials['html'], $parameters);32}33}34if ($text === null && $html === null) {35throw new sfException('A text and/or HTML partial must be given');36}3738try {39/*40* Load connection for mailer41*/42$connection = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 465, 'ssl')->setUsername(sfconfig::get('app_sendgrid_username'))->setPassword(sfconfig::get('app_sendgrid_api_key'));4344// setup connection/content45$mailer = Swift_Mailer::newInstance($connection);46$message = Swift_Message::newInstance()->setSubject($subject)->setTo($mailTo);4748if ($text && $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}5657// if contains SMTPAPI header add it58if (null !== $sgHeaders) {59$message->getHeaders()->addTextHeader('X-SMTPAPI', json_encode($sgHeaders));60}6162// update the from address line to include an actual name63if (is_array($mailFrom) and count($mailFrom) == 2) {64$mailFrom = array(65$mailFrom['email'] => $mailFrom['name']66);67}6869// add attachments to email70if ($attachments !== null and is_array($attachments)) {71foreach ($attachments as $attachment) {72$attach = Swift_Attachment::fromPath($attachment['file'], $attachment['mime'])->setFilename($attachment['filename']);73$message->attach($attach);74}75}7677// Send78$message->setFrom($mailFrom);79$mailer->send($message);80}81catch (Exception $e) {82throw new sfException('Error sending email out - ' . $e->getMessage());83}84}85}86?>
Then configure your credentials on apps/frontend/app.yml
1prod:2sendgrid:3username: sendgridusername4password: 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:
1apps/2frontend/3modules/4mail/5_registrationHTML.php6_registrationTEXT.php
Add this to apps/frontend/modules/mail/_registrationTEXT.php
1Dear <!--?php echo $name ?-->,2Thank you for registering. Please go to http://domain.com to finish your registration.
Add this to apps/frontend/modules/mail/_registrationHTML.php
1Dear <!--?php echo $name ?-->,2Thank you for registering. Please go to <a href="http://domain.com">here</a> to finish your registration.
And send the message as follows:
1<?php2myEmail::sendEmail(array('text'=>'mail/registrationTEXT', 'html'=>'mail/registrationHTML'), array('name'=>'Recipient Name'), 'youremail@domain.com', 'recipient@example.com', 'Registration Information');3?>