-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_scraper.php
43 lines (35 loc) · 1.08 KB
/
email_scraper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'ENTER_PASSWORD_HERE';
while(1){
/* Try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* Grab emails */
$emails = imap_search($inbox,'UNSEEN FROM "[email protected]"');
if($emails) {
/* Begin output var */
$output = '';
/* Put the newest emails on top */
rsort($emails);
foreach($emails as $email_number) {
/* Get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,2);
/* Output the email body */
$output.= $overview[0]->from.';';
$output.= $message;
}
$myfile = fopen("garage_output.txt", "w") or die("Unable to open file!");
echo $output;
echo PHP_EOL;
fwrite($myfile, $output);
fclose($myfile);
break;
}
/* Don't need to consistently poll - once every 7 seconds is adequate */
sleep(7);
/* Close the connection */
imap_close($inbox);
}
?>