Advertisement
michaelyuen

Email Attached php

Feb 6th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.69 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors',1); // enable php error display for easy trouble shooting
  3. error_reporting(E_ALL); // set error display to all
  4.  
  5.     $to = "recipient@domain.com"; // change accordingly
  6.     $from ="sender@domain.com"; // change accordingly
  7.     $subject = "Your Email Subject"; // change accordingly
  8.     $bodytext = "Your Email Message"; // change accordingly
  9.     $filepath = "image.jpg"; // Path to the file (example)
  10.     $filetype = "application/jpg"; // File Type
  11.     $filename = "image.jpg"; // Filename that will be used for the file as the attachment
  12.  
  13.     // boundary
  14.     $semi_rand = md5(time());
  15.     $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  16.  
  17.     $headers= "From:" . $from;
  18.    
  19.     // headers for attachment
  20.     $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
  21.  
  22.     // multipart boundary
  23.     $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $bodytext . "\n\n";
  24.     $message .= "--{$mime_boundary}\n";  
  25.    
  26.     $file = fopen($filepath,'rb');
  27.     $data = fread($file,filesize($filepath));
  28.     fclose($file);
  29.     $data = chunk_split(base64_encode($data));
  30.     $message .= "Content-Type: {'application/octet-stream'};\n" . " name=\"$filename\"\n" .
  31.     "Content-Disposition: attachment;\n" . " filename=\"$filename\"\n" .
  32.     "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  33.     $message .= "--{$mime_boundary}\n";
  34.    
  35.     if (mail($to,$subject,$message,$headers)) {
  36.     echo 'Successfully Sent';
  37.     } else {
  38.     echo 'Message Sending Failed';
  39.     }
  40.  
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement