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

时间:2019-05-30 08:36:36

标签: php

我试图绕过附件,并在gmail正文中获取文本。

我尝试过:

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

并尝试使用几个不同的部分#,但是即使有附件,即使正文中有一条消息,我所得到的也是这样的文本:

  

gu1OyQR8H / cpWM3Vnmv2R9O + ymSvBRIImhqaK7EqPaH8GUtxwyfxHhc6JjiM6nmEyMzIJ / jcameckzqQL

我试图获取gmail的所有部分,但是我不确定代码是什么。

如果我知道电子邮件正文的#号是什么,我也许可以检索正文文本


编辑

以下是访问 gmail 电子邮件的代码:

<?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);
?>

这是输出:

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

请注意,上面的正文为,但电子邮件中显示的是 文本:

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

但是,在中,我看到的却是这样:

RGVhciBWYWx1ZWQgUGFydG5lciwgPGJyLz4NCg0KPGJyLz4NCkxvYW4gTm8uMTIxOTE4NjIyMSBo
YXMgYmVlbiBzdWNjZXNzZnVsbHkgbG9ja2VkIHdpdGggYSBGaW5hbCBQcmljZSBvZiAtLjgyMSUu
IFBsZWFzZSBzZWUgdGhlIGF0dGFjaGVkIExvY2sgQ29uZmlybWF0aW9uIGZvciBkZXRhaWxzLiBZ
b3UgbW

因此,似乎电子邮件正文中的文本已加密,并且与IMAP代码无关。

如果我转发电子邮件至相同电子邮件地址,则当我检查消息时,此代码行有效:

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

因为在转发的电子邮件的来源中,电子邮件正文中的文本正常显示,没有原始加密

编辑2


添加以下代码即可达到目的:

 $message1 = imap_base64($message);

1 个答案:

答案 0 :(得分:0)

对于有相同问题的任何人,以下是有效的代码:

<?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);
?>