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:
_14mailer:_14class: sfMailer_14param:_14logging: %SF_LOGGING_ENABLED%_14charset: %SF_CHARSET%_14delivery_strategy: realtime_14transport:_14class: Swift_SmtpTransport_14param:_14host: smtp.sendgrid.net_14port: 587_14encryption: ~_14username: sendgridusername_14api_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?>
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_86class 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 && $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 && $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
_10prod:_10sendgrid:_10username: sendgridusername_10password: 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:
_10apps/_10frontend/_10modules/_10mail/_10_registrationHTML.php_10_registrationTEXT.php
Add this to apps/frontend/modules/mail/_registrationTEXT.php
_10Dear <!--?php echo $name ?-->,_10Thank you for registering. Please go to http://domain.com to finish your registration.
Add this to apps/frontend/modules/mail/_registrationHTML.php
_10Dear <!--?php echo $name ?-->,_10Thank 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_10myEmail::sendEmail(array('text'=>'mail/registrationTEXT', 'html'=>'mail/registrationHTML'), array('name'=>'Recipient Name'), 'youremail@domain.com', 'recipient@example.com', 'Registration Information');_10?>