Advertisement
puggan

mail attachments

Aug 11th, 2011
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. function send($subject, $body, $to, $from = 'utskick@faceways.se', $attachments = NULL, $html = TRUE, $charset = "iso-8859-1")
  2.         {
  3.             /* Set the SMTP headers for recipient and senders. */
  4.             $headers  = "From: {$from}\n";
  5.             $headers .= "Reply-To: {$from}\n";
  6.  
  7.             /* Generate a unique boundary string. */
  8.             $BREAK_BOUNDARY = md5('BREAK' . date('Y-m-d H:i:s'));
  9.  
  10.             /* Set the SMTP headers to use HTML. */
  11.             $headers .= "MIME-Version: 1.0\n";
  12.             $headers .= "Content-Type: multipart/mixed; boundary=\"{$BREAK_BOUNDARY}\"\n";
  13.  
  14.             /* Set the language of the content. */
  15.             $headers .= "Content-Language: sv-SE\n";
  16.  
  17.             /* Set the mail client field. */
  18.             $headers .= "X-Mailer: Interfaceways v1.x\n";
  19.  
  20.             /* Mark the end of the headers. */
  21.             $headers .= "\n";
  22.  
  23.             /* Introduce the message for those who do not have a capable client. */
  24.             $message  = "Det här är ett flerdelat meddelande i MIME-format.\n";
  25.             $message .= "\n";
  26.  
  27.             /* Split the message. */
  28.             $message .= "--{$BREAK_BOUNDARY}\n";
  29.  
  30.             if($html)
  31.             {
  32.                 /* Set message headers for the HTML version of the message. */
  33.                 $message .= "Content-Type: text/html; charset={$charset}\n";
  34.                 $message .= "Content-Transfer-Encoding: 7bit\n";
  35.                 $message .= "\n";
  36.  
  37.                 /* Wrap the message in a html tag-set. */
  38.                 $message .= "<html><head><title>{$subject}</title></head><body>\n";
  39.                 $message .= "{$body}\n";
  40.                 $message .= "</body></html>\n";
  41.                 $message .= "\n";
  42.             }
  43.             else
  44.             {
  45.                 /* Set message headers for the HTML version of the message. */
  46.                 $message .= "Content-Type: text/plain; charset={$charset}\n";
  47.                 $message .= "Content-Transfer-Encoding: 7bit\n";
  48.                 $message .= "\n";
  49.  
  50.                 /* Set the message text. */
  51.                 $message .= "{$body}\n";
  52.                 $message .= "\n";
  53.             }
  54.  
  55.             /* Create a resource to PECL Fileinfo. */
  56.             //$file_info = finfo_open(FILEINFO_MIME);
  57.  
  58.             /* Iterate over each file attachment. */
  59.             foreach((array) $attachments as $attachment)
  60.             {
  61.                 /* Split the message. */
  62.                 $message .= "--{$BREAK_BOUNDARY}\n";
  63.  
  64.                 /* If the attachment is a file.. */
  65.                 if(file_exists($attachment))
  66.                 {
  67.                     /* moved in here */
  68.                     $file_info = finfo_open(FILEINFO_MIME);
  69.  
  70.                     /* Read the file mime type, name etc. */
  71.                     $file_mime_type = finfo_file($file_info, $attachment);
  72.                     $file_name = basename($attachment);
  73.  
  74.                     /* moved in here */
  75.                     finfo_close($file_info);
  76.  
  77.                     /* Set the headers for the file attachment. */
  78.                     $message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
  79.                     $message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
  80.                     $message .= "Content-Transfer-Encoding: base64\n";
  81.                     $message .= "\n";
  82.  
  83.                     /* Encode the attached file into base64 and split it up to fit on a line. */
  84.                     $message .= chunk_split(base64_encode(file_get_contents($file)));
  85.                     $message .= "\n";
  86.                 }
  87.                 else
  88.                 {
  89.                     /* Read the file mime type, name etc. */
  90.                     $file_mime_type = $attachment['file_mime_type'];
  91.                     $file_name = $attachment['file_name'];
  92.  
  93.                     /* Set the headers for the file attachment. */
  94.                     $message .= "Content-Type: {$file_mime_type}; name=\"{$file_name}\"\n";
  95.                     $message .= "Content-Disposition: attachment; filename=\"{$file_name}\"\n";
  96.                     $message .= "Content-Transfer-Encoding: base64\n";
  97.                     $message .= "\n";
  98.  
  99.                     /* Encode the attached file into base64 and split it up to fit on a line. */
  100.                     $message .= chunk_split(base64_encode($attachment['data']));
  101.                     $message .= "\n";
  102.                 }
  103.             }
  104.  
  105.             /* Free the resource to PECL Fileinfo. */
  106.             //finfo_close($file_info);
  107.  
  108.             /* End the message. */
  109.             $message .= "--{$BREAK_BOUNDARY}--\n";
  110.  
  111.             /* Send the actual message. */
  112.             mail($to, $subject, $message, $headers);
  113.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement