We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

paulbooker on 11/16/08


Tagged


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

jamesming
luman
baqc
rinx


Sending email with attachments


Published in: PHP 


  1. $to = ;
  2. $from = ;
  3. $subject = ;
  4. $message = ;
  5.  
  6. $headers = "From: $from";
  7.  
  8. // Generate a boundary string
  9. $semi_rand = md5(time());
  10. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  11.  
  12. // Add the headers for a file attachment
  13. $headers .= "\nMIME-Version: 1.0\n" .
  14. "Content-Type: multipart/mixed;\n" .
  15. " boundary=\"{$mime_boundary}\"";
  16.  
  17. // Add a multipart boundary above the plain message
  18. $message = "This is a multi-part message in MIME format.\n\n" .
  19. "--{$mime_boundary}\n" .
  20. "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  21. "Content-Transfer-Encoding: 7bit\n\n" .
  22. $message . "\n\n";
  23.  
  24. $data = $output ;
  25.  
  26. // Base64 encode the file data
  27. $data = chunk_split(base64_encode($data));
  28.  
  29. // Add file attachment to the message
  30. $message .= "--{$mime_boundary}\n" .
  31. "Content-Type: xml;\n" .
  32. " name=\"segala.rdf\"\n" .
  33. "Content-Disposition: attachment;\n" .
  34. " filename=\"segala.rdf\"\n" .
  35. "Content-Transfer-Encoding: base64\n\n" .
  36. $data . "\n\n" .
  37. "--{$mime_boundary}--\n";
  38.  
  39.  
  40. $posted = @mail($to, $subject, $message, $headers);

Report this snippet 

You need to login to post a comment.