Exemplo n.º 1
0
 /**
  * Send the message.  If MAILER_RESULT_FAIL is returned, use GetErrors() to
  * determine the problem.
  *
  * @param EmailMessage $message
  * @return int results of operation (MAILER_RESULT_OK | MAILER_RESULT_FAIL)
  */
 function Send($message)
 {
     $mailer = new PHPMailer();
     // this prevents problems with phpmailer not being able to locate the language path
     $mailer->SetLanguage("en", $this->LangPath);
     $mailer->From = $message->From->Email;
     $mailer->FromName = $message->From->RealName;
     if ($message->ReplyTo) {
         $mailer->AddReplyTo($message->ReplyTo->Email, $message->ReplyTo->RealName);
     }
     $mailer->Subject = $message->GetSubject();
     $mailer->Body = $this->FixBareLB($message->GetBody());
     $mailer->ContentType = $message->Format == MESSAGE_FORMAT_TEXT ? "text/plain" : "text/html";
     $mailer->Mailer = strtolower($this->Method);
     $mailer->Host = $this->Path;
     $mailer->Sendmail = $this->Path;
     // use authentication if necessary
     if ($this->AuthUsername) {
         $mailer->SMTPAuth = true;
         $mailer->Username = $this->AuthUsername;
         $mailer->Password = $this->AuthPassword;
     }
     // if custom headers are to be provided, include them in the message
     foreach ($message->Headers as $header_key => $header_val) {
         $mailer->AddCustomHeader($header_key . ': ' . $header_val);
     }
     if ($message->Sender) {
         $this->_log[] = "Adding Sender " . $message->Sender;
         // phpmailer accepts this but it seems to not work consistently..?
         // $mailer->Sender = $message->Sender;
         // instead add the dang headers ourselves
         $mailer->AddCustomHeader("Sender: " . $message->Sender);
         $mailer->AddCustomHeader("Return-Path: " . $message->Sender);
     }
     if (!$this->IsValid($mailer->From)) {
         $this->_errors[] = "Sender '" . $mailer->From . "' is not a valid email address.";
         return MAILER_RESULT_FAIL;
     }
     // add the recipients
     foreach ($message->Recipients as $recipient) {
         $this->_log[] = "Adding Recipient " . $recipient->RealName . " [" . $recipient->Email . "]";
         if (!$this->IsValid($recipient->Email)) {
             $this->_errors[] = "Recipient '" . $recipient->Email . "' is not a valid email address.";
             return MAILER_RESULT_FAIL;
         }
         $mailer->AddAddress($recipient->Email, $recipient->RealName);
     }
     foreach ($message->CCRecipients as $recipient) {
         $this->_log[] = "Adding CC Recipient " . $recipient->RealName . " [" . $recipient->Email . "]";
         if (!$this->IsValid($recipient->Email)) {
             $this->_errors[] = "CC Recipient '" . $recipient->Email . "' is not a valid email address.";
             return MAILER_RESULT_FAIL;
         }
         $mailer->AddCC($recipient->Email, $recipient->RealName);
     }
     foreach ($message->BCCRecipients as $recipient) {
         $this->_log[] = "Adding BCC Recipient " . $recipient->RealName . " [" . $recipient->Email . "]";
         if (!$this->IsValid($recipient->Email)) {
             $this->_errors[] = "BCC Recipient '" . $recipient->Email . "' is not a valid email address.";
             return MAILER_RESULT_FAIL;
         }
         $mailer->AddBCC($recipient->Email, $recipient->RealName);
     }
     $result = MAILER_RESULT_OK;
     $this->_log[] = "Sending message using " . $mailer->Mailer;
     ob_start();
     // buffer output because class.phpmailer.php Send() is chatty and writes to stdout
     $fail = !$mailer->Send();
     ob_end_clean();
     // clear the buffer
     if ($fail || $mailer->ErrorInfo) {
         $result = MAILER_RESULT_FAIL;
         $this->_errors[] = trim(str_replace(array('<p>', '</p>'), array(', ', ''), $mailer->ErrorInfo));
     }
     return $result;
 }