Guest User

Untitled

a guest
Mar 1st, 2019
1,805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. //define the receiver of the email
  2. //define the subject of the email
  3. $subject = 'Test email with attachment';
  4. //create a boundary string. It must be unique
  5. //so we use the MD5 algorithm to generate a random hash
  6. $random_hash = md5(date('r', time()));
  7. //define the headers we want passed. Note that they are separated with rn
  8. //add boundary string and mime type specification
  9. $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";
  10. //read the atachment file contents into a string,
  11. //encode it with MIME base64,
  12. //and split it into smaller chunks
  13. $attachment = chunk_split(base64_encode(file_get_contents($_FILES['curriculum_vitae']['tmp_name'])));
  14. //define the body of the message.
  15. ob_start(); //Turn on output buffering
  16. ?>
  17. --PHP-mixed-<?php echo $random_hash; ?>
  18. Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
  19.  
  20. --PHP-alt-<?php echo $random_hash; ?>
  21. Content-Type: text/plain; charset="iso-8859-1"
  22. Content-Transfer-Encoding: 7bit
  23.  
  24. Hello World!!!
  25. This is simple text email message.
  26.  
  27. --PHP-alt-<?php echo $random_hash; ?>
  28. Content-Type: text/html; charset="iso-8859-1"
  29. Content-Transfer-Encoding: 7bit
  30.  
  31. <h2>Hello World!</h2>
  32. <p>This is something with <b>HTML</b> formatting.</p>
  33.  
  34. --PHP-alt-<?php echo $random_hash; ?>--
  35.  
  36. --PHP-mixed-<?php echo $random_hash; ?>
  37. Content-Type: application/zip; name="attachment.zip"
  38. Content-Transfer-Encoding: base64
  39. Content-Disposition: attachment
  40.  
  41. <?php echo $attachment; ?>
  42. --PHP-mixed-<?php echo $random_hash; ?>--
  43.  
  44. <?php
  45. //copy current buffer contents into $message variable and delete current output buffer
  46. $message = ob_get_clean();
  47. //send the email
  48. $mail_sent = @mail( $to, $subject, $message, $headers );
  49. //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
  50. echo $mail_sent ? "Mail sent" : "Mail failed";
  51.  
  52. Content-Type: application/zip; name="attachment.zip"
  53.  
  54. Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['name']; ?>"
  55.  
  56. $subject = 'Test email with attachment';
  57. $random_hash = md5(date('r', time()));
  58. $headers .= "rnContent-Type: multipart/mixed; boundary="PHP-mixed-".$random_hash.""";
  59. if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ $attachment = chunk_split(base64_encode(file_get_contents($_FILES['curriculum_vitae']['tmp_name']))); }
  60. ob_start();
  61. ?>
  62. --PHP-mixed-<?php echo $random_hash; ?>
  63. Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
  64.  
  65. --PHP-alt-<?php echo $random_hash; ?>
  66. Content-Type: text/plain; charset="iso-8859-1"
  67. Content-Transfer-Encoding: 7bit
  68.  
  69. Hello World!!!
  70. This is simple text email message.
  71.  
  72. --PHP-alt-<?php echo $random_hash; ?>
  73. Content-Type: text/html; charset="iso-8859-1"
  74. Content-Transfer-Encoding: 7bit
  75.  
  76. <h2>Hello World!</h2>
  77. <p>This is something with <b>HTML</b> formatting.</p>
  78.  
  79. --PHP-alt-<?php echo $random_hash; ?>--
  80.  
  81. <?php if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ ?>--PHP-mixed-<?php echo $random_hash; ?>
  82. Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['name']; ?>"
  83. Content-Transfer-Encoding: base64
  84. Content-Disposition: attachment
  85.  
  86. <?php echo $attachment; ?>
  87. --PHP-mixed-<?php echo $random_hash; ?>--
  88. <?php } ?>
  89. <?php
  90. $message = ob_get_clean();
  91. $mail_sent = @mail( $to, $subject, $message, $headers );
  92. echo $mail_sent ? "Mail sent" : "Mail failed";
  93.  
  94. <?php
  95. function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
  96. $file = $path.$filename;
  97. $file_size = filesize($file);
  98. $handle = fopen($file, "r");
  99. $content = fread($handle, $file_size);
  100. fclose($handle);
  101. $content = chunk_split(base64_encode($content));
  102. $uid = md5(uniqid(time()));
  103. $name = basename($file);
  104. $header = "From: ".$from_name." <".$from_mail.">rn";
  105. $header .= "Reply-To: ".$replyto."rn";
  106. $header .= "MIME-Version: 1.0rn";
  107. $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
  108. $header .= "This is a multi-part message in MIME format.rn";
  109. $header .= "--".$uid."rn";
  110. $header .= "Content-type:text/plain; charset=iso-8859-1rn";
  111. $header .= "Content-Transfer-Encoding: 7bitrnrn";
  112. $header .= $message."rnrn";
  113. $header .= "--".$uid."rn";
  114. $header .= "Content-Type: application/octet-stream; name="".$filename.""rn"; // use different content types here
  115. $header .= "Content-Transfer-Encoding: base64rn";
  116. $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
  117. $header .= $content."rnrn";
  118. $header .= "--".$uid."--";
  119. if (mail($mailto, $subject, "", $header)) {
  120. echo "mail send ... OK"; // or use booleans here
  121. } else {
  122. echo "mail send ... ERROR!";
  123. }
  124. }
  125. ?>
  126.  
  127. <?php
  128. $my_file = "somefile.zip";
  129. $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
  130. $my_name = "Olaf Lederer";
  131. $my_mail = "[email protected]";
  132. $my_replyto = "[email protected]";
  133. $my_subject = "This is a mail with attachment.";
  134. $my_message = "Hallo,rndo you like this script? I hope it will help.rnrngr. Olaf";
  135. mail_attachment($my_file, $my_path, "[email protected]", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
  136. ?>
Advertisement
Add Comment
Please, Sign In to add comment