fgtsai0418 Posted August 2, 2013 Share Posted August 2, 2013 This code can fetch Gmail all mail. But how to retrieve particular "domain name" like @123.xstartwar.com or particular "message" which title have "Mobile Position Report" <? set_time_limit(0); //Your gmail email address and password $username = 'name@gmail.com'; $password = 'pwd'; //Which folders or label do you want to access? - Example: INBOX, All Mail, Trash, labelname //Note: It is case sensitive $imapmainbox = "INBOX"; //Select messagestatus as ALL or UNSEEN which is the unread email $messagestatus = "UNSEEN"; //------------------------------------------------------------------- //Gmail Connection String $imapaddress = "{imap.gmail.com:993/imap/ssl}"; //Gmail host with folder $hostname = $imapaddress . $imapmainbox; //Open the connection $connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); //Grab all the emails inside the inbox $emails = imap_search($connection,$messagestatus); //number of emails in the inbox $totalemails = imap_num_msg($connection); echo "Total Emails: " . $totalemails . "<br>"; if($emails) { //sort emails by newest first rsort($emails); //loop through every email int he inbox foreach($emails as $email_number) { //grab the overview and message $header = imap_fetch_overview($connection,$email_number,0); //Because attachments can be problematic this logic will default to skipping the attachments $message = imap_fetchbody($connection,$email_number,1.1); if ($message == "") { // no attachments is the usual cause of this $message = imap_fetchbody($connection, $email_number, 1); } //split the header array into variables $status = ($header[0]->seen ? 'read' : 'unread'); $subject = $header[0]->subject; $from = $header[0]->from; $date = $header[0]->date; echo "status: " . $status . "<br>"; echo "from: " . $from . "<br>"; echo "date: " . $date . "<br>"; echo "message: " . $message . "<br><hr><br>"; //This is where you would want to start parsing your emails, send parts of the email into a database or trigger something fun to happen based on the emails. } } // close the connection imap_close($connection); ?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 2, 2013 Share Posted August 2, 2013 Check the list of options for imap_search, there are several criteria you can tell it to search for. http://www.php.net/manual/en/function.imap-search.php Link to comment Share on other sites More sharing options...
fgtsai0418 Posted August 3, 2013 Author Share Posted August 3, 2013 (edited) Hi I want use imap_search to sort what I want , but it not working. can somebody help? <? set_time_limit(0); //Your gmail email address and password $username = 'name@gmail.com'; $password = 'pwd'; //Which folders or label do you want to access? - Example: INBOX, All Mail, Trash, labelname //Note: It is case sensitive $imapmainbox = "INBOX"; //Select messagestatus as ALL or UNSEEN which is the unread email $messagestatus = "UNSEEN"; //------------------------------------------------------------------- //Gmail Connection String $imapaddress = "{imap.gmail.com:993/imap/ssl}"; //Gmail host with folder $hostname = $imapaddress . $imapmainbox; //Open the connection $connection = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); //Grab all the emails inside the inbox //$emails = imap_search($connection,$messagestatus); $emails = imap_search($connection,$messagestatus,'FROM, "c12.mobile.net"' ); //number of emails in the inbox $totalemails = imap_num_msg($connection); echo "Total Emails: " . $totalemails . "<br>"; if($emails) { //sort emails by newest first rsort($emails); //loop through every email int he inbox foreach($emails as $email_number) { //grab the overview and message $header = imap_fetch_overview($connection,$email_number,0); //Because attachments can be problematic this logic will default to skipping the attachments $message = imap_fetchbody($connection,$email_number,1.1); if ($message == "") { // no attachments is the usual cause of this $message = imap_fetchbody($connection, $email_number, 1); } //split the header array into variables $status = ($header[0]->seen ? 'read' : 'unread'); $subject = $header[0]->subject; $from = $header[0]->from; $date = $header[0]->date; echo "status: " . $status . "<br>"; echo "from: " . $from . "<br>"; echo "date: " . $date . "<br>"; echo "message: " . $message . "<br><hr><br>"; } } // close the connection imap_close($connection); ?> Edited August 4, 2013 by fgtsai0418 Link to comment Share on other sites More sharing options...
fgtsai0418 Posted August 5, 2013 Author Share Posted August 5, 2013 case close Link to comment Share on other sites More sharing options...
Net123 Posted August 7, 2013 Share Posted August 7, 2013 then try with "subject".... Link to comment Share on other sites More sharing options...
fgtsai0418 Posted August 7, 2013 Author Share Posted August 7, 2013 I have figure out this problem. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now