Example #1
0
 public static function sendWithAttachments($to, $subject, $body, $attachments, $from = false, $headers = '', $sendmailParams = '-t -i')
 {
     if (!$from) {
         $from = static::getDefaultFrom();
     }
     if (is_array($headers)) {
         $headers = implode(PHP_EOL, $headers) . PHP_EOL;
     }
     if (is_array($to)) {
         $to = implode(', ', $to);
     }
     if ($from) {
         $headers .= "From: {$from}" . PHP_EOL;
         $fromAddress = preg_replace('/^[^<]*<([^>]+)>?$/', '$1', $from);
         $sendmailParams .= sprintf(' -f%1$s -F%1$s', $fromAddress);
     }
     $mimeBoundary = '==Multipart_Boundary_x' . md5(time()) . 'x';
     $headers .= 'MIME-Version: 1.0' . PHP_EOL;
     $headers .= 'Content-Type: multipart/mixed; boundary="' . $mimeBoundary . '"' . PHP_EOL;
     // plain text version
     $data = strip_tags($body) . PHP_EOL . PHP_EOL;
     $data .= '--' . $mimeBoundary . PHP_EOL;
     // html version
     $data .= 'Content-Type: text/html; charset=iso-8859-1' . PHP_EOL;
     $data .= 'Content-Transfer-Encoding: 7bit' . PHP_EOL . PHP_EOL;
     $data .= $body . PHP_EOL . PHP_EOL;
     $data .= '--' . $mimeBoundary . PHP_EOL;
     // attachments
     foreach ($attachments as $filename => $contents) {
         $cleanFilename = str_replace('"', '', $filename);
         $mimeType = File::getMIMETypeFromContents($contents);
         $data .= "Content-Type: {$mimeType}; name=\"{$cleanFilename}\"" . PHP_EOL;
         $data .= "Content-Disposition: attachment; filename=\"{$cleanFilename}\"" . PHP_EOL;
         $data .= 'Content-Transfer-Encoding: base64' . PHP_EOL . PHP_EOL;
         $data .= chunk_split(base64_encode($contents)) . PHP_EOL . PHP_EOL;
         $data .= '--' . $mimeBoundary . PHP_EOL;
     }
     return @mail($to, $subject, $data, $headers, $sendmailParams);
 }