Advertisement
Guest User

Untitled

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