Narendra123

send email with attachment file working

Feb 1st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. <?php
  2. if(isset($_POST['submit']))
  3. {
  4. //The form has been submitted, prep a nice thank you message
  5.  
  6. //Set the form flag to no display (cheap way!)
  7.  
  8.  
  9. //Deal with the email
  10. $to = 'bnarendra6036@gmail.com';
  11. $subject = 'a file for you';
  12.  
  13. $message = strip_tags($_POST['message']);
  14. $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
  15. $filename = $_FILES['file']['name'];
  16.  
  17. $boundary =md5(date('r', time()));
  18.  
  19. $headers = "From: bnarendra6036@gmail.com\r\nReply-To: bnarendra6036@gmail.com";
  20. $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
  21.  
  22. $message="This is a multi-part message in MIME format.
  23.  
  24. --_1_$boundary
  25. Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
  26.  
  27. --_2_$boundary
  28. Content-Type: text/plain; charset=\"iso-8859-1\"
  29. Content-Transfer-Encoding: 7bit
  30.  
  31. $message
  32.  
  33. --_2_$boundary--
  34. --_1_$boundary
  35. Content-Type: application/octet-stream; name=\"$filename\"
  36. Content-Transfer-Encoding: base64
  37. Content-Disposition: attachment
  38.  
  39. $attachment
  40. --_1_$boundary--";
  41.  
  42. if(mail($to, $subject, $message, $headers))
  43. {
  44. echo "mail success";
  45. }else{
  46. echo "fail to send mail";
  47. }
  48. }
  49. ?>
  50. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  51. <html>
  52. <head>
  53. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  54. <title>MailFile</title>
  55. </head>
  56.  
  57. <body>
  58.  
  59. <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
  60. <p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
  61. <p><label for="file">File</label> <input type="file" name="file" id="file"></p>
  62. <p><input type="submit" name="submit" id="submit" value="send"></p>
  63. </form>
  64. </body>
  65. </html>
Add Comment
Please, Sign In to add comment