Пример #1
0
 /**
  * Send the mail.
  *
  * @param Midas_Mail $mail mail instance
  * @throws Midas_Service_AppEngine_Exception
  */
 public function sendMail(Midas_Mail $mail)
 {
     if (is_null($this->_client)) {
         $this->_client = new \google\appengine\api\mail\Message();
     }
     $this->_client->addBcc($mail->getBcc());
     $this->_client->addCc($mail->getCc());
     $this->_client->addTo($mail->getTo());
     $this->_client->setHtmlBody($mail->getBodyHtml(true));
     $this->_client->setReplyTo($mail->getReplyTo());
     $this->_client->setSender($mail->getFrom());
     $this->_client->setSubject($mail->getSubject());
     $this->_client->setTextBody($mail->getBodyText(true));
     try {
         $this->_client->send();
     } catch (\Exception $exception) {
         throw new Midas_Service_AppEngine_Exception($exception);
     }
 }
Пример #2
0
 public function testSetReplyTo()
 {
     $message = new Message();
     $this->setExpectedException("InvalidArgumentException", "Invalid reply-to: invalid.email");
     $message->setReplyTo("invalid.email");
 }
Пример #3
0
 /**
  * Send an email.
  *
  * This is a re-implementation of PHP's mail() function using App Engine
  * mail API. The function relies on mailparse extension to parse emails.
  *
  * @param string $to Receiver, or receivers of the mail.
  * @param string $subject Subject of the email to be sent.
  * @param string $message Message to be sent.
  * @param string $additional_headers optional
  *   String to be inserted at the end of the email header.
  * @param string $additional_parameters optional
  *   Additional flags to be passed to the mail program. This arugment is
  *   added only to match the signature of PHP's mail() function. The value is
  *   always ignored.
  * @return bool
  *   TRUE if the message is sent successfully, otherwise return FALSE.
  *
  * @see http://php.net/mail
  */
 public static function sendMail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
 {
     $raw_mail = "To: {$to}\r\nSubject: {$subject}\r\n";
     if ($additional_headers != null) {
         $raw_mail .= trim($additional_headers);
     }
     $raw_mail .= "\r\n\r\n{$message}";
     $mime = mailparse_msg_create();
     mailparse_msg_parse($mime, $raw_mail);
     $root_part = mailparse_msg_get_part_data($mime);
     // Set sender address based on the following order
     // 1. "From" header in $additional_headers
     // 2. "sendmail_from" ini setting
     // 3. Default address "mailer@<app-id>.appspotmail.com
     $from = ini_get('sendmail_from');
     if (isset($root_part['headers']['from'])) {
         $from = $root_part['headers']['from'];
     }
     if ($from === false || $from == "") {
         $from = sprintf(self::DEFAULT_SENDER_ADDRESS_FORMAT, AppIdentityService::getApplicationId());
         syslog(LOG_WARNING, "mail(): Unable to determine sender's email address from the " . "'sendmail_from' directive in php.ini or from the 'From' " . "header. Falling back to the default {$from}.");
     }
     $email = new Message();
     try {
         $email->setSender($from);
         $email->addTo($root_part['headers']['to']);
         if (isset($root_part['headers']['cc'])) {
             $email->AddCc($root_part['headers']['cc']);
         }
         if (isset($root_part['headers']['bcc'])) {
             $email->AddBcc($root_part['headers']['bcc']);
         }
         if (isset($root_part['headers']['reply-to'])) {
             $email->setReplyTo($root_part['headers']['reply-to']);
         }
         $email->setSubject($root_part['headers']['subject']);
         $parts = mailparse_msg_get_structure($mime);
         if (count($parts) > 1) {
             foreach ($parts as $part_id) {
                 $part = mailparse_msg_get_part($mime, $part_id);
                 self::parseMimePart($part, $raw_mail, $email);
             }
         } else {
             if ($root_part['content-type'] == 'text/plain') {
                 $email->setTextBody($message);
             } else {
                 if ($root_part['content-type'] == 'text/html') {
                     $email->setHtmlBody($message);
                 }
             }
         }
         $extra_headers = array_diff_key($root_part['headers'], array_flip(['from', 'to', 'cc', 'bcc', 'reply-to', 'subject', 'content-type']));
         foreach ($extra_headers as $key => $value) {
             try {
                 $email->addHeader($key, $value);
             } catch (\InvalidArgumentException $e) {
                 syslog(LOG_WARNING, "mail:() Dropping disallowed email header {$key}");
             }
         }
         $email->send();
     } catch (\Exception $e) {
         trigger_error('mail(): ' . $e->getMessage(), E_USER_WARNING);
         return false;
     }
     return true;
 }