Dragen Posted December 9, 2007 Share Posted December 9, 2007 Hi, I was wondering if it was possible to have php connect to a pop3 email server and download messages from it and store them (probably to a mysql database), so I can run an email system, sending receiving emails and also replying to them. I can easily write a send mail script I'm just not sure about receiving. Thanks Quote Link to comment Share on other sites More sharing options...
BradleyBrokers Posted February 1, 2008 Share Posted February 1, 2008 Hi Dragen, Good idea. What you are attempting is feasable. I am attempting it too. I hope the code I have enclosed will be helpful. I am working on this same possibillity with a few extra bells-and-whistles. I would like my website, www.BradleyBrokers.com , to read my yahoo email, sort it (by keywords in the Subject line), and reply with various replys depending upon the keyword.... Perhaps we can help eachother? Cheers, Tom Try this to read the email: <? PHP // The ultimate goal is to read, sort, file certain information into a mysql database, and selectively reply. // This is the first part. I am sharing it with you Dragen and anyone else.... Feel the love. // For all others, if it doesn't work, please don't be a snot. Either help-out or go somewhere else. define("SERVER", "YourServerName.com"); define("USER", "YourEmailAddress@yahoo.com"); define("PASS", "YourPassWord"); // The below should connect to yahoo $connection_array = POP3_connect(SERVER, USER, PASS); $POP3_connection = $connection_array['handle']; if($POP3_connection) { $list_array = POP3_list($POP3_connection); // The next portion of the script creates requests for each email message. // Then the script displays the messages in the $list_array. for($xx=0; $xx<count($list_array); $xx++) { // Parsing the mail ID from the message size. list($mail_id, $size) = explode(" ", $list_array[$xx]); // Requesting message for the mail ID. $message = POP3_retr($POP3_connection, $mail_id); // Desplaying it in an array echo "mail_id, $size\n"; $mail_array[$xx]['ID'] = $mail_id; $mail_array[$xx]['SIZE'] = $size; $mail_array[$xx]['MESSAGE']; = $message; $mail_array[$xx]['SUBJECT']; = $subject; $mail_array[$xx]['SENDER']; = $sender; // Disabling HTML tags in order to display message in <xmp></xmp> tags. // This will permit the script to be run in a browser. // Then the session ends. echo "<xmp>$message</xmp>"; echo "<xmp>$subject</xmp>"; echo "<xmp>$sender</xmp>"; echo POP3_quit($POP3_connection); } else { echo "Sorry, you made a login mistake or another error exists."; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 1, 2008 Share Posted February 1, 2008 Look at the IMAP functions. This set of functions will also do the POP protocol. Ken Quote Link to comment Share on other sites More sharing options...
BradleyBrokers Posted February 1, 2008 Share Posted February 1, 2008 Hi, Good thought. I looked at the IMAP thread as you suggested... This post (copied and pasted) from "Ananymous" might be of interest: <HTML> <HEAD> <TITLE>imap_check</TITLE> </HEAD> <BODY> <? //check for new messages $mailbox = imap_open("{localhost/pop3:110}INBOX", "#username#","#password#"); // Check messages $check = imap_check($mailbox); print("<PRE>"); print("Date most recent message : " . $check->Date); print("<BR>"); print("Connection type : " . $check->Driver); print("<BR>"); print("Name of the mailbox : " . $check->Mailbox); print("<BR>"); print("Number of messages : " . $check->Nmsgs); print("<BR>"); print("Number of recent messages : " . $check->Recent); print("<BR>"); print("</PRE>"); // show headers for messages $index=1; $header = imap_header($mailbox, $index); print("<PRE>"); print("Header Date : " . $header->Date . "<BR>"); print("Header To : " . $header->to) . "<BR>"; print("Header From : " . $header->From . "<BR>"); print("Header cc : " . $header->cc . "<BR>"); print("Header ReplyTo : " . $header->ReplyTo . "<BR>"); print("Header Subject : " . $header->Subject . "<BR></PRE>"); print("<PRE>"); print(imap_body($mailbox,$index)); print("</PRE><HR>"); imap_close($mailbox); ?> </BODY></HTML> (edited by kenrbnsn to add tags) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 1, 2008 Share Posted February 1, 2008 Whenever you post code to the forum, please surround it with tags. Ken Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.