Ejemplo n.º 1
0
 /**
  * Sends an email message with attachments (if presented).
  * If $attachExternals is true, all external images in HTML mails
  * are downloaded and attached to the mail body itself.
  *
  * @param string $mail_text_with_headers  Headers are separated from body with newline.
  * @param array $attachments              See attachments format above.
  * @param bool $attachExternals           If true, download and attach extednal images.
  */
 function mail($mail, $attachments = null, $attachExternals = false)
 {
     if ($attachExternals) {
         list($mail, $attached) = Mail_Simple::_attach_externals($mail);
         if (!$attachments) {
             $attachments = array();
         }
         $attachments += $attached;
     }
     // Encode mail headers and body
     $mail = Mail_Simple::mailenc($mail);
     // Split the mail by headers and body.
     list($headers, $body) = preg_split("/\r?\n\r?\n/s", $mail, 2);
     $headers .= "\r\n";
     $overallHeaders = $headers;
     // Select "To".
     $to = "";
     if (preg_match('/\\A(.*)^To:\\s*([^\\r\\n]*)[\\r\\n]*(.*)\\Z/mis', $overallHeaders, $p)) {
         $to = $p[2];
         $overallHeaders = $p[1] . $p[3];
     }
     // Select "Return-Path".
     $retpath = "";
     if (preg_match('/\\A(.*)^Return-Path:\\s*([^\\r\\n]*)[\\r\\n]*(.*)\\Z/mis', $overallHeaders, $p)) {
         $retpath = $p[2];
         $overallHeaders = $p[1] . $p[3];
     }
     // Select "Subject".
     $subject = "";
     if (preg_match('/\\A(.*)^Subject:\\s*([^\\r\\n]*)[\\r\\n]*(.*)\\Z/mis', $overallHeaders, $p)) {
         $subject = $p[2];
         $overallHeaders = $p[1] . $p[3];
     }
     // Attachment processing.
     if ($attachments) {
         $multiparts = array();
         foreach ($attachments as $name => $attachment) {
             $file = null;
             $mime = 'application/octet-stream';
             $id = null;
             $data = null;
             if (is_array($attachment)) {
                 $file = isset($attachment['file']) ? $attachment['file'] : null;
                 if (isset($attachment['mime'])) {
                     $mime = $attachment['mime'];
                 }
                 if (isset($attachment['id'])) {
                     $id = $attachment['id'];
                 }
                 if (isset($attachment['data'])) {
                     $data = $attachment['data'];
                 }
             } else {
                 $file = $attachment;
             }
             if ($file !== null && !file_exists($file)) {
                 continue;
             }
             if ($data === null) {
                 $data = file_get_contents($file);
             }
             if (is_int($name)) {
                 $name = $file !== null ? basename($file) : $id;
             }
             $type = $id === null ? 'mixed' : 'related';
             $head = '';
             $head .= "Content-Type: {$mime}\r\n";
             $head .= "Content-Disposition: attachment; filename=" . addslashes($name) . "\r\n";
             $head .= "Content-Transfer-Encoding: base64\r\n";
             if ($id !== null) {
                 $head .= "Content-ID: <{$id}>\r\n";
             }
             $head .= "\r\n" . chunk_split(base64_encode($data));
             $multiparts[$type][] = $head;
         }
         // Related multiparts must always be situated on most depth.
         if (isset($multiparts['related'])) {
             $related = $multiparts['related'];
             unset($multiparts['related']);
             $multiparts = array('related' => $related) + $multiparts;
         }
         foreach ($multiparts as $type => $parts) {
             array_unshift($parts, $headers . "\r\n" . $body);
             $boundary = md5(uniqid(mt_rand(), true));
             $body = "--" . $boundary . "\r\n" . join("\r\n--" . $boundary . "\r\n", $parts) . "\r\n" . "--" . $boundary . "--\r\n";
             $headers = "Content-Type: multipart/{$type}; boundary={$boundary}\r\n";
         }
         $overallHeaders = preg_replace('/^Content-Type:\\s*.*?\\r?\\n/mix', '', $overallHeaders);
         $headers = $overallHeaders . $headers;
     } else {
         $headers = $overallHeaders;
     }
     // Remove \r (because GMail or DKIM could have conflict with them).
     $headers = str_replace("\r", "", trim($headers));
     $body = str_replace("\r", "", $body);
     // Send mail.
     $opt = null;
     if (preg_match('/<(.*?)>/s', $retpath, $m)) {
         $opt = "-f " . escapeshellarg($m[1]);
     }
     //var_dump($to, $subject, $body, trim($headers), $opt); die();
     mail($to, $subject, $body, $headers, $opt);
 }