· 8 years ago · Jul 09, 2018, 09:30 PM
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 = '@gmail.com';
8$password = '';
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
14showEMailsFromAccount($hostname, $username, $password, $mailseparator, $max_message_len, $max_mail_per_mailbox);
15
16function 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
59 $encoding = $part->encoding;
60 if (isset($part->parts))
61 {
62 if (is_array($part->parts[0]->parameters))
63 {
64 if (is_array($part->parameters))
65 {
66 if ($part->parts[0]->parameters[0]->attribute == "CHARSET")
67 {
68 $charset = $part->parts[0]->parameters[0]->value;
69 $message .= imap_fetchbody($inbox,$email_number,$part_cnt.'.1');
70 }
71 }
72 }
73 }
74 else
75 {
76 if (is_array($part->parameters))
77 {
78 if ($part->parameters[0]->attribute == "CHARSET")
79 {
80 $charset = $part->parameters[0]->value;
81 $message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
82 }
83 }
84 }
85
86 //Attachments
87 //if ($part->parameters[0]->attribute == "NAME") {
88 //
89 // $att_name = imap_utf8($part->parameters[0]->value);
90 //
91 // $savefilename = $att_cntr.'_'.$att_name;
92 //
93 // $att_cntr++;
94 //
95 // $atts[] = array($att_name, $gmail_attachments_upload_to.$savefilename);
96 //}
97
98 $part_cnt++;
99
100 }
101
102 }
103 else
104 {
105 if (is_array($structure->parameters) && count($structure->parameters) > 0)
106 {
107 if ($structure->parameters[0]->attribute == "CHARSET")
108 {
109 if(property_exists($structure->parameters[0], "value")) $charset = $structure->parameters[0]->value;
110 $encoding = $structure->encoding;
111 $message .= imap_body($inbox,$email_number);
112 }
113 }
114 }
115
116 $subject = "";
117 $elementos = imap_mime_header_decode($overview[0]->subject);
118 for ($i=0; $i<count($elementos); $i++) {
119 $subject .= $elementos[$i]->text;
120 }
121
122 if ($encoding == 0)
123 {
124 $message = $message; //to7bit($message, $charset);
125 }
126 elseif ($encoding == 1)
127 {
128 $message = $message; //imap_qprint(imap_8bit($message));
129 }
130 elseif ($encoding == 2)
131 {
132 $message = imap_binary($message);
133 }
134 elseif ($encoding == 3)
135 {
136 $message = imap_base64($message);
137 }
138 elseif ($encoding == 4)
139 {
140 $message = quoted_printable_decode($message); //imap_qprint($message); //
141 }
142 elseif ($encoding == 5)
143 {
144 $message = $message;
145 }
146 $charset = strtoupper($charset);
147
148 echo "\n$mailseparator\n";
149 echo "FROM: " . $overview[0]->from . "\n";
150 echo "DATE: " . $overview[0]->date . "\n";
151 echo "SUBJECT: " . $subject . "\n";
152 echo "ENCODING: " . $encoding . "\n";
153 echo "CHARSET: " . $charset . "\n";
154 echo "MESSAGE:\n";
155 if($charset != "")
156 $message = mb_convert_encoding ($message, 'ISO-8859-15', $charset);
157 else
158 $message = mb_convert_encoding ($message, 'ISO-8859-15');
159 if($max_message_len > 0) $message = substr($message, 0, $max_message_len);
160 echo $message. ";\n";
161 echo "\n$mailseparator\n";
162 }
163 }
164 }
165 }
166 }
167 else
168 {
169 echo imap_last_error() . "\n";
170 }
171
172 imap_close($inbox);
173}
174?>