Guest User

Untitled

a guest
Jul 9th, 2018
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. <?php
  2. //HOSTS:
  3. //GMAIL: '{imap.gmail.com:993/ssl/novalidate-cert}';
  4. //HOTMAIL: '{pop3.live.com:995/pop3/ssl/novalidate-cert}';
  5. //YAHOO: '{pop.correo.yahoo.es:995/pop3/ssl/novalidate-cert}';
  6. $hostname= '{imap.gmail.com:993/ssl/novalidate-cert}';
  7. $username = 'tumejorpagina@gmail.com';
  8. $password = 'LKJ745632';
  9.  
  10. $mailseparator = "################################################################################################";
  11. $max_mail_per_mailbox = 0; //N?mero de mails a extraer por cada carpeta
  12. $max_message_len = 0; //N?mero m?ximo de caracteres de cada mail, 0 para extraer todo el mensaje
  13.  
  14. showEMailsFromAccount($hostname, $username, $password, $mailseparator, $max_message_len, $max_mail_per_mailbox);
  15.  
  16. function showEMailsFromAccount($hostname, $username, $password, $mailseparator, $max_message_len, $max_mail_per_mailbox)
  17. {
  18. $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to: ' . print_r(imap_errors()));
  19.  
  20. $mailboxes = imap_list ($inbox, $hostname, "*");
  21.  
  22. if (is_array($mailboxes))
  23. {
  24. foreach($mailboxes as $mailbox)
  25. {
  26. if(imap_reopen ($inbox , $mailbox))
  27. {
  28. $emails = imap_search($inbox,'ALL');
  29.  
  30. echo "\nMAILS FROM : " . $mailbox . "\n\n";
  31.  
  32. if($emails)
  33. {
  34. rsort($emails);
  35.  
  36. $count = 0;
  37.  
  38. foreach($emails as $email_number)
  39. {
  40. if($max_mail_per_mailbox > 0)
  41. $count++;
  42.  
  43. $overview = imap_fetch_overview($inbox,$email_number,0);
  44. $structure = imap_fetchstructure($inbox, $email_number);
  45.  
  46. $encoding = 0;
  47. $charset = "";
  48. $message = "";
  49.  
  50. if (isset($structure->parts)){
  51.  
  52. $parts = $structure->parts;
  53.  
  54. $part_cnt = 1;
  55.  
  56. foreach ($parts as $part) {
  57.  
  58. print_r($part);
  59. $encoding = $part->encoding;
  60. if (isset($part->parts))
  61. {
  62.  
  63. if (is_array($part->parts[0]->parameters))
  64. {
  65. if (is_array($part->parameters))
  66. {
  67. if ($part->parts[0]->parameters[0]->attribute == "CHARSET")
  68. {
  69. $charset = $part->parts[0]->parameters[0]->value;
  70. $message .= imap_fetchbody($inbox,$email_number,$part_cnt.'.1');
  71. }
  72. }
  73. }
  74. }
  75. else
  76. {
  77. if (is_array($part->parameters))
  78. {
  79. if ($part->parameters[0]->attribute == "CHARSET")
  80. {
  81. $charset = $part->parameters[0]->value;
  82. $message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
  83. }
  84. }
  85. }
  86.  
  87. //Attachments
  88. //if ($part->parameters[0]->attribute == "NAME") {
  89. //
  90. // $att_name = imap_utf8($part->parameters[0]->value);
  91. //
  92. // $savefilename = $att_cntr.'_'.$att_name;
  93. //
  94. // $att_cntr++;
  95. //
  96. // $atts[] = array($att_name, $gmail_attachments_upload_to.$savefilename);
  97. //}
  98.  
  99. $part_cnt++;
  100.  
  101. }
  102.  
  103. }
  104. else
  105. {
  106. if (is_array($part->parameters) && count($part->parameters) > 0)
  107. {
  108. if(property_exists($structure->parameters[0], "value") $charset = $structure->parameters[0]->value;
  109. $encoding = $structure->encoding;
  110. $message .= imap_body($inbox,$email_number);
  111. }
  112. }
  113.  
  114. $subject = "";
  115. $elementos = imap_mime_header_decode($overview[0]->subject);
  116. for ($i=0; $i<count($elementos); $i++) {
  117. $subject .= $elementos[$i]->text;
  118. }
  119.  
  120. if ($encoding == 0)
  121. {
  122. $message = $message; //to7bit($message, $charset);
  123. }
  124. elseif ($encoding == 1)
  125. {
  126. $message = $message; //imap_qprint(imap_8bit($message));
  127. }
  128. elseif ($encoding == 2)
  129. {
  130. $message = imap_binary($message);
  131. }
  132. elseif ($encoding == 3)
  133. {
  134. $message = imap_base64($message);
  135. }
  136. elseif ($encoding == 4)
  137. {
  138. $message = quoted_printable_decode($message); //imap_qprint($message); //
  139. }
  140. elseif ($encoding == 5)
  141. {
  142. $message = $message;
  143. }
  144. $charset = strtoupper($charset);
  145.  
  146. echo "\n$mailseparator\n";
  147. echo "FROM: " . $overview[0]->from . "\n";
  148. echo "DATE: " . $overview[0]->date . "\n";
  149. echo "SUBJECT: " . $subject . "\n";
  150. echo "ENCODING: " . $encoding . "\n";
  151. echo "CHARSET: " . $charset . "\n";
  152. echo "MESSAGE:\n";
  153. if($charset != "")
  154. $message = mb_convert_encoding ($message, 'ISO-8859-15', $charset);
  155. else
  156. $message = mb_convert_encoding ($message, 'ISO-8859-15');
  157. if($max_message_len > 0) $message = substr($message, 0, $max_message_len);
  158. echo $message. ";\n";
  159. echo "\n$mailseparator\n";
  160. }
  161. }
  162. }
  163. }
  164. }
  165. else
  166. {
  167. echo imap_last_error() . "\n";
  168. }
  169.  
  170. imap_close($inbox);
  171. }
  172. ?>
Add Comment
Please, Sign In to add comment