问题描述
我在解码使用Gmail API收到的电子邮件的邮件正文时遇到严重问题。我想抓取消息内容并将内容放在div中。我正在使用base64解码器,我知道它不会解码编码不同的电子邮件,但我不知道如何检查电子邮件以决定使用哪个解码器 - 说明它们是utf-8编码的电子邮件已成功解码base64解码器,但不是一个utf-8解码器。
I am having serious problems decoding the message body of the emails I get using the Gmail API. I want to grab the message content and put the content in a div. I am using a base64 decoder, which I know won't decode emails encoded differently, but I am not sure how to check an email to decide which decoder to use -- emails that say they are utf-8 encoded are successfully decoded by the base64 decoder, but not be a utf-8 decoder.
我已经研究了几天的电子邮件解码,而且我已经知道我有点出局了我的联赛在这里。我之前没有做过很多关于电子邮件编码的工作。以下是我用于获取电子邮件的代码:
I've been researching email decoding for several days now, and I've learned that I am a little out of my league here. I haven't done much work with coding around email before. Here is the code I am using to get the emails:
gapi.client.load('gmail', 'v1', function() {
var request = gapi.client.gmail.users.messages.list({
labelIds: ['INBOX']
});
request.execute(function(resp) {
document.getElementById('email-announcement').innerHTML = '<i>Hello! I am reading your <b>inbox</b> emails.</i><br><br>------<br>';
var content = document.getElementById("message-list");
if (resp.messages == null) {
content.innerHTML = "<b>Your inbox is empty.</b>";
} else {
var encodings = 0;
content.innerHTML = "";
angular.forEach(resp.messages, function(message) {
var email = gapi.client.gmail.users.messages.get({
'id': message.id
});
email.execute(function(stuff) {
if (stuff.payload == null) {
console.log("Payload null: " + message.id);
}
var header = "";
var sender = "";
angular.forEach(stuff.payload.headers, function(item) {
if (item.name == "Subject") {
header = item.value;
}
if (item.name == "From") {
sender = item.value;
}
})
try {
var contents = "";
if (stuff.payload.parts == null) {
contents = base64.decode(stuff.payload.body.data);
} else {
contents = base64.decode(stuff.payload.parts[0].body.data);
}
content.innerHTML += '<b>Subject: ' + header + '</b><br>';
content.innerHTML += '<b>From: ' + sender + '</b><br>';
content.innerHTML += contents + "<br><br>";
} catch (err) {
console.log("Encoding error: " + encodings++);
}
})
})
}
});
});
我正在执行一些检查和调试,所以剩下的 console.log
和其他一些仅用于测试的东西。不过,你可以在这里看到我想要做的事情。
I was performing some checks and debugging, so there's leftover console.log
's and some other things that are only there for testing. Still, you can see here what I am trying to do.
解码我从Gmail API中提取的电子邮件的最佳方法是什么?我应该尝试将电子邮件放入< script>
,并使用 charset
和类型
匹配电子邮件编码内容的属性?我相信我记得charset只适用于 src
属性,我在这里没有。有什么建议吗?
What is the best way to decode the emails I pull from the Gmail API? Should I try to put the emails into <script>
's with charset
and type
attributes matching the encoding content of the email? I believe I remember charset only works with a src
attribute, which I wouldn't have here. Any suggestions?
推荐答案
对于我正在编写的原型应用程序,以下代码对我有用:
For a prototype app I'm writing, the following code is working for me:
var base64 = require('js-base64').Base64;
// js-base64 is working fine for me.
var bodyData = message.payload.body.data;
// Simplified code: you'd need to check for multipart.
base64.decode(bodyData.replace(/-/g, '+').replace(/_/g, '/'));
// If you're going to use a different library other than js-base64,
// you may need to replace some characters before passing it to the decoder.
警告:这些点没有明确记录,可能是错误的:
Caution: these points are not explicitly documented and could be wrong:
-
默认返回已解析的正文内容。无论
Content-Type
和Content-Transfer-Encoding $ c $,此数据似乎始终以UTF-8和Base64编码。 c> header。
The
users.messages: get
API returns "parsed body content" by default. This data seems to be always encoded in UTF-8 and Base64, regardless of theContent-Type
andContent-Transfer-Encoding
header.
例如,我的代码在解析包含这些标题的电子邮件时没有问题: Content-Type:text / plain; charset = ISO-2022-JP
, Content-Transfer-Encoding:7bit
。
For example, my code had no problem parsing an email with these headers: Content-Type: text/plain; charset=ISO-2022-JP
, Content-Transfer-Encoding: 7bit
.
Base64编码的映射表。 Gmail API使用 -
和 _
作为表格的最后两个字符,由的URL和文件名安全字母。
The mapping table of the Base64 encoding varies among various implementations. Gmail API uses -
and _
as the last two characters of the table, as defined by RFC 4648's "URL and Filename safe Alphabet".
检查您的Base64库是否使用不同的映射表。如果是这样,请在将正文传递到解码器之前将这些字符替换为您的库所接受的字符。
Check if your Base64 library is using a different mapping table. If so, replace those characters with the ones your library accepts before passing the body to the decoder.
文档中有一条支持线:返回body content as base64url encoded string。 (谢谢埃里克!)
There is one supportive line in the documentation: the "raw" format returns "body content as a base64url encoded string". (Thanks Eric!)
这篇关于Gmail API解码Javascript中的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!