Retrive your Emails using PHP

Sending email is very simple in PHP using mail() function. what about receiving it? Retrive your Emails using PHP and IMAP. IMAP stands for Internet Message Access Protocol. It is a method of accessing electronic mail that are kept on a mail server.

Email stored on an IMAP server can be manipulated from a desktop computer at home, office, and a notebook computer while traveling.

Retrieving emails from your account using PHP is very easy.

Connect to a Mailbox:
$host = '{}INBOX';//For Yahoo
//$host = '{}INBOX';//For Gmail
$user = '';
$password = '';

$mbox = imap_open($host,$user,$password) or die('Cannot connect: ' . imap_last_error());

This opens an IMAP connection to the server. It also passes along a username and password as the second and third arguments.

//Number of mails.
$size = imap_num_msg($mbox );

//Put the newest email on top.

$headers = imap_headerinfo($mbox, $i);

$subject = $headers->subject;//Subject of Mail.
//From Email ID
$from_email = $headers->fromaddress;
//To Email ID
$to_email = $headers->toaddress;
//CC Email ID
$cc_email = $headers->ccaddress;
//Email Messaage.
$body = imap_fetchbody($mbox,$i,2);




No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: