我正在一个临时电子邮件的网站上工作,我必须实现的功能之一就是获取电子邮件的来源(获取电子邮件的标题),所以我实现了这个代码来实现这一点:
后端(PHP):
function get_original_email($mailid, $address) {
global $mailbox;
if (_load_one_email($mailid, $address) !== null) {
header('Content-type: application/json');
$headers = imap_fetchheader($mailbox->getImapStream(), $mailid, FT_UID);
//$body = imap_body($mailbox->getImapStream(), $mailid, FT_UID);
print (json_encode(array('originalEmail' => $headers)));
} else {
error(404, 'retrieving error: invalid username/mailid combination');
}
}
angularjs控制器:
self.getOriginal = function (mail, domain) {
var firstTo = Object.keys(mail.to)[0];
$http.get(backend_url, {params:{username: firstTo, original_email_id: mail.id, domain:domain}})
.then(
function successCallback(response) {
self.originalEmail = response.data.originalEmail;
$log.error(response); //To show response content.
},
function errorCallback(response) {
$log.error(response, this);
self.error = {
title: "HTTP_ERROR",
desc: "There is a problem with geting the original email. (HTTP_ERROR).",
detail: response
};
});
}
此代码在大多数电子邮件中运行良好,但是当我尝试注册Blockchain.com时,原始版本失败了,我在Chrome检查器中得到了这个:
{data: "", status: 200, config: {…}, statusText: "OK", headers: ƒ}config: {method: "GET", transformRequest: Array(1), transformResponse: Array(1), jsonpCallbackParam: "callback", paramSerializer: ƒ, …}data: ""headers: ƒ (d)status: 200statusText: "OK"__proto__: Object
我下载了标题的内容:
Return-path: <bounces+1772275-ae77-jeff=mailinbox.co@sendgrid.net>
Envelope-to: jeff@mailinbox.co
Delivery-date: Mon, 25 Sep 2017 18:25:32 +0200
Received: from o1.mail.blockchain.info ([198.21.6.174])
by with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128)
(Exim 4.89)
(envelope-from <bounces+1772275-ae77-jeff=mailinbox.co@sendgrid.net>)
id 1dwWCK-0008U5-Eu
for jeff@mailinbox.co; Mon, 25 Sep 2017 18:25:32 +0200
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed;
d=blockchain.info;
h=content-transfer-encoding:content-type:from:mime-version:subject:to;
s=m1; bh=A+wGBBGT0nPZi611WDiY8hyoJiA=; b=BvDSkuUQpbkbwvS176xTK19
evW+h0n9QLlMlqNQRy8pEZXRaMtPm3o9szysUlrzFB8+ucaSyCQqhdjobKRA5LvP
L5BS9814iqFpV+Kzrb8gMc3aSl/aycBTqBxQYeuTtsWWT1I6VMHLPquVqOuS4ZjG
n1ia1O5mFsFtvNJpligI=
Received: by filter0934p1mdw1.sendgrid.net with SMTP id filter0934p1mdw1-27911-59C92DF5-1D
2017-09-25 16:25:25.327461844 +0000 UTC
Received: from MTc3MjI3NQ (236.212.190.35.bc.googleusercontent.com [35.190.212.236])
by ismtpd0001p1lon1.sendgrid.net (SG) with HTTP id 8U6IPbOfSEO07nzJUAkd1Q
for <jeff@mailinbox.co>; Mon, 25 Sep 2017 16:25:25.209 +0000 (UTC)
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8
Date: Mon, 25 Sep 2017 16:25:25 +0000 (UTC)
From: "Blockchain" <no-reply@blockchain.info>
Mime-Version: 1.0
Subject: Welcome To My Wallet
To: jeff@mailinbox.co
Message-ID: <8U6IPbOfSEO07nzJUAkd1Q@ismtpd0001p1lon1.sendgrid.net>
X-SG-EID: rp6B7bPLkmILpCROEdQ6zxTTWV8/jMJblylXDv403K5XiCqxnco7VnA/3IqSz7BIqYEji4cv4ijjl3
obxT0otJleksoYG645Y4eGShknp4SWqOkaFs63Ycjvax2oooiiWDaX5/JGhenxjRDma8xla9eBS4Fg
kwS3NpJQZSN3iSig19KAiQSGMIT4FhORo8kD0hDQAJhfzUT/JT1pcnAx8mMsZ9/JyELfQe9pet+yLB
8=
X-Spam-Score: 10
X-Spam-Bar: +
X-Spam-Report: Spam detection software, running on the system "", has
identified this incoming email as possible spam. The original message
has been attached to this so you can view it (if it isn't spam) or label
similar future email. If you have any questions, see
the administrator of that system for details.
Content preview: Verify Your Email Thank you for creating a Blockchain wallet.
Verify your email below to complete your setup. Yes, This Is My Email Your
Wallet ID: 8a622806-96bd-44f3-a267-77f5af68a3dd Use your unique Wallet ID
to log into your Blockchain wallet. Copyright © 2017 Blockchain Luxembourg
S.A. All rights reserved. Click here to Unsubscribe [...]
Content analysis details: (1.0 points, 5.0 required)
pts rule name description
---- ---------------------- --------------------------------------------------
0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked.
See
http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block
for more information.
[URIs: blockchain.info]
0.0 HTML_MESSAGE BODY: HTML included in message
1.1 MIME_HTML_ONLY BODY: Message only has text/html MIME parts
-0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's
domain
0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid
-0.1 DKIM_VALID Message has at least one valid DKIM or DK signature
X-redirected: yes
因为我在下载函数中获取了直接使用print而不使用json_encode(发送json格式)的内容,所以我认为问题是json格式,但我不知道是什么问题确切,所以如果你有任何想法感谢分享。
答案 0 :(得分:0)
问题在于消息内容中可能出现的一些特殊字符($headers
),所以我必须清理它们,因为我创建了这个函数:
function _clean_origin($origin){
$origin = preg_replace('/[^\da-z \n\rA-Z.,!<>()+-?]/'," ",$origin);
return $origin;
}
我在get_original_email
中调用以清除$headers
,因此我可以正常获取标题。