web 2.0

How to use PHP & Send Email Attachment File

How to use PHP & Send Email Attachment File This the tutorial/example a scripts how to use PHP and send mail/email and  Attachment File from php Scirpt.

ShotDev Focus:
- PHP & Send Mail (Attachment File)

Example 1

php_sendmail_attachment1.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $strTo = "Mr.Member ShotDev.Com<member@shotdev.com>";  
  8. $strSubject = "Test sending mail.";  
  9. $strMessage = "My Body & <b>My Description</b>";  
  10.   
  11. //*** Uniqid Session ***//  
  12. $strSid = md5(uniqid(time()));  
  13.   
  14. $strHeader = "";  
  15. $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";  
  16. $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>\n";  
  17. $strHeader .= "Bcc: webmaster@shotdev.com";  
  18.   
  19. $strHeader .= "MIME-Version: 1.0\n";  
  20. $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
  21. $strHeader .= "This is a multi-part message in MIME format.\n";  
  22.   
  23. $strHeader .= "--".$strSid."\n";  
  24. $strHeader .= "Content-type: text/html; charset=windows-874\n"// or UTF-8 //  
  25. $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
  26. $strHeader .= $strMessage."\n\n";  
  27.   
  28. //*** Files 1 ***//  
  29. $strFilesName1 = "my1.txt";  
  30. $strContent1 = chunk_split(base64_encode(file_get_contents($strFilesName1)));  
  31. $strHeader .= "--".$strSid."\n";  
  32. $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName1."\"\n";  
  33. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  34. $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName1."\"\n\n";  
  35. $strHeader .= $strContent1."\n\n";  
  36.   
  37. //*** Files 2 ***//  
  38. $strFilesName2 = "my2.txt";  
  39. $strContent2 = chunk_split(base64_encode(file_get_contents($strFilesName2)));  
  40. $strHeader .= "--".$strSid."\n";  
  41. $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName2."\"\n";  
  42. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  43. $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName2."\"\n\n";  
  44. $strHeader .= $strContent2."\n\n";  
  45.   
  46. $flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //  
  47. if($flgSend)  
  48. {  
  49. echo "Mail send completed.";  
  50. }  
  51. else  
  52. {  
  53. echo "Cannot send mail.";  
  54. }  
  55. ?>  
  56. </body>  
  57. </html>  

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_sendmail_attachment1.php

Screenshot

PHP Send Mail (Attachment File)

PHP Send Mail (Attachment File)

.

Example 2 (Multiple Attachment With Array)

php_sendmail_attachment2.php

  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. $strTo = "Mr.Member ShotDev.Com<member@shotdev.com>";  
  8. $strSubject = "Test sending mail.";  
  9. $strMessage = "My Body & <b>My Description</b>";  
  10.   
  11. //*** Uniqid Session ***//  
  12. $strSid = md5(uniqid(time()));  
  13.   
  14. $strHeader = "";  
  15. $strHeader .= "From: Mr.Weerachai Nukitram<webmaster@shotdev.com>\nReply-To: webmaster@shotdev.com\n";  
  16. $strHeader .= "Cc: Mr.Surachai Sirisart<surachai@shotdev.com>\n";  
  17. $strHeader .= "Bcc: webmaster@shotdev.com";  
  18.   
  19. $strHeader .= "MIME-Version: 1.0\n";  
  20. $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
  21. $strHeader .= "This is a multi-part message in MIME format.\n";  
  22.   
  23. $strHeader .= "--".$strSid."\n";  
  24. $strHeader .= "Content-type: text/html; charset=windows-874\n"// or UTF-8 //  
  25. $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
  26. $strHeader .= $strMessage."\n\n";  
  27.   
  28. //*** Attachment Files ***//  
  29. $arrFiles[] = "my1.txt";  
  30. $arrFiles[] = "my2.txt";  
  31. $arrFiles[] = "my3.txt";  
  32. $arrFiles[] = "my4.txt";  
  33. $arrFiles[] = "my5.txt";  
  34.   
  35. for($i=0;$i<count($arrFiles);$i++)  
  36. {  
  37. if(trim($arrFiles[$i]) != "")  
  38. {  
  39. $strFilesName = $arrFiles[$i];  
  40. $strContent = chunk_split(base64_encode(file_get_contents($strFilesName)));  
  41.   
  42. $strHeader .= "--".$strSid."\n";  
  43. $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
  44. $strHeader .= "Content-Transfer-Encoding: base64\n";  
  45. $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
  46. $strHeader .= $strContent."\n\n";  
  47. }  
  48. }  
  49.   
  50. $flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No show error //  
  51. if($flgSend)  
  52. {  
  53. echo "Mail send completed.";  
  54. }  
  55. else  
  56. {  
  57. echo "Cannot send mail.";  
  58. }  
  59. ?>  
  60. </body>  
  61. </html>  

Create a php file and save to path root-path/myphp/

Run
http://localhost/myphp/php_sendmail_attachment2.php

Screenshot

PHP Send Mail (Attachment File)

PHP Send Mail (Attachment File)
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

One Response to “How to use PHP & Send Email Attachment File”

  1. 1extension…

Leave a Reply

You must be logged in to post a comment.