eug*_*832 8 javascript email decoding character-encoding gmail-api
我在解码使用Gmail API收到的电子邮件的邮件正文时遇到严重问题.我想抓取消息内容并将内容放在div中.我正在使用base64解码器,我知道它不会解码编码不同的电子邮件,但我不知道如何检查电子邮件以决定使用哪个解码器 - 说明它们是utf-8编码的电子邮件已成功解码base64解码器,但不是utf-8解码器.
我已经研究了几天的电子邮件解码,而且我已经了解到我在这里的联盟有点不合适.我之前没有做过很多关于电子邮件编码的工作.这是我用于获取电子邮件的代码:
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++);
}
})
})
}
});
});
Run Code Online (Sandbox Code Playgroud)
我正在执行一些检查和调试,所以有剩余console.log
的和其他一些只用于测试的东西.不过,你可以在这里看到我想要做的事情.
解码我从Gmail API中提取的电子邮件的最佳方法是什么?我应该尝试把电子邮件到<script>
的与charset
和type
属性相匹配的电子邮件的内容编码?我相信我记得charset只适用于src
属性,我不会在这里.有什么建议?
ent*_*nto 18
对于我正在编写的原型应用程序,以下代码对我有用:
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.
Run Code Online (Sandbox Code Playgroud)
注意:这些要点没有明确记录,可能是错误的:
该users.messages: get
API返回"解析主体内容"默认情况下.无论是Content-Type
和Content-Transfer-Encoding
标头,这些数据似乎总是以UTF-8和Base64编码.
例如,我的代码在解析带有这些标题的电子邮件时没有问题:Content-Type: text/plain; charset=ISO-2022-JP
, Content-Transfer-Encoding: 7bit
.
Base64编码的映射表在各种实现中有所不同.Gmail API使用-
和_
作为表格的最后两个字符,由RFC 4648的"URL和文件名安全字母" 1定义.
检查Base64库是否使用不同的映射表.如果是这样,请在将正文传递给解码器之前将这些字符替换为您的库所接受的字符.
1文档中有一条支持线:"raw"格式将"body content作为base64url编码的字符串"返回.(谢谢埃里克!)
使用 atob 解码 JavaScript 中的消息(请参阅ref)。为了访问消息有效负载,您可以编写一个函数:
var extractField = function(json, fieldName) {
return json.payload.headers.filter(function(header) {
return header.name === fieldName;
})[0].value;
};
var date = extractField(response, "Date");
var subject = extractField(response, "Subject");
Run Code Online (Sandbox Code Playgroud)
参考我之前的SO问题和
var part = message.parts.filter(function(part) {
return part.mimeType == 'text/html';
});
var html = atob(part.body.data);
Run Code Online (Sandbox Code Playgroud)
如果上面的内容不能 100% 正确解码,@cgenco 对此答案的评论可能适用于您。在这种情况下,做
var html = atob(part.body.data.replace(/-/g, '+').replace(/_/g, '/'));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6695 次 |
最近记录: |