Ejemplo n.º 1
0
 /**
  * Send email
  *
  * This is based on {@see wp_mail()} which is in turn based on PHP's
  * built-in mail() function. This is typically called from the overriden
  * version of {@see wp_mail()} below.
  *
  * @param string|array $to          Array or comma-separated list of email addresses to send message.
  * @param string       $subject     Email subject
  * @param string       $message     Message contents
  * @param string|array $headers     Optional. Additional headers.
  * @param string|array $attachments Optional. Files to attach.
  *
  * @return bool Whether the email contents were sent successfully.
  */
 function send_mail($to, $subject, $message, $headers = '', $attachments = array())
 {
     // Compact the input, apply the filters, and extract them back out
     extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
     global $phpmailer;
     if (!is_object($phpmailer) || !is_a($phpmailer, 'Message')) {
         $phpmailer = new Message();
     }
     $cc = array();
     $bcc = array();
     // Headers
     if (empty($headers)) {
         $headers = array();
     } else {
         if (!is_array($headers)) {
             // Explode the headers out, so this function can take both
             // string headers and an array of headers.
             $tempheaders = explode("\n", str_replace("\r\n", "\n", $headers));
         } else {
             $tempheaders = $headers;
         }
         $headers = array();
         // If it's actually got contents
         if (!empty($tempheaders)) {
             // Iterate through the raw headers
             foreach ((array) $tempheaders as $header) {
                 if (false === strpos($header, ':')) {
                     if (false !== stripos($header, 'boundary=')) {
                         $parts = preg_split('/boundary=/i', trim($header));
                         $boundary = trim(str_replace(array("'", '"'), '', $parts[1]));
                     }
                     continue;
                 }
                 // Explode them out
                 list($name, $content) = explode(':', trim($header), 2);
                 // Cleanup crew
                 $name = trim($name);
                 $content = trim($content);
                 switch (strtolower($name)) {
                     // Mainly for legacy -- process a From: header if it's there
                     case 'from':
                         if (false !== strpos($content, '<')) {
                             // So... making my life hard again?
                             $from_name = substr($content, 0, strpos($content, '<') - 1);
                             $from_name = str_replace('"', '', $from_name);
                             $from_name = trim($from_name);
                             $from_email = substr($content, strpos($content, '<') + 1);
                             $from_email = str_replace('>', '', $from_email);
                             $from_email = trim($from_email);
                         } else {
                             $from_email = trim($content);
                         }
                         break;
                     case 'content-type':
                         if (false !== strpos($content, ';')) {
                             list($type, $charset) = explode(';', $content);
                             $content_type = trim($type);
                             if (false !== stripos($charset, 'charset=')) {
                                 $charset = trim(str_replace(array('charset=', '"'), '', $charset));
                             } elseif (false !== stripos($charset, 'boundary=')) {
                                 $boundary = trim(str_replace(array('BOUNDARY=', 'boundary=', '"'), '', $charset));
                                 $charset = '';
                             }
                         } else {
                             $content_type = trim($content);
                         }
                         break;
                     case 'cc':
                         $cc = array_merge((array) $cc, explode(',', $content));
                         break;
                     case 'bcc':
                         $bcc = array_merge((array) $bcc, explode(',', $content));
                         break;
                     default:
                         // Add it to our grand headers array
                         $headers[trim($name)] = trim($content);
                         break;
                 }
             }
         }
     }
     // Empty out the values that may be set
     $phpmailer->clearBcc();
     $phpmailer->clearCc();
     $phpmailer->clearReplyTo();
     $phpmailer->clearTo();
     // From email and name
     // If we don't have a name from the input headers
     if (!isset($from_name)) {
         $from_name = 'App Engine';
     }
     /* If we don't have an email from the input headers default to wordpress@$sitename
      * Some hosts will block outgoing mail from this address if it doesn't exist but
      * there's no easy alternative. Defaulting to admin_email might appear to be another
      * option but some hosts may refuse to relay mail from an unknown domain. See
      * http://trac.wordpress.org/ticket/5007.
      */
     if (!isset($from_email)) {
         $from_email = get_option('appengine_email', false);
         if (!$from_email) {
             $from_email = get_default_email();
         }
     }
     // Plugin authors can override the potentially troublesome default
     // TODO: Currently, App Engine doesn't support a from name. We should
     //       come back to this and fix it if/when it does
     //$phpmailer->setSender( apply_filters( 'wp_mail_from_name', $from_name ) . " <" . apply_filters( 'wp_mail_from', $from_email ) . ">");
     $phpmailer->setSender(apply_filters('wp_mail_from', $from_email));
     // Set destination addresses
     if (!is_array($to)) {
         $to = explode(',', $to);
     }
     foreach ((array) $to as $recipient) {
         try {
             // Break $recipient into name and address parts if in the format "Foo <*****@*****.**>"
             $recipient_name = '';
             if (preg_match('/(.*)<(.+)>/', $recipient, $matches)) {
                 if (count($matches) == 3) {
                     $recipient_name = $matches[1];
                     $recipient = $matches[2];
                 }
             }
             $phpmailer->addTo($recipient, $recipient_name);
         } catch (Exception $e) {
             syslog(LOG_DEBUG, 'Mail error: ' . $e->getMessage());
             continue;
         }
     }
     // Add any CC and BCC recipients
     $cc = array_filter($cc);
     $bcc = array_filter($bcc);
     if (!empty($cc)) {
         foreach ((array) $cc as $recipient) {
             try {
                 // Break $recipient into name and address parts if in the format "Foo <*****@*****.**>"
                 $recipient_name = '';
                 if (preg_match('/(.*)<(.+)>/', $recipient, $matches)) {
                     if (count($matches) == 3) {
                         $recipient_name = $matches[1];
                         $recipient = $matches[2];
                     }
                 }
                 $phpmailer->addCc($recipient, $recipient_name);
             } catch (Exception $e) {
                 syslog(LOG_DEBUG, 'Mail error: ' . $e->getMessage());
                 continue;
             }
         }
     }
     if (!empty($bcc)) {
         foreach ((array) $bcc as $recipient) {
             try {
                 // Break $recipient into name and address parts if in the format "Foo <*****@*****.**>"
                 $recipient_name = '';
                 if (preg_match('/(.*)<(.+)>/', $recipient, $matches)) {
                     if (count($matches) == 3) {
                         $recipient_name = $matches[1];
                         $recipient = $matches[2];
                     }
                 }
                 $phpmailer->addBcc($recipient, $recipient_name);
             } catch (Exception $e) {
                 syslog(LOG_DEBUG, 'Mail error: ' . $e->getMessage());
                 continue;
             }
         }
     }
     // Set Content-Type and charset
     // If we don't have a content-type from the input headers
     if (!isset($content_type)) {
         $content_type = 'text/plain';
     }
     $content_type = apply_filters('wp_mail_content_type', $content_type);
     // Set whether it's plaintext, depending on $content_type
     if ('text/html' == $content_type) {
         $phpmailer->setHtmlBody($message);
     } else {
         $phpmailer->setTextBody($message);
     }
     $phpmailer->setSubject($subject);
     // If we don't have a charset from the input headers
     if (!isset($charset)) {
         $charset = get_bloginfo('charset');
     }
     // Set the content-type and charset
     //$phpmailer->charsset = apply_filters( 'wp_mail_charset', $charset );
     // Set custom headers
     if (!empty($headers)) {
         if (isset($headers['MIME-Version'])) {
             unset($headers['MIME-Version']);
         }
         $phpmailer->addHeaderArray($headers);
         if (false !== stripos($content_type, 'multipart') && !empty($boundary)) {
             $phpmailer->addHeaderArray('Content-Type', sprintf("%s;\n\t boundary=\"%s\"", $content_type, $boundary));
         }
     }
     if (!empty($attachments)) {
         foreach ($attachments as $attachment) {
             try {
                 $name = basename($attachment);
                 $data = file_get_contents($attachment);
                 $phpmailer->addAttachment($name, $data);
             } catch (Exception $e) {
                 syslog(LOG_DEBUG, 'Mail error: ' . $e->getMessage());
                 continue;
             }
         }
     }
     // Send!
     $phpmailer->send();
     return true;
 }