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