Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download email with PHP

Tags:

php

email

imap

I am trying to write a very simple web-based email client from scratch with PHP's standard library. I'll be honest; I'm not entirely sure what I'm doing, so this is something that I hacked together for trial-and-error learning. However, I keep timing out no matter what I try. Any advice? I know the intended output won't look pretty, but like I said, this is just for trial and error.

<?php

$stream = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', '<email_removed>', '<password_removed>') or
    die('Cannot connect to server: ' . imap_last_error());

$date = date('d M Y', strtotime('-1 month'));
if ($emails = imap_search($stream, "SINCE \"$date\"")) {
    rsort($emails);

    foreach ($emails as $email_number) {
        echo imap_fetchbody($stream, $email_number, 2);
    }
}

imap_close($stream);

?>

The output I get it as follows.

Warning: imap_open() [function.imap-open]: Couldn't open stream {imap.gmail.com:993/imap/ssl}INBOX in on line 3 Cannot connect to server: Can't connect to gmail-imap.l.google.com,993: Network is unreachable

Notice: Unknown: Can't connect to gmail-imap.l.google.com,993: Connection timed out (errflg=1) in Unknown on line 0

Notice: Unknown: Can't connect to gmail-imap.l.google.com,993: Connection timed out (errflg=1) in Unknown on line 0

Notice: Unknown: Can't connect to gmail-imap.l.google.com,993: Network is unreachable (errflg=2) in Unknown on line 0

like image 479
Tyler Crompton Avatar asked Oct 22 '22 19:10

Tyler Crompton


1 Answers

After further research, my host (HostMonster) disallows this, so I will have to take my business elsewhere. :/

like image 155
Tyler Crompton Avatar answered Oct 27 '22 09:10

Tyler Crompton