code_junkie

How to get email and their attachments from PHP

Nov 11th, 2011
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. // Open pop mailbox
  2. if (!$mbox = imap_open ("{localhost:110/pop3/notls}INBOX", "user", "tester")) {
  3. die ('Cannot connect/check pop mail! Exiting');
  4. }
  5.  
  6. if ($hdr = imap_check($mbox)) {
  7. $msgCount = $hdr->Nmsgs;
  8. } else {
  9. echo "Failed to get mail";
  10. exit;
  11. }
  12.  
  13. $MN=$msgCount;
  14. $overview=imap_fetch_overview($mbox,"1:$MN",0);
  15.  
  16. for ($X = 1; $X <= $MN; $X++) {
  17.  
  18. $file = imap_fetchbody($mbox, $X, 1);
  19.  
  20. imap_delete($mbox, $X);
  21. }
  22.  
  23. imap_expunge($mbox);
  24. imap_close($mbox);
  25.  
  26. msgsubmit: "| /usr/bin/php ~path/to/example.php", msgsubmit-box
  27.  
  28. msgsubmit
  29. "| /usr/bin/php ~path/to/example.php"
  30. msgsubmit-box
  31.  
  32. <?php
  33. //make sure that submit button name is 'Submit'
  34. if(isset($_POST['Submit'])){
  35.  
  36.  
  37. $name = $_POST['visitorname'];
  38. $email = $_POST['visitoremail'];
  39. $message = $_POST['visitormessage'];
  40.  
  41.  
  42.  
  43. $subject="From ".$name;
  44.  
  45.  
  46. $from = $email;
  47.  
  48. // generate a random string to be used as the boundary marker
  49. $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
  50.  
  51. // now we'll build the message headers
  52. $headers = "From: $fromrn" .
  53. "MIME-Version: 1.0rn" .
  54. "Content-Type: multipart/mixed;rn" .
  55. " boundary="{$mime_boundary}"";
  56.  
  57. // next, we'll build the invisible portion of the message body
  58. // note that we insert two dashes in front of the MIME boundary
  59. // when we use it
  60. $message = "This is a multi-part message in MIME format.nn" .
  61. "--{$mime_boundary}n" .
  62. "Content-Type: text/plain; charset="iso-8859-1"n" .
  63. "Content-Transfer-Encoding: 7bitnn" .
  64. $message . "nn";
  65.  
  66. foreach($_FILES as $userfile)
  67. {
  68. // store the file information to variables for easier access
  69. $tmp_name = $userfile['tmp_name'];
  70. $type = $userfile['type'];
  71. $name = $userfile['name'];
  72. $size = $userfile['size'];
  73.  
  74.  
  75.  
  76. // if the upload succeded, the file will exist
  77. if (file_exists($tmp_name))
  78. {
  79.  
  80. // check to make sure that it is an uploaded file and not a system file
  81. if(is_uploaded_file($tmp_name))
  82. {
  83.  
  84. // open the file for a binary read
  85. $file = fopen($tmp_name,'rb');
  86.  
  87. // read the file content into a variable
  88. $data = fread($file,filesize($tmp_name));
  89.  
  90. // close the file
  91. fclose($file);
  92.  
  93. // now we encode it and split it into acceptable length lines
  94. $data = chunk_split(base64_encode($data));
  95. }
  96.  
  97. // now we'll insert a boundary to indicate we're starting the attachment
  98. // we have to specify the content type, file name, and disposition as
  99. // an attachment, then add the file content.
  100. // NOTE: we don't set another boundary to indicate that the end of the
  101. // file has been reached here. we only want one boundary between each file
  102. // we'll add the final one after the loop finishes.
  103. $message .= "--{$mime_boundary}n" .
  104. "Content-Type: {$type};n" .
  105. " name="{$name}"n" .
  106. "Content-Disposition: attachment;n" .
  107. " filename="{$fileatt_name}"n" .
  108. "Content-Transfer-Encoding: base64nn" .
  109. $data . "nn";
  110. }
  111. }
  112.  
  113.  
  114. $ok = @mail($to, $subject, $message , $headers);
  115. if ($ok) {
  116. if (($_FILES["file"]["type"] == "image/gif")
  117. || ($_FILES["file"]["type"] == "image/jpeg")
  118. || ($_FILES["file"]["type"] == "image/pjpeg")
  119. && ($_FILES["file"]["size"] < 20000))
  120. {
  121. if ($_FILES["file"]["error"] > 0)
  122. {
  123. echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  124. }
  125. else
  126. {
  127.  
  128. if (file_exists("upload/" . $_FILES["file"]["name"]))
  129. {
  130. echo $_FILES["file"]["name"] . " already exists. ";
  131. }
  132. else
  133. {
  134. move_uploaded_file($_FILES["file"]["tmp_name"],
  135. "upload/" . $_FILES["file"]["name"]);
  136.  
  137. }
  138. }
  139. }
  140. else
  141. {
  142.  
  143. }
  144. echo "<span class='red'>E-mail has been sent successfully from $mail_name to $to</span>"; }
  145. else{
  146. echo "<span class='red'>Failed to send the E-mail from $from to $to</span>";
  147. }
  148. }
  149. ?>
Add Comment
Please, Sign In to add comment