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