Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Retrieve Your Gmail Emails Using PHP and IMAP

82 Responses »
Gmail »

Grabbing emails from your Gmail account using PHP is probably easier than you think. Armed with PHP and its IMAP extension, you can retrieve emails from your Gmail account in no time! Just for fun, I'll be using the MooTools Fx.Accordion plugin to display each email.

The CSS

div.toggler				{ border:1px solid #ccc; background:url(gmail2.jpg) 10px 12px #eee no-repeat; cursor:pointer; padding:10px 32px; }
div.toggler .subject	{ font-weight:bold; }
div.read					{ color:#666; }
div.toggler .from, div.toggler .date { font-style:italic; font-size:11px; }
div.body					{ padding:10px 20px; }

Some simple CSS formatting.

The MooTools JavaScript

window.addEvent('domready',function() {
	var togglers = $$('div.toggler');
	if(togglers.length) var gmail = new Fx.Accordion(togglers,$$('div.body'));
	togglers.addEvent('click',function() { this.addClass('read').removeClass('unread'); });
	togglers[0].fireEvent('click'); //first one starts out read
});

Some simple accordion code.

The PHP

/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'davidwalshblog@gmail.com';
$password = 'davidwalsh';

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {
	
	/* begin output var */
	$output = '';
	
	/* put the newest emails on top */
	rsort($emails);
	
	/* for every email... */
	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 header information */
		$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
		$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
		$output.= '<span class="from">'.$overview[0]->from.'</span>';
		$output.= '<span class="date">on '.$overview[0]->date.'</span>';
		$output.= '</div>';
		
		/* output the email body */
		$output.= '<div class="body">'.$message.'</div>';
	}
	
	echo $output;
} 

/* close the connection */
imap_close($inbox);

With our individual username/password settings set, we connect to Gmail. Once connected, we request all emails. If we find emails, I reverse sort the emails so that the newest emails appear on top. For every email we receive, I output the subject and message in MooTools Fx.Accordion format.

Now that I've shown you the basics, it's time for you to build your next great PHP email app! Go ahead and send a few emails to davidwalshblog@gmail.com to see your eamil display on the page!

There are some encoding issues and I've not found a great way to include images. Any tips appreciated!

Discussion

  1. July 8, 2009 @ 9:00 am

    great and useful many thanks David!

  2. July 8, 2009 @ 9:27 am

    nice shot Dave!

  3. ahmed
    July 8, 2009 @ 9:42 am

    I love it. Wish I could mark emails as read when you toggle them though.
    Oh, and I noticed a flicker when toggling “unread” emails.

  4. ahmed
    July 8, 2009 @ 9:43 am

    NVM the flicker thing, guess it was my FF acting weird :P

  5. July 8, 2009 @ 10:35 am

    Your picture is outdated…gmail is out of beta ;)

  6. July 8, 2009 @ 10:41 am

    I did that on purpose. Always beta!
    O

  7. July 8, 2009 @ 10:52 am

    how can one get a list of contacts of a gmail account? Facebook and most social networking sites do that…but how?

  8. July 9, 2009 @ 1:13 am

    Awesome post. Can we do it for other emails ie Yahoo, Hotmail etc.

  9. July 9, 2009 @ 1:26 am

    wow this is great code. Congratulations…

    how to display new mails only?

  10. July 9, 2009 @ 9:07 am

    @scvinodkumar – The method imap_fetch_overview() returns an array, one parameter of which is called ‘seen’. This flag indicates whether the message has been read or not. Likewise you have ‘answered’, whether you’ve replied to the message.

    Good post David, thank you.

  11. July 9, 2009 @ 11:10 am

    @Devang Paliwal: Absolutely! You’d need to get each service’s IMAP information though.

  12. zeus
    July 11, 2009 @ 9:01 am

    Is it secure? Could this be implemented with accounts contain personal and confidential mails?

  13. July 11, 2009 @ 9:13 am

    @Zues: How you use this is up to you. I don’t recommend making your primary email account public.

  14. July 11, 2009 @ 11:11 am

    Hmmm okay. But this is something great. Can brainstorm and come up with new and cool ideas :)

  15. soso
    July 12, 2009 @ 9:56 am

    Fatal error: Call to undefined
    function imap_open() in
    C:\wamp\www\New folder\index.php on
    line 13

    Can someone help ?

  16. July 13, 2009 @ 12:15 am

    @soso: please enable “extension=php_imap.dll” in php.ini

  17. stephex
    July 13, 2009 @ 12:40 pm

    I want get attachment de Gmail. How? Thanks.

  18. chris
    July 13, 2009 @ 5:04 pm

    Hey this looks awesome!
    I tried to use it with wamp and am getting this error…any ideas? Thanks a lot for any help!!

    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in C:\wamp\www\email\howdy.php on line 13
    Cannot connect to Gmail: Can’t open mailbox {imap.gmail.com:993/imap/ssl}INBOX: invalid remote specification

  19. July 14, 2009 @ 11:54 am

    @chris: Not sure, could be an outbound connection or firewall issue. “Localhost” always bring trouble.

  20. chris
    July 14, 2009 @ 11:56 am

    Thanks David, you were correct it was a firewall issue that blocked 993.

  21. July 18, 2009 @ 4:43 am

    is this works for inline attachments such as picture added inside the text through outlook express?
    Great stuff!

  22. iván
    July 20, 2009 @ 5:57 pm

    Q? Why I get this error : Cannot connect to Gmail: Can’t open mailbox {imap.gmail.com:993/imap/ssl}INBOX: invalid remote specification

  23. chris
    July 20, 2009 @ 6:30 pm

    @Iván: I had this same error, it’s your firewall settings blocking 993 port. When you unblock it, it works.

  24. iván
    July 20, 2009 @ 6:56 pm

    I´ve fixed my problem.

    Thank You Chris.

  25. stylianos
    July 22, 2009 @ 2:24 pm

    Thank you so much you are genius!!!

  26. nimz
    July 29, 2009 @ 2:31 pm

    great idea,
    demo is down though, “Cannot connect to Gmail: Too many login failures”

  27. July 29, 2009 @ 2:35 pm

    @nimz: Ah, a Chelsea boy. Nice. Since I posted the article the accounts been hacked at. I’ll see if I can restore it.

  28. August 31, 2009 @ 7:20 am

    Cannot connect to Gmail: Can’t open mailbox {imap.gmail.com:993/imap/ssl}INBOX: invalid remote specification – this error i get, when i run this code in localhost means local system..

  29. el santo!
    September 4, 2009 @ 4:41 pm

    dude.. awesome job.. but im unsing latin characters and i got ugly formed emails.. there is any parameter for change the charset?

  30. ben
    September 6, 2009 @ 2:25 pm

    This doesn’t work for me for some reason. I’m running it locally on Wamp, and I turned the firewall off… I can get the email subjects but when I click on them they don’t do anything.

  31. September 18, 2009 @ 10:43 am

    maybe Im missing something but I could not find a download link to download this amazing script.

    I downloaded the mootools.js file into a dir on my cpanel server. Then created a new file and think I put the above codes into the right place etc but when i try to load the page,it just sits there saying loading in the status bar, nothing happens, been waiting 9 minutes without error message.

    any ideas please or am i missing a download link to download all files needed.

    cheers

    Dextor

  32. September 18, 2009 @ 10:47 am

    Dextor: All of the necessary code is here. I’m guessing that your machine is having a problem connecting to GMail, or GMail is slow. The fact that it tries for a while instead of just dying means it’s attempting to get to Gmail. You may also want to change your username and password.

  33. September 18, 2009 @ 10:50 am

    wow quick response, thanks David, can i pastbin my file so you can see if i have it setup right?

  34. September 18, 2009 @ 10:53 am

    here is the pastebin.ca link

    http://pastebin.ca/1570904

  35. September 18, 2009 @ 11:05 am

    @Dextor: You need to make your code a complete page with ,, etc. I also don’t see you bringing in the MooTools library.

  36. September 18, 2009 @ 11:18 am

    This look better?

    dreamweaver code color says the css / mootools part looks odd / wrong color like the syntax is out of place / incorrect

    http://pastebin.ca/1570922

  37. gordie
    September 23, 2009 @ 12:29 pm

    I get this error….

    Cannot connect to imap: Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/O=Google Inc/CN=Google Internet Authority

  38. noe rodriguez
    October 2, 2009 @ 3:07 pm

    How to solve the encoding problem’s???

    Thank’s if you have an answer!

  39. October 5, 2009 @ 6:11 pm

    I get a bunch of garbage that looks like a SSL certificate after the first message and the body does not print on remaining emails. Any ideas?

    –1248921015.4E4FF544.18054 Date: Wed, 29 Jul 2009 19:30:15 -0700 MIME-Version: 1.0 Content-Type: image/gif Content-Transfer-Encoding: base64 Content-Disposition: inline; filename=”email-vox-logo.gif” Content-ID: R0lGODlhawAtAOYAANcZIP////fR0v/8/Od1edgeJdopMPO7vd05P/309f739/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4wBstM8MoNOQEGSFJBdIggKiF5Ad+IAd DUFgRZYwwHIwIWFKMOKm4CGIHDCAQgFZgRCkaIE2VWMu23kABMaoH0ZVJGAfIIgdAGxAALwNQgFW UGIpfnCC8g3CBTHQAbfQU4Ie3EsRD6CBCiAJAA0MQYqCqIERbmiKAoQghZ9cxCGAuIRPGCIKXRnZ JTywzBwcAh8kEAAOlJKtSAxRCjMQQF0qEggAOw== –1248921015.4E4FF544.18054 Date: Wed, 29 Jul 2009 19:30:15 -0700 MIME-Version: 1.0 Content-Type: image/gif Content-Transfer-Encoding: base64 Content-Disposition: inline; filename=”spacer.gif” Content-ID: R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw== –1248921015.4E4FF544.18054–

  40. noe rodriguez
    October 5, 2009 @ 7:23 pm

    Bob Snider : If the email has an attachment you will see that. The code in this place is just an idea to make a better code. Check the Imap functions at php.net . I’m still working in this, the idea is good but not complete.

  41. October 7, 2009 @ 12:03 am

    hi this is superb script…

    but i got some errors

    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/ospreyan/public_html/kushal/mail.php on line 8
    Cannot connect to Gmail: Can’t connect to gmail-imap.l.google.com,993: Connection refused

    can you help me in that

  42. deepak
    October 12, 2009 @ 7:24 am

    i used ur exact code. there is no error but half of the code is printed instead of the emails.
    here is the code i took from u exactly. try to run it with ur username and password. does it run correct. change the username and password. thanks anybody.

    <?
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'yourusername@gmail.com';
    $password = 'yourpassword';
    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to

    Gmail: ' . imap_last_error());
    /* grab emails */
    $emails = imap_search($inbox,'ALL');
    /* if emails are returned, cycle through each… */
    if($emails) {
    /* begin output var */
    $output = '';
    /* put the newest emails on top */
    rsort($emails);
    print_r($emails);
    /* for every email… */
    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 header information */
    $output.= 'seen ? “read” : “unread”).’”>’;
    $output.= ”.$overview[0]->subject.’ ‘;
    $output.= ”.$overview[0]->from.”;
    $output.= ‘on ‘.$overview[0]->date.”;
    $output.= ”;
    /* output the email body */
    $output.= ”.$message.”;
    }
    echo $output;
    }
    /* close the connection */
    imap_close($inbox);
    ?>

  43. October 12, 2009 @ 11:01 am

    If you are getting a lot of weird characters in the message body (i.e. =0D or =0A) then you have to check the encoding with imap_fetchstructure() and then decode it properly. You only need to decode for Quoted-Printable and Base64, however. Here is my code for this:

    $structure = imap_fetchstructure($mailbox, $uid, FT_UID);
    if($structure->encoding == “3″){
    $body = base64_decode(imap_fetchbody($mailbox, imap_msgno($mailbox, $uid), 1));
    }
    elseif($structure->encoding == “4″){
    $body = imap_qprint(imap_fetchbody($mailbox, imap_msgno($mailbox, $uid), 1));
    }else{
    $body = imap_fetchbody($mailbox, imap_msgno($mailbox, $uid), 1);
    }

    Note that I use UIDs, so this will have to be changed for however you are doing it.

  44. swati
    October 13, 2009 @ 2:21 am

    @deepak: Fatal error: Call to undefined function imap_open() in C:\wamp\www\swaticlass\imapp.php
    Im geeting this errior could nyone solve it

  45. October 14, 2009 @ 5:20 am

    Cery cool script !!! But I notice that is very very slow to load. Why ?

  46. chris
    October 20, 2009 @ 12:02 am

    Hi dave, I tried your exact code, but the javascript didn’t work. I’m using firefox 3.0.14 – php5 – apache2. All the email body (the messages) are all listed before i have to click the email subject. Any suggestion?

    This is my phpcode:
    <?php
    $formatcss = "

    “;
    echo $formatcss;

    $javascript = ”
    window.addEvent(‘domready’,function() {
    var togglers = $$(‘div.toggler’);
    if(togglers.length) var gmail = new Fx.Accordion(togglers,$$(‘div.body’));
    togglers.addEvent(‘click’,function() { this.addClass(‘read’).removeClass(‘unread’); });
    togglers[0].fireEvent(‘click’); //first one starts out read
    });
    “;
    echo $javascript;

    /* connect to gmail */
    $hostname = ‘{imap.gmail.com:993/imap/ssl}INBOX’;
    $username = ‘username@gmail.com’;
    $password = ‘password’;

    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die(‘Cannot connect to Gmail: ‘ . imap_last_error());

    /* grab emails */
    $emails = imap_search($inbox,’ALL’);

    /* if emails are returned, cycle through each… */
    if($emails) {

    /* begin output var */
    $output = ”;

    /* put the newest emails on top */
    rsort($emails);

    /* for every email… */
    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 header information */
    $output.= ‘seen ? ‘read’ : ‘unread’).’”>’;
    $output.= ”.$overview[0]->subject.’ ‘;
    $output.= ”.$overview[0]->from.”;
    $output.= ‘on ‘.$overview[0]->date.”;
    $output.= ”;

    /* output the email body */
    $output.= ”.$message.”;
    }

    echo $output;
    }

    /* close the connection */
    imap_close($inbox);
    ?>

    thanks

  47. eicky
    October 20, 2009 @ 4:29 am

    hi,
    I got this error..Call to undefined function imap_open()
    I enabled “extension=php_imap.dll” in php.ini
    stil I have the same problem

  48. novadash
    October 22, 2009 @ 11:10 pm

    @chris: how to blocked the 993 port on win xp…i had been added the port on exception, but still not working…
    thanks…!!

  49. novadash
    October 22, 2009 @ 11:16 pm

    @David Walsh: one more…
    i used wamp on my server with localhost,…
    and the error is stil “couldn’t not open stream”…
    my server spec is : php. 5.2 and apache 2
    if i replaced the php_imap.dll with the new one from php.5.3..the error is “Call to undefined function imap_open()”..even the php_imap extension is still enable on php.ini..
    can u explain how to setting the php.ini or the firewall setup…please mail me…ty..

  50. October 26, 2009 @ 11:16 pm

    hi this is superb script…

    but i got some errors

    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/ospreyan/public_html/kushal/mail.php on line 8
    Cannot connect to Gmail: Can’t connect to gmail-imap.l.google.com,993: Connection refused

    can you help me in that

  51. November 2, 2009 @ 2:50 pm

    If you are using localhost and having problems, look into XAMPP instead of WAMP

  52. saif
    November 4, 2009 @ 1:52 am

    this is very good idea…..

  53. saif
    November 4, 2009 @ 2:02 am

    Can you help me to take the html code of each mail….. and also need to display only the unreaded messages ….. please help me sir…..

  54. November 9, 2009 @ 4:59 am

    It doesn’t seem to work with Danish characters, any suggestions?

  55. November 20, 2009 @ 1:54 am

    its working well for me .. i m using this with my own server not with gmail. thanks for this great effort…..

  56. axel
    December 6, 2009 @ 11:29 am

    great, thank you!

  57. December 17, 2009 @ 1:22 am

    hi david,

    can we use jquery instead of using mootools javascript.

  58. marvinstefani
    December 19, 2009 @ 6:32 pm

    if u have problem with certificate, add /novalidate-cert
    imap.gmail.com:993/imap/ssl/novalidate-cert

    if you cant connect then its the port 993 which is blocked (outbound blocking)

    i have great ideas to use this, muahahahaaha(evil laughter)

  59. December 20, 2009 @ 11:18 pm

    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in \\hmfsw\web\dtcwin071\6700.com.ar\public_html\ver\mail.php on line 8
    Cannot connect to Gmail: Can’t connect to gmail-imap.l.google.com,993: Host unreachable the php say’s that, i be read about this and say’s “993 port is locked by firewall” what i can do?

  60. old guy
    December 26, 2009 @ 2:32 pm

    @deepak: your PHP implementation probably requires <?php, not just <? on the first line

  61. old guy
    December 26, 2009 @ 2:36 pm

    @Bob Snider: Its an atachment – you need to read up on mime and how to parse email

  62. January 5, 2010 @ 2:08 am

    david, amazing! can it retrieve also the sent mails? or even user generated folders? i will be studying this IMAP thing.. thanks for the great idea! feed on!

  63. stubleu
    January 22, 2010 @ 1:07 am

    @David Walsh: i seem to be having the same problem as nimz where i’m getting “Cannot connect to Gmail: Too many login failures” even though my accounts are perfectly fine?

  64. igor
    March 1, 2010 @ 4:57 pm

    Thank you, David, very useful stuff.
    Something strange in folder structure, where does Google store spam…

  65. March 11, 2010 @ 12:51 am

    Hi David,

    Just wanted to know if we could also acquire the attachments with the email as well.

    If this is possible please let me know..

    In advance,

    Thanks for your help..

  66. mango
    March 19, 2010 @ 2:11 am

    hey david can you please post the whole script here at your blog, because i think your codes are incomplete and i cannot see sliding animation in my page…

    Thanks

  67. March 20, 2010 @ 3:20 pm

    Wel the script is 100% working, but i want to know how to paginate the inbox to multiple pages, can anybody tell me?

  68. March 30, 2010 @ 8:23 am

    Interesting technique, and I like how you included the accordian to make things more interesting.

    Do we really need another PHP mail app though? :) Integration possibilities are endless though of course.

  69. craig
    April 4, 2010 @ 4:25 am

    Excellent lesson. I have a question. Im using it to retreive an overview of my gmail into my private intranet. Works great but I would like to make every subject link back to the gmail account directly.. i know i need to get some kind of ID for each mail then redirect to mail.google.com/#inbox or something.. any ideas?

  70. April 6, 2010 @ 3:48 pm

    Thanks david really helpfull

  71. prashant
    April 7, 2010 @ 12:49 am

    @Iván: Ivan its prob with your config file…go to php.ini and enable (extension_imap blah blah)…and you are done…

  72. April 24, 2010 @ 2:28 pm

    Hi,
    First of: Great code example, gets a man up and running in no time! Big thanks.
    Second: have you been folowing the release of gMail IMAP + oAuth?
    That opens the door to all sorts of weird and wonderful opertunities… anything from “Open your gmail from my website” to a Gmail Facebook Tab (because, lets be honest, the current implementations suck :-)

    greetz,
    P.

  73. dan
    May 15, 2010 @ 3:46 pm

    I saved the php code in a TextWrangler document and uploaded it, but on my localhost (MAMP with an IMAP code edit) that delivers a blank page and on x10hosting, I get the following:

    Parse error: syntax error, unexpected $end in /home/r5radar/public_html/walsh.php on line 70

    (I have a thorough understanding of HTML, have some experience with XML, and am just learning php)

    How should the accordion, CSS and php codes be saved? I just want to get this awesome code to work before I embed it in an actual page or design…

  74. May 16, 2010 @ 4:02 am

    @eicky: i think you need to copy the dll into your php directory as well…

  75. dan
    May 16, 2010 @ 7:05 am

    Alright I think I have the ssl and firewall issues solved (I’m on a mac), but I’ve come to an unexpected T_STRING error on line 55 I can’t see. Here’s my code:

    Gmail to RSS test

    window.addEvent(‘domready’,function() {
    var togglers = $$(‘div.toggler’);
    if(togglers.length) var gmail = new Fx.Accordion(togglers,$$(‘div.body’));
    togglers.addEvent(‘click’,function() { this.addClass(‘read’).removeClass(‘unread’); });
    togglers[0].fireEvent(‘click’); //first one starts out read
    });

    <?php
    $formatcss = "

    ";
    echo $formatcss;

    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'route5radarproxy@gmail.com';
    $password = 'gothrough';

    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    /* grab emails */
    $emails = imap_search($inbox,'ALL');

    /* if emails are returned, cycle through each… */
    if($emails) {

    /* begin output var */
    $output = ";

    /* put the newest emails on top */
    rsort($emails);

    /* for every email… */
    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 header information */
    $output.= 'seen ? ‘read’ : ‘unread’).’”>’;
    $output.= ”.$overview[0]->subject.’ ‘;
    $output.= ‘on ‘.$overview[0]->date.”;
    $output.= ”;

    /* output the email body */
    $output.= ”.$message.”;
    }

    echo $output;
    }

    /* close the connection */
    imap_close($inbox);
    ?>

  76. dan
    May 16, 2010 @ 7:19 am

    /*

    Gmail to RSS test

    window.addEvent(‘domready’,function() {
    var togglers = $$(‘div.toggler’);
    if(togglers.length) var gmail = new Fx.Accordion(togglers,$$(‘div.body’));
    togglers.addEvent(‘click’,function() { this.addClass(‘read’).removeClass(‘unread’); });
    togglers[0].fireEvent(‘click’); //first one starts out read
    });

    <?php
    $formatcss = "

    ";
    echo $formatcss;

    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'route5radarproxy@gmail.com';
    $password = 'gothrough';

    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

    /* grab emails */
    $emails = imap_search($inbox,'ALL');

    /* if emails are returned, cycle through each… */
    if($emails) {

    /* begin output var */
    $output = ";

    /* put the newest emails on top */
    rsort($emails);

    /* for every email… */
    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 header information */
    $output.= 'seen ? ‘read’ : ‘unread’).’”>’;
    $output.= ”.$overview[0]->subject.’ ‘;
    $output.= ‘on ‘.$overview[0]->date.”;
    $output.= ”;

    /* output the email body */
    $output.= ”.$message.”;
    }

    echo $output;
    }

    /* close the connection */
    imap_close($inbox);
    ?>

    */

  77. naresh kumar
    May 20, 2010 @ 9:09 am

    @Dan: Please help me Dan

    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in C:\xampp\htdocs\POC\test.php on line 24
    Cannot connect to Gmail: Can’t open mailbox {imap.gmail.com:993/imap/ssl}INBOX: invalid remote specification

    it was a firewall is connecting 993.

    Still i am getting above error

    Regards,
    Naresh

  78. vinoth s
    July 2, 2010 @ 12:29 am

    Hi all,

    David ur script really nice, But i want mail download from pop3, if u have any idea for that, Please help me…

    Thanks & Regards,
    Vinoth S

  79. vinoth s
    July 2, 2010 @ 12:35 am

    I’m download the mail from local(means my company) mail server, and the values are spilit and stored into DB, But its a POP amail server, so if u have a idea or something please help me….

  80. gerrger
    July 29, 2010 @ 1:54 am

    How modify code only view 5 last new mail receive on gmail inbox?

  81. smk
    August 2, 2010 @ 8:28 am

    hai david i tried lot of time.but it will not connect with gmail.it say folling errors

    {imap.gmail.com:993/imap/ssl}INBOX in /home/ospreyan/public_html/kushal/mail.php on line 8
    Cannot connect to Gmail: Can’t connect to gmail-imap.l.google.com,993: Connection time out

  82. varsha
    August 5, 2010 @ 5:07 am

    Hi, i m receiveing this
    Warning: imap_open() [function.imap-open]: Couldn’t open stream {imap.gmail.com:993/imap/ssl}INBOX in /home/uppstu5/public_html/test_gmail.php on line 8
    Cannot connect to Gmail: Too many login failures
    Plz help me

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!