从Gmail获取邮件时获取发件人的电子邮件ID

时间:2012-06-15 06:49:49

标签: php email gmail

我从gmail中获取电子邮件,我可以获取正文,发件人姓名等。我必须获取发件人的电子邮件ID,我无法获取。在imap_header获取headerinfo之后我尝试了一些变量名称,比如fromaddress,但是没有用。 我可以得到一些帮助吗?

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?
        }
}

我用几种方法试过运气但每次都失败了...... 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我做到了..Hurray!

以下是代码: -

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.;

        }
}

谢谢:)