-->

Getting e-mail ID of sender while fetching mails f

2020-08-04 09:27发布

问题:

I am fetching email from gmail where I can fetch body, name of the sender etc. I have to get the email-id of the sender, which I am not able to get. I have tried with some variables names like fromaddress after fetchting headerinfo by imap_header but didn't work. Can I get some help regarding this ?

function connect_mail(){

    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = '*****@gmail.com';
    $password = '*****';    
    $inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));   
    $emails = imap_search($inbox,'ALL');    
    $Msgcount = count($emails);
        for ($x = 1; $x <= $Msgcount; $x++)
        {
           $overview = imap_fetch_overview($inbox, $x);
           $title = $overview[0]->subject;
           echo "Subject of the Mail : ".$title."</br>";
           $from = $overview[0]->from;
           echo "Name of the sender : ".$from."</br>";

           //Now I have to get mail ID of senders & print it, but how?
        }
}

I have tried my luck with several methods but got failed each time... Thanks in Advance :)

回答1:

I have done it..Hurray!

Here is the code:-

function connect_mail(){

    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = '*****@gmail.com';
    $password = '*****';    
    $inbox = imap_open($hostname,$username,$password) or die(t('Cannot connect to Gmail: ' . imap_last_error()));   
    $emails = imap_search($inbox,'ALL');    
    $Msgcount = count($emails);
        for ($x = 1; $x <= $Msgcount; $x++)
        {
           $overview = imap_fetch_overview($inbox, $x);
           $title = $overview[0]->subject;
           echo "Subject of the Mail : ".$title."</br>";
           $from = $overview[0]->from;
           echo "Name of the sender : ".$from."</br>";

           $header = imap_headerinfo($inbox, $x);
           $fromaddress = $header->from[0]->mailbox . "@" . $header->from[0]->host;
           echo "From E-Mail Address : ".$fromaddress.;

        }
}

Thanks :)



标签: php email gmail