· 8 years ago · Jul 09, 2018, 11: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 = 100; //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 if (!($part->parts[0]->parameters[$k]->attribute == "NAME" || $part->parts[0]->parameters[$k]->attribute == "FILENAME"))
80 if ($part->parts[0]->parameters[$k]->attribute == "CHARSET")
81 {
82 $charset = $part->parts[0]->parameters[$k]->value;
83 $message_aux = imap_fetchbody($inbox,$email_number,$part_cnt.'.1');
84 if($message_aux == "")
85 $message .= imap_fetchbody($inbox,$email_number,$part_cnt);
86 else
87 $message .= $message_aux;
88 }
89 else
90 {
91 $miss_warning = "MISS ERROR CODE1";
92 //DEBUG print_r($part->parts[0]->parameters);
93 }
94 }
95 }
96 else
97 {
98 $miss_warning = "MISS ERROR CODE2";
99 //DEBUG print_r($part->parts[0]->parameters);
100 }
101 }
102 else
103 {
104 $miss_warning = "MISS ERROR CODEERROR CODE 3";
105 //DEBUG print_r($part->parts);
106 }
107 }
108 else
109 {
110 if (is_array($part->parameters))
111 {
112 for($k = 0; $k < count($part->parameters); $k++)
113 {
114 if (!($part->parameters[$k]->attribute == "NAME" || $part->parameters[$k]->attribute == "FILENAME"))
115 if ($part->parameters[$k]->attribute == "CHARSET")
116 {
117 $charset = $part->parameters[$k]->value;
118 $message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
119 }
120 else
121 {
122 $miss_warning = "MISS ERROR CODE4";
123 //DEBUG print_r($structure->parameters);
124 }
125 }
126 }
127 else
128 {
129 $message .= imap_fetchbody($inbox,$email_number,$part_cnt) . "\n";
130 }
131 }
132
133 //Attachments
134 //if ($part->parameters[0]->attribute == "NAME") {
135 //
136 // $att_name = imap_utf8($part->parameters[0]->value);
137 //
138 // $savefilename = $att_cntr.'_'.$att_name;
139 //
140 // $att_cntr++;
141 //
142 // $atts[] = array($att_name, $gmail_attachments_upload_to.$savefilename);
143 //}
144
145 $part_cnt++;
146
147 }
148
149 }
150 //No hay partes
151 else
152 {
153 if (is_array($structure->parameters) && count($structure->parameters) > 0)
154 {
155 for($k = 0; $k < count($structure->parameters); $k++)
156 {
157 if (!($structure->parameters[$k]->attribute == "NAME" || $structure->parameters[$k]->attribute == "FILENAME"))
158 if ($structure->parameters[$k]->attribute == "CHARSET")
159 {
160 if(property_exists($structure->parameters[$k], "value")) $charset = $structure->parameters[$k]->value;
161 $encoding = $structure->encoding;
162 $message .= imap_body($inbox,$email_number);
163 }
164 else
165 {
166 $miss_warning = "MISS ERROR CODE6";
167 //DEBUG print_r($structure->parameters);
168 }
169 }
170 }
171 else
172 {
173 $miss_warning = "MISS ERROR CODE7";
174 //DEBUG print_r($structure->parameters);
175 }
176
177 }
178
179 $subject = "";
180 $elementos = imap_mime_header_decode($overview[0]->subject);
181 for ($i=0; $i<count($elementos); $i++) {
182 $subject .= $elementos[$i]->text;
183 }
184
185 if ($encoding == 0)
186 {
187 $message = $message; //to7bit($message, $charset);
188 }
189 elseif ($encoding == 1)
190 {
191 $message = $message; //imap_qprint(imap_8bit($message));
192 }
193 elseif ($encoding == 2)
194 {
195 $message = imap_binary($message);
196 }
197 elseif ($encoding == 3)
198 {
199 $message = imap_base64($message);
200 }
201 elseif ($encoding == 4)
202 {
203 $message = quoted_printable_decode($message); //imap_qprint($message); //
204 }
205 elseif ($encoding == 5)
206 {
207 $message = $message;
208 }
209 $charset = strtoupper($charset);
210
211 echo "\n$mailseparator\n";
212 echo "FROM: " . $overview[0]->from . "\n";
213 echo "DATE: " . $overview[0]->date . "\n";
214 echo "SUBJECT: " . $subject . "\n";
215 echo "ENCODING: " . $encoding . "\n";
216 echo "CHARSET: " . $charset . "\n";
217
218 if($charset != "ISO-8859-15" && $charset != "ISO-8859-1")
219 if($charset != "")
220 $message = mb_convert_encoding ($message, 'ISO-8859-15', $charset);
221 else
222 $message = mb_convert_encoding ($message, 'ISO-8859-15');
223
224 echo "CHARS:" . strlen($message) . "\n";
225
226 if($max_message_len > 0) $message = substr($message, 0, $max_message_len);
227
228 echo "MESSAGE:\n";
229 echo $message . "\n";
230 if($miss_warning != null) echo "MISS_ERROR:" . $miss_warning;
231 echo "\n$mailseparator\n";
232 }
233 }
234 }
235 }
236 }
237 else
238 {
239 echo imap_last_error() . "\n";
240 }
241
242 imap_close($inbox);
243}
244?>