Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

IMAP Mail Reading With PHP3
We'll make a nice and simple 3-script based mail reader.
The first script will be to read in your IMAP username and password. We will use the standard PHP authentication for that.
login.php3 contains:

<?php

if (!$PHP_AUTH_USER) {
    
Header("WWW-authenticate: basic realm=\"Mail Chek\"");
    
Header("HTTP/1.0 401 Unauthorized");
} else {
    
$MYDIR=ereg_replace("/[^/]+$","",$PHP_SELF);
    
Header("Location: $SERVER_NAME$MYDIR/messages.php3");
}

?>
This simply reads in your username and password and redirects you to the read mail reading page.
messages.php3 contains:

<?php

$MAILSERVER
="{localhost/imap}";
$link=imap_open($MAILSERVER,$PHP_AUTH_USER,$PHP_AUTH_PW);
$headers=imap_headers($link);

for(
$x=1; $x < count($headers); $x++) {
    
$idx=($x-1);
    echo
"<a href=\"view.php3?num=$x\">$headers[$idx]</a><br>";
}

?>
It opens up an IMAP connection to the mailserver specified by $MAILSERVER, passing in your username and password.
It then get a list of all the messgae headers and in a loop, prints them all out. Besides printing them out, it also makes each one a link to view.php3, passing it that message number.
view.php3 contains:

<?php

$MAILSERVER
="{localhost/imap}";
$link=imap_open($MAILSERVER,$PHP_AUTH_USER,$PHP_AUTH_PW);
$header=imap_header($link,$num);

echo
"From: $header[fromaddress]<br>";
echo
"To: $header[toaddress]<br>";
echo
"Date: $header[Date]<br>";
echo
"Subject: $header[Subject]<br><br>";
echo
imap_body($link,$num);

?>
view.php3 opens up the IMAP connection the same as above, and gets the mail message's header information and prints it out.
It then reads in the body of the mail message and prints that out to the screen.
Thats it! You now have a functional web based mail reader...Hotmail watch out, here comes php! ;^)
--Mark

[Page 1]  [Page 2]  


