【问题标题】:Get a listing of the parts of a gmail with PHP and IMAP使用 PHP 和 IMAP 获取 gmail 部分的列表
【发布时间】:2019-10-15 21:26:06
【问题描述】:

我正在尝试跳过附件并获取 gmail 正文中的文本。

我试过了:

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

并尝试使用几个不同的部分#s,但如果有附件,即使正文中有一条消息,我得到的只是看起来像这样的文本:

gu1OyQR8H/cpWM3Vnmv2R9O+ymSvBRIImhqaK7EqPaH8GUtxwyfxHhc6JjiM6nmEyMzIJ/jcameckzqQL

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

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


编辑

这是访问 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. ...

但是,在 source 我看到的是这个:

RGVhciBWYWx1ZWQgUGFydG5lciwgPGJyLz4NCg0KPGJyLz4NCkxvYW4gTm8uMTIxOTE4NjIyMSBo
YXMgYmVlbiBzdWNjZXNzZnVsbHkgbG9ja2VkIHdpdGggYSBGaW5hbCBQcmljZSBvZiAtLjgyMSUu
IFBsZWFzZSBzZWUgdGhlIGF0dGFjaGVkIExvY2sgQ29uZmlybWF0aW9uIGZvciBkZXRhaWxzLiBZ
b3UgbW

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

如果我将电子邮件转发相同的电子邮件地址,当我检查那个消息时,这行代码有效:

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

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

编辑 2


添加这行代码就可以了:

 $message1 = imap_base64($message);

【问题讨论】:

  • “gmail”是指“电子邮件”吗?更重要的是,你能告诉我们任何消息吗(edit它进入问题,不要试图在评论中添加它)?如果没有至少有代表性的内容摘录,我们所能做的就是猜测问题所在。

标签: php


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2013-02-06
    • 2015-11-20
    • 2011-05-30
    相关资源
    最近更新 更多