Jump to content

fetch particular "domain name" or particular "message"


fgtsai0418

Recommended Posts

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

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 by fgtsai0418
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...