/** * Send an email with HTML content. * * @see sendPlain() for sending plaintext emails only. * @uses Mailer->sendHTML() * * @param string $messageID Optional message ID so the message can be identified in bounces etc. * @return bool Success of the sending operation from an MTA perspective. * Doesn't actually give any indication if the mail has been delivered to the recipient properly) */ public function send($messageID = null) { // Check required objects if ($this->required_objects) { foreach ($this->required_objects as $reqName => $reqClass) { if ($reqName == 'Member' && !$this->templateData()->{$reqName}) { $this->templateData()->{$reqName} = Member::currentUser(); } if ($reqName == 'SiteConfig' && !$this->templateData()->{$reqName}) { $this->templateData()->{$reqName} = SiteConfig::current_site_config(); } if (!$this->templateData()->{$reqName}) { throw new Exception('Required object ' . $reqName . ' of class ' . $reqClass . ' is not defined in template data'); } } } // Check for Subject if (!$this->subject) { throw new Exception('You must set a subject'); } $this->from = MandrillMailer::resolveDefaultFromEmail($this->from); if (!$this->from) { throw new Exception('You must set a sender'); } if ($this->to_member && !$this->to) { // Include name in to as standard rfc $this->to = $this->to_member->FirstName . ' ' . $this->to_member->Surname . ' <' . $this->to_member->Email . '>'; } $this->to = MandrillMailer::resolveDefaultToEmail($this->to); if (!$this->to) { throw new Exception('You must set a recipient'); } // Set language to use for the email $restore_locale = null; if ($this->locale) { $restore_locale = i18n::get_locale(); i18n::set_locale($this->locale); } if ($this->to_member) { // If no locale is defined, use Member locale if ($this->to_member->Locale && !$this->locale) { $restore_locale = i18n::get_locale(); i18n::set_locale($this->to_member->Locale); } } $res = parent::send($messageID); if ($restore_locale) { i18n::set_locale($restore_locale); } return $res; }
/** * Basic functionality to allow sending of emails using templates up in Mandrill by using the * sendTemplate function of the Messages class in the mailer. * @param string templateName the name of the template in mandril. * @param array globalMergeVars * @param string $messageID Optional message ID so the message can be identified in bounces etc. * @return bool Success result of the sending operation. */ public function sendTemplate($templateName, $globalMergeVars = null, $messageID = null) { // @TODO allow non-global merge vars and possibly impliment other features of sendTemplate. // Do some checks that required things are set. if (!$templateName) { throw new Exception('You must set a template'); } if (!$this->subject) { throw new Exception('You must set a subject'); } $this->from = MandrillMailer::resolveDefaultFromEmail($this->from); if (!$this->from) { throw new Exception('You must set a sender'); } if ($this->to_member && !$this->to) { // Include name in to as standard rfc $this->to = $this->to_member->FirstName . ' ' . $this->to_member->Surname . ' <' . $this->to_member->Email . '>'; } $this->to = MandrillMailer::resolveDefaultToEmail($this->to); if (!$this->to) { throw new Exception('You must set a recipient'); } // Need to call the sendTemplate function in the mailer which in turn calls // the mandrill->messages->sendTemplate() to make the sendTemplate API call. return self::mailer()->sendTemplate($templateName, $globalMergeVars, $this->to, $this->from, $this->subject, $this->customHeaders); }