Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 2.55 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP mail() attachment problems
  2. $to = "myemail@mydomain.com";
  3. $from = "Website <website@mydomain.com>";
  4. $subject = "Test Attachment Email";
  5.  
  6. $separator = md5(time());
  7.  
  8. // carriage return type (we use a PHP end of line constant)
  9. $eol = PHP_EOL;
  10.  
  11. // attachment name
  12. $filename = "document.pdf";
  13.  
  14. //$pdfdoc is PDF generated by FPDF
  15. $attachment = chunk_split(base64_encode($pdfdoc));
  16.  
  17. // main header
  18. $headers  = "From: ".$from.$eol;
  19. $headers .= "MIME-Version: 1.0".$eol;
  20. $headers .= "Content-Type: multipart/mixed; boundary="".$separator.""".$eol.$eol;
  21. $headers .= "Content-Transfer-Encoding: 7bit".$eol;
  22. $headers .= "This is a MIME encoded message.".$eol.$eol;
  23.  
  24. // message
  25. $headers .= "--".$separator.$eol;
  26. $headers .= "Content-Type: text/html; charset="iso-8859-1"".$eol;
  27. $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  28. $headers .= $message.$eol.$eol;
  29.  
  30. // attachment
  31. $headers .= "--".$separator.$eol;
  32. $headers .= "Content-Type: application/octet-stream; name="".$filename.""".$eol;
  33. $headers .= "Content-Transfer-Encoding: base64".$eol;
  34. $headers .= "Content-Disposition: attachment".$eol.$eol;
  35. $headers .= $attachment.$eol.$eol;
  36. $headers .= "--".$separator."--";
  37.  
  38. // send message
  39. if (mail($to, $subject, "", $headers)) {
  40.     echo "mail send ... OK";
  41. } else {
  42.     echo "mail send ... ERROR";
  43. }
  44.        
  45. $to = "myemail@mydomain.com";
  46. $from = "Website <website@mydomain.com>";
  47. $subject = "Test Attachment Email";
  48.  
  49. $separator = md5(time());
  50.  
  51. // carriage return type (we use a PHP end of line constant)
  52. $eol = PHP_EOL;
  53.  
  54. // attachment name
  55. $filename = "document.pdf";
  56.  
  57. //$pdfdoc is PDF generated by FPDF
  58. $attachment = chunk_split(base64_encode($pdfdoc));
  59.  
  60. // main header
  61. $headers  = "From: ".$from.$eol;
  62. $headers .= "MIME-Version: 1.0".$eol;
  63. $headers .= "Content-Type: multipart/mixed; boundary="".$separator.""";
  64.  
  65. // no more headers after this, we start the body! //
  66.  
  67. $body = "--".$separator.$eol;
  68. $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
  69. $body .= "This is a MIME encoded message.".$eol;
  70.  
  71. // message
  72. $body .= "--".$separator.$eol;
  73. $body .= "Content-Type: text/html; charset="iso-8859-1"".$eol;
  74. $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
  75. $body .= $message.$eol;
  76.  
  77. // attachment
  78. $body .= "--".$separator.$eol;
  79. $body .= "Content-Type: application/octet-stream; name="".$filename.""".$eol;
  80. $body .= "Content-Transfer-Encoding: base64".$eol;
  81. $body .= "Content-Disposition: attachment".$eol.$eol;
  82. $body .= $attachment.$eol;
  83. $body .= "--".$separator."--";
  84.  
  85. // send message
  86. if (mail($to, $subject, $body, $headers)) {
  87.     echo "mail send ... OK";
  88. } else {
  89.     echo "mail send ... ERROR";
  90. }