这是我的php代码
<?php
error_reporting(0);
// Multiple email account
$emails = array(
array(
'no' => '5',
'label' => 'inbox 1',
'host' => '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX',
'username' => 'xxx@gmail.com',
'password' => 'xxx'
),
array(
'no' => '2',
'label' => 'Inbox Email 2',
'host' => '{mail.domain.net:143/notls}INBOX',
'username' => 'mail2@domain.net',
'password' => 'xxxxxxxxxx'
)
);
foreach ($emails as $email) {
$read = imap_open($email['host'], $email['username'], $email['password']) or die('Cannot connect to yourdomain.com: ' . imap_last_error() . '</div>');
$array = imap_search($read, 'SUBJECT "123"');
if ($array) {
$html = '';
rsort($array);
foreach ($array as $result) {
$overview = imap_fetch_overview($read, $result, 0);
$message = imap_body($read, $result, 0);
$reply = imap_headerinfo($read, $result, 0);
//Now I want a json output like bellow structure
/*
"email no":"1",//from emails array
"email lebel":"label",//from emails arrey
"subject" :"mail_subject",
"date" :"full date time",
"message" :"messege",
"sent_from":"sender email"
*/
}
}
imap_close($read);
}
?>
我希望从获取的电子邮件中输出json
,如下所示:
"email no":"email no",
"email_label":"label",
"subject" :"mail_subject",
"date" :"full date time",
"message" :"messege",
"sent_from":"sender email"
如果有人编写示例代码对我非常有帮助,我不知道该怎么做
我使用以下代码测试了它是否可以正常打印:
echo $overview[0]->from;
echo $overview[0]->date;
echo $overview[0]->subject;
echo $reply->from[0]->mailbox.'@'.$reply->from[0]->host;
echo $message;
这是输出:
sahidul islam Sun, 23 Jun 2019 12:54:32 +0800123sahid4745@gmail.com--0000000000008222fd058bf67e3a Content-Type: text/plain; charset="UTF-8" Thi is test message --0000000000008222fd058bf67e3a Content-Type: text/html; charset="UTF-8"
Thi is test message
输出为--0000000000008222fd058bf67e3a Content-Type: text/plain; charset="UTF-8" Thi is test message --0000000000008222fd058bf67e3a Content-Type: text/html; charset="UTF-8"
Thi is test message
时,消息json_encode
的这一部分可能会冲突,如何从Thi is test message
中提取$message
?
答案 0 :(得分:0)
我的猜测是,在这里我们可以组装一个我们想要输出的数组,然后是json_encode
:
$json_output = []; // output array to be JSONed
foreach ($emails as $email) {
$read = imap_open($email['host'], $email['username'], $email['password']) or die('Cannot connect to yourdomain.com: ' . imap_last_error() . '</div>');
$array = imap_search($read, 'SUBJECT "123"');
if ($array) {
$html = '';
rsort($array);
foreach ($array as $result) {
$overview = imap_fetch_overview($read, $result, 0);
$message = imap_body($read, $result, 0);
$reply = imap_headerinfo($read, $result, 0);
// data from email array
$json_output["email no"] = $email["no"];
$json_output["email label"] = $email["label"];
// data from other variables
$json_output["subject"] = $result["mail_subject"];
$json_output["date"] = $result["full date time"];
$json_output["message"] = $result["messege"];
$json_output["sent_from"] = $result["sender email"];
$json_output = json_encode($json_output, true);
}
}
imap_close($read);
}
我不确定这些var的数据来自哪里,我确定您会知道:
$json_output["subject"] = $result["mail_subject"];
$json_output["date"] = $result["full date time"];
$json_output["message"] = $result["messege"];
$json_output["sent_from"] = $result["sender email"];
应该有更好的方法来提取消息,其中之一是:
$re = '/charset="UTF-8"\s*(.+?)\s+-/s';
$str = '--0000000000008222fd058bf67e3a Content-Type: text/plain; charset="UTF-8" Thi is test message --0000000000008222fd058bf67e3a Content-Type: text/html; charset="UTF-8"
Thi is test message';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);