Comments:
RE: How I can adduser for mail by phpBaseel06/24/05 18:22
Compile PHP5 with IMAP support on win2K?Gilbert03/24/05 03:38
Can't open streamNilay01/12/05 07:58
excuting IMAP and php in linuxStanley Njei01/07/05 14:10
RE: Cannot add header information Tom12/23/04 15:12
How to create new mail accounts?khanna12/20/04 04:55
php+sendmail on linuxrena12/04/04 00:05
RE: send email about another smtp-servermartin02/12/04 12:39
Error in this script. Fixed here.Larry Hans Arroyo Vargas08/23/03 03:14
RE: How I can read mails from yahoo/indiatimes phpcodings06/19/03 08:46
how 2 install Linux & configure it 4 phpimapshahid03/10/03 03:38
RE: web based mail program like hotmail or yahoosuresh12/25/02 08:44
Cannot add header information Andrea12/02/02 05:06
i can't read my mailspoojammm11/06/02 07:09
attache filevanthath10/24/02 00:32
parsing the message bodyMaria10/21/02 19:39
Specific search in imap_searchvrykyl10/20/02 05:49
RE: about imap_searchJim Peñaloza10/16/02 15:40
How I can read mails from yahoo/indiatimes brige10/16/02 03:23
Delete button?Liam Farr10/11/02 23:01
Provide Login authentication in Win98Romana10/02/02 16:55
RE: about imap_searchalex 09/29/02 00:16
Retrieve mailJane09/22/02 23:26
reading mail from the mail serverjohn09/11/02 02:55
IMAP RELAY AUTHAlejandro08/28/02 19:55
RE: can't read result of imap_header()porkys08/07/02 08:22
RE: create new userDavíð Fannar08/02/02 06:35
"Warning: Couldn't open stream {localhost/...Simon07/29/02 10:17
send email about another smtp-serverChristian07/28/02 10:48
RE: Attachmentslogudotcom07/24/02 01:13
RE: create new usergautam07/21/02 13:39
RE: web based mail program like hotmail or yahoogautam07/21/02 13:34
web based mail program like hotmail or yahooPriya07/17/02 01:07
RE: IMAP on Win98,2000,MEAndrej05/05/02 06:20
create new userhweidi04/19/02 17:46
IMAP on Win98,2000,MEFred Kamwaza04/04/02 09:09
IMAP, how to?Fendy03/08/02 22:40
cannot add header informationhiertjou03/06/02 15:43
imap_createmailbox ?= create new userDapid Candra02/11/02 04:19
how to setup a mail servervidhya02/07/02 11:55
can't read result of imap_header()Rick02/03/02 18:06
Re: ATTACHMENT questionBlackpete02/01/02 10:49
ATTACHMENT question.....Gary01/23/02 18:41
RE: Slow IMAP Login in PHPRolf Holtsmark01/13/02 09:56
POP 3 only email reader (no IMAP Library)Damien01/12/02 16:48
RE: using php with debian Onno Benschop01/09/02 05:42
RE: bad to use imap in phpSteven Chalker01/04/02 16:49
it's Blankmartin12/31/01 01:45
RE: filtersnutkase12/30/01 13:35
bad to use imap in phppham trung dung12/28/01 08:54
How to browse M$Exchange Global Address Book?Andrei12/27/01 09:13
RE: POP3 reader - no imapNaveen Kumar Verma12/23/01 01:51
using php with debian Steven M12/15/01 16:07
filtersapoorv12/14/01 09:42
Couldn't open streamTika Mahata12/13/01 09:30
cannot open a streamTika Mahata12/13/01 08:44
IMAP Ev Michaud11/28/01 21:11
IMAP problemsZmith11/23/01 05:00
RE: Slow IMAP Login in PHPraghu11/19/01 06:58
Found some problemsSteven Chalker11/18/01 12:42
RE: Mark a message as READ or SEEN?Lewis White11/16/01 07:08
RE: Web-based emailDave G11/15/01 02:01
This lessonDave G11/14/01 22:41
Mark a message as READ or SEEN?Dominic Hess11/11/01 00:58
MySQl Reading DATALuis10/25/01 16:04
PagesSteven C10/24/01 08:19
RE: IMAP on Win98jaya sridhar10/23/01 12:13
RE: POP3 reader - no imapSteven09/15/01 10:07
RE: Web-based emailAashu Savani08/31/01 04:10
imap_createmailbox ()?libo200008/29/01 23:16
imap_createmailbox ()?libo08/29/01 23:13
RE: IMAP on Win98Alejandro08/14/01 15:33
RE: Attachmentssara08/11/01 15:11
Secure loginKing Rollo08/10/01 08:24
POP3 reader - no imapArgile08/09/01 06:22
Attachments filenamesAndreas Wedberg08/06/01 11:08
header lost ..nikkamal08/02/01 05:54
IMAP vs POP3nuit08/01/01 15:00
RE: How to install IMAP server on win2000 ??VDE07/27/01 02:20
RE: Web-based emailSantu07/25/01 22:48
imap on linuxMuhammad Arif07/11/01 01:50
RE: AutoRespondersdwayne06/19/01 08:18
how to use imap_open in windows ntmanisha06/17/01 22:25
AutoRespondersThomas A. Dunlap06/17/01 17:34
RE: How I can adduser for mail by phpkarthikeyan06/15/01 07:29
Help:How to create new mail accounts?Aravind06/04/01 04:08
email grabberFrancesco06/04/01 00:21
php mail systemleriErdil06/01/01 23:41
RE: How I can adduser for mail by phpshivkumar04/05/01 14:29
How to install IMAP server on win2000 ??chris04/02/01 22:25
imap and Empty MailboxesPaul Goodyear03/13/01 09:12
RE: PROBLEM in email reading using phpChingching Lau03/11/01 12:34
RE: how to create new mail box using php-imapKishy03/11/01 11:02
about imap_searchwind03/10/01 03:07
RE: Total Web based email solution.Horst venzke02/13/01 07:25
RE: AttachmentsMichael01/30/01 11:35
AttachmentsJochen Kächelin01/24/01 20:00
RE: How I can adduser for mail by phpJools - french webmaster01/12/01 00:29
RE: How I can adduser for mail by phpSuandi12/29/00 18:07
Total Web based email solution.Mark Berry12/28/00 13:44
RE: Slow IMAP Login in PHPPatrick Niessen12/26/00 20:38
Slow IMAP Login in PHPPatrick Niessen12/22/00 00:37
RE: Web-based emailKilled By PHP12/20/00 08:44
RE: Web-based emaillingder12/10/00 21:44
How I can adduser for mail by phpKampanart 11/27/00 09:51
IMAP on Win98Jose11/14/00 17:59
in PHPJose Morales09/09/00 17:31
in PHPJose Morales09/09/00 17:31
how to create new mail box using php-imapmustafa08/24/00 02:03
Web-based emailktkuan07/14/00 02:39
PROBLEM in email reading using phpAbhishek06/29/00 01:47
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.