Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //HOSTS:
- //GMAIL: '{imap.gmail.com:993/ssl/novalidate-cert}';
- //HOTMAIL: '{pop3.live.com:995/pop3/ssl/novalidate-cert}';
- //YAHOO: '{pop.correo.yahoo.es:995/pop3/ssl/novalidate-cert}';
- $hostname= '{imap.gmail.com:993/ssl/novalidate-cert}';
- $username = 'tumejorpagina@gmail.com';
- $password = 'LKJ745632';
- $mailseparator = "################################################################################################";
- $max_mail_per_mailbox = 0; //N?mero de mails a extraer por cada carpeta
- $max_message_len = 0; //N?mero m?ximo de caracteres de cada mail, 0 para extraer todo el mensaje
- showEMailsFromAccount($hostname, $username, $password, $mailseparator, $max_message_len, $max_mail_per_mailbox);
- function showEMailsFromAccount($hostname, $username, $password, $mailseparator, $max_message_len, $max_mail_per_mailbox)
- {
- $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to: ' . print_r(imap_errors()));
- $mailboxes = imap_list ($inbox, $hostname, "*");
- if (is_array($mailboxes))
- {
- foreach($mailboxes as $mailbox)
- {
- if(imap_reopen ($inbox , $mailbox))
- {
- $emails = imap_search($inbox,'ALL');
- echo "\nMAILS FROM : " . $mailbox . "\n\n";
- if($emails)
- {
- rsort($emails);
- $count = 0;
- foreach($emails as $email_number)
- {
- if($max_mail_per_mailbox > 0)
- $count++;
- $overview = imap_fetch_overview($inbox,$email_number,0);
- $structure = imap_fetchstructure($inbox, $email_number);
- $encoding = 0;
- $charset = "";
- $message = "";
- if (isset($structure->parts)){
- $parts = $structure->parts;
- $part_cnt = 1;
- foreach ($parts as $part) {
- print_r($part);
- $encoding = $part->encoding;
- if (isset($part->parts))
- {
- if (is_array($part->parts[0]->parameters))
- {
- if (is_array($part->parameters))
- {
- if ($part->parts[0]->parameters[0]->attribute == "CHARSET")
- {
- $charset = $part->parts[0]->parameters[0]->value;
- $message .= imap_fetchbody($inbox,$email_number,$part_cnt.'.1');
- }
- }
- }
- }
- else
- {
- if (is_array($part->parameters))
- {
- if ($part->parameters[0]->attribute == "CHARSET")
- {
- $charset = $part->parameters[0]->value;
- $message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
- }
- }
- }
- //Attachments
- //if ($part->parameters[0]->attribute == "NAME") {
- //
- // $att_name = imap_utf8($part->parameters[0]->value);
- //
- // $savefilename = $att_cntr.'_'.$att_name;
- //
- // $att_cntr++;
- //
- // $atts[] = array($att_name, $gmail_attachments_upload_to.$savefilename);
- //}
- $part_cnt++;
- }
- }
- else
- {
- if (is_array($part->parameters) && count($part->parameters) > 0)
- {
- if(property_exists($structure->parameters[0], "value") $charset = $structure->parameters[0]->value;
- $encoding = $structure->encoding;
- $message .= imap_body($inbox,$email_number);
- }
- }
- $subject = "";
- $elementos = imap_mime_header_decode($overview[0]->subject);
- for ($i=0; $i<count($elementos); $i++) {
- $subject .= $elementos[$i]->text;
- }
- if ($encoding == 0)
- {
- $message = $message; //to7bit($message, $charset);
- }
- elseif ($encoding == 1)
- {
- $message = $message; //imap_qprint(imap_8bit($message));
- }
- elseif ($encoding == 2)
- {
- $message = imap_binary($message);
- }
- elseif ($encoding == 3)
- {
- $message = imap_base64($message);
- }
- elseif ($encoding == 4)
- {
- $message = quoted_printable_decode($message); //imap_qprint($message); //
- }
- elseif ($encoding == 5)
- {
- $message = $message;
- }
- $charset = strtoupper($charset);
- echo "\n$mailseparator\n";
- echo "FROM: " . $overview[0]->from . "\n";
- echo "DATE: " . $overview[0]->date . "\n";
- echo "SUBJECT: " . $subject . "\n";
- echo "ENCODING: " . $encoding . "\n";
- echo "CHARSET: " . $charset . "\n";
- echo "MESSAGE:\n";
- if($charset != "")
- $message = mb_convert_encoding ($message, 'ISO-8859-15', $charset);
- else
- $message = mb_convert_encoding ($message, 'ISO-8859-15');
- if($max_message_len > 0) $message = substr($message, 0, $max_message_len);
- echo $message. ";\n";
- echo "\n$mailseparator\n";
- }
- }
- }
- }
- }
- else
- {
- echo imap_last_error() . "\n";
- }
- imap_close($inbox);
- }
- ?>
Add Comment
Please, Sign In to add comment