dongliehuan3925 2019-05-30 08:36
浏览 116

使用PHP和IMAP获取gmail部分的列表

I am trying to get past an attachment and get the text in the body of a gmail.

I have tried:

$message = imap_qprint(imap_fetchbody($inbox,$email_number,""));

And have attempted using several different part #s, but if there is an attachment, even if there is a message in the body, all I get is text that looks like this:

gu1OyQR8H/cpWM3Vnmv2R9O+ymSvBRIImhqaK7EqPaH8GUtxwyfxHhc6JjiM6nmEyMzIJ/jcameckzqQL

I have tried to get all the part #s of the gmail, but I am not sure what the code is for that.

If I knew what the part # is for the body of the email I might be able to retrieve the text of the body


EDIT

Here is the code to access the gmail email:

<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'user';
$password = 'pass';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

$emails = imap_search($inbox,"UNSEEN");

if($emails) {
$output = '';

rsort($emails);

foreach($emails as $email_number) {

$headerInfo = imap_headerinfo($inbox,$email_number);
$structure = imap_fetchstructure($inbox, $email_number);

$overview = imap_fetch_overview($inbox,$email_number,0);

$message = imap_qprint(imap_fetchbody($inbox,$email_number,"1.2"));

$output .= 'Subject: '.$overview[0]->subject.'<br />';
$output .= 'Body: '.$message.'<br />';
$output .= 'From: '.$overview[0]->from.'<br />';
$output .= 'Date: '.$overview[0]->date.'<br />';
$output .= 'CC: '.$headerInfo->ccaddress.'<br />';

$status = imap_setflag_full($inbox, $overview[0]->msgno, "\\Seen \\Flagged");
}

echo $output;
}

imap_close($inbox);
?>

And here is the output:

Subject: Test
Body: 
From: Test Acct
Date: Thu, 30 May 2019 11:59:22 -0400
CC:

Notice that the Body above is empty, but in the email there is displayed text:

Dear Valued Partner,

Loan No.23322 has been successfully locked with a Final Price of -.821%. Please see the attached Lock Confirmation for details. You may also pick the document up on EASE. ...

However, in the source I see this instead:

RGVhciBWYWx1ZWQgUGFydG5lciwgPGJyLz4NCg0KPGJyLz4NCkxvYW4gTm8uMTIxOTE4NjIyMSBo
YXMgYmVlbiBzdWNjZXNzZnVsbHkgbG9ja2VkIHdpdGggYSBGaW5hbCBQcmljZSBvZiAtLjgyMSUu
IFBsZWFzZSBzZWUgdGhlIGF0dGFjaGVkIExvY2sgQ29uZmlybWF0aW9uIGZvciBkZXRhaWxzLiBZ
b3UgbW

So it appears as if the text in the body of the email is encrypted and has nothing to do with the IMAP code.

If I forward the email to the same email address, when I check that message, this line of code works:

$message = imap_qprint(imap_fetchbody($inbox,$email_number,"1.2"));

because in the source of the forwarded email the text in the body of the email displays normally, without the original encryption

EDIT 2


Adding this line of code did the trick:

 $message1 = imap_base64($message);
  • 写回答

1条回答 默认 最新

  • drvpv7995 2019-05-30 17:09
    关注

    For anybody who has the same issue, here is the code that works:

    <?php
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'user';
    $password = 'pass';
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
    
    $emails = imap_search($inbox,"UNSEEN");
    
    if($emails) {
    $output = '';
    
    rsort($emails);
    
    foreach($emails as $email_number) {
    
    $headerInfo = imap_headerinfo($inbox,$email_number);
    $structure = imap_fetchstructure($inbox, $email_number);
    
        $overview = imap_fetch_overview($inbox,$email_number,0);
    
        $message = imap_qprint(imap_fetchbody($inbox,$email_number,"1"));
    
    //since the incoming email was encrypted with base64, I insert this line below to decode it, and the above part number, "1", will work. This was the key
    $message1 = imap_base64($message);
    
    $output .= 'Subject: '.$overview[0]->subject.'<br />';
    $output .= 'Body: '.$message1.'<br />';
    $output .= 'From: '.$overview[0]->from.'<br />';
    $output .= 'Date: '.$overview[0]->date.'<br />';
    $output .= 'CC: '.$headerInfo->ccaddress.'<br />';
    
    $status = imap_setflag_full($inbox, $overview[0]->msgno, "\\Seen \\Flagged");
    }
    
    echo $output;
    }
    
    /* close the connection */
    imap_close($inbox);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看