· 8 years ago · Jul 10, 2018, 12:02 AM
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 = 3; //N?mero de mails a extraer por cada carpeta
12$max_message_len = 500; //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 //Conectamos con el servidor de correo
19 $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to: ' . print_r(imap_errors()));
20
21 //Sacamos los buzones
22 $mailboxes = imap_list ($inbox, $hostname, "*");
23
24 //Vemos si hay algun
25 if (is_array($mailboxes))
26 {
27 //Los recorremos
28 foreach($mailboxes as $mailbox)
29 {
30 //Reabrimos apuntando al buzon (INBOX, etc)
31 if(imap_reopen ($inbox , $mailbox))
32 {
33 //Sacamos los emails
34 $emails = imap_search($inbox,'ALL');
35
36 echo "\nMAILS FROM : " . $mailbox . "\n\n";
37
38 if($emails)
39 {
40 rsort($emails);
41
42 $count = 0;
43
44 //Recorremos los emails
45 foreach($emails as $email_number)
46 {
47 if($max_mail_per_mailbox > 0)
48 $count++;
49
50 //Sacamos la cabecera
51 $overview = imap_fetch_overview($inbox,$email_number,0);
52 //Sacamos el cuerpo del mail
53 $structure = imap_fetchstructure($inbox, $email_number);
54
55 $encoding = 0;
56 $charset = "";
57 $message = "";
58 $miss_warning = null;
59 //Vemos si tiene partes (poner print_r($structure) para ver la estructura
60 if (isset($structure->parts)){
61
62 $parts = $structure->parts;
63
64 $part_cnt = 1;
65 //DEBUG print_r($structure);
66 //Recorremos las partes
67 foreach ($parts as $part)
68 {
69 //Puede haber varios tipos
70 $encoding = $part->encoding;
71 if (isset($part->parts))
72 {
73 if (is_array($part->parts))
74 {
75 if (is_array($part->parts[0]->parameters))
76 {
77 for($k = 0; $k < count($part->parts[0]->parameters); $k++)
78 {
79
80 if (!($part->parts[0]->parameters[$k]->attribute == "NAME" || $part->parts[0]->parameters[$k]->attribute == "FILENAME"))
81 if ($part->parts[0]->parameters[$k]->attribute == "CHARSET")
82 {
83 $charset = $part->parts[0]->parameters[$k]->value;
84 $message_aux = "";
85 $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt);
86 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".1");
87 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".2");
88 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".1.1");
89 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".2.2");
90 if($message_aux == "") $miss_warning = "NULL";
91 $message .= $message_aux;
92 echo "AQUI2\n" . strlen($message);
93 }
94 else
95 {
96 $miss_warning = "1";
97 //DEBUG print_r($part->parts[0]->parameters);
98 }
99 }
100 }
101 else
102 {
103 $miss_warning = "2";
104 //DEBUG print_r($part->parts[0]->parameters);
105 }
106 }
107 else
108 {
109 $miss_warning = "ERROR CODE 3";
110 //DEBUG print_r($part->parts);
111 }
112 }
113 else
114 {
115 if (is_array($part->parameters))
116 {
117 for($k = 0; $k < count($part->parameters); $k++)
118 {
119 if (!($part->parameters[$k]->attribute == "NAME" || $part->parameters[$k]->attribute == "FILENAME"))
120 if ($part->parameters[$k]->attribute == "CHARSET")
121 {
122 $charset = $part->parameters[$k]->value;
123 //$message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
124 $message_aux = "";
125 $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt);
126 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".1");
127 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".2");
128 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".1.1");
129 if($message_aux == "") $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt . ".2.2");
130 if($message_aux == "") $miss_warning = "NULL";
131 $message .= $message_aux;
132 }
133 else
134 {
135 $miss_warning = "4";
136 //DEBUG print_r($structure->parameters);
137 }
138 }
139 }
140 else
141 {
142 $message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
143 }
144 }
145
146 //Attachments
147 //if ($part->parameters[0]->attribute == "NAME") {
148 //
149 // $att_name = imap_utf8($part->parameters[0]->value);
150 //
151 // $savefilename = $att_cntr.'_'.$att_name;
152 //
153 // $att_cntr++;
154 //
155 // $atts[] = array($att_name, $gmail_attachments_upload_to.$savefilename);
156 //}
157
158 $part_cnt++;
159
160 }
161
162 }
163 //No hay partes
164 else
165 {
166 if (is_array($structure->parameters) && count($structure->parameters) > 0)
167 {
168 for($k = 0; $k < count($structure->parameters); $k++)
169 {
170 if (!($structure->parameters[$k]->attribute == "NAME" || $structure->parameters[$k]->attribute == "FILENAME"))
171 if ($structure->parameters[$k]->attribute == "CHARSET")
172 {
173 if(property_exists($structure->parameters[$k], "value")) $charset = $structure->parameters[$k]->value;
174 $encoding = $structure->encoding;
175 $message .= imap_body($inbox,$email_number);
176 }
177 else
178 {
179 $miss_warning = "6";
180 //DEBUG print_r($structure->parameters);
181 }
182 }
183 }
184 else
185 {
186 $miss_warning = "7";
187 //DEBUG print_r($structure->parameters);
188 }
189
190 }
191
192 $subject = "";
193 $elementos = imap_mime_header_decode($overview[0]->subject);
194 for ($i=0; $i<count($elementos); $i++) {
195 $subject .= $elementos[$i]->text;
196 }
197
198 $message_pre = $message;
199 if ($encoding == 0)
200 {
201 $message = $message; //to7bit($message, $charset);
202 }
203 elseif ($encoding == 1)
204 {
205 $message = $message; //imap_qprint(imap_8bit($message));
206 }
207 elseif ($encoding == 2)
208 {
209 $message = imap_binary($message);
210 }
211 elseif ($encoding == 3)
212 {
213 $message = imap_base64($message);
214 }
215 elseif ($encoding == 4)
216 {
217 $message = quoted_printable_decode($message); //imap_qprint($message); //
218 }
219 elseif ($encoding == 5)
220 {
221 $message = $message;
222 }
223 if($message == "") $message = $message_pre;
224 $charset = strtoupper($charset);
225
226 echo "\n$mailseparator\n";
227 echo "FROM: " . $overview[0]->from . "\n";
228 echo "DATE: " . $overview[0]->date . "\n";
229 echo "SUBJECT: " . $subject . "\n";
230 echo "ENCODING: " . $encoding . "\n";
231 echo "CHARSET: " . $charset . "\n";
232
233 if($charset != "ISO-8859-15" && $charset != "ISO-8859-1")
234 if($charset != "")
235 $message = mb_convert_encoding ($message, 'ISO-8859-15', $charset);
236 else
237 $message = mb_convert_encoding ($message, 'ISO-8859-15');
238
239 echo "CHARS:" . strlen($message) . "\n";
240
241 if($max_message_len > 0) $message = substr($message, 0, $max_message_len);
242
243 echo "MESSAGE:\n";
244 echo $message . "\n";
245 if($miss_warning != null) echo "MISS:" . $miss_warning;
246 echo "\n$mailseparator\n";
247 }
248 }
249 }
250 }
251 }
252 else
253 {
254 echo imap_last_error() . "\n";
255 }
256
257 imap_close($inbox);
258}
259?>