我正在尝试通过Gmail API发送带附件的邮件。
我按照以下规定成功发送邮件:
function sendmail(to, subject, message) {
var email =
"To: " + to + "\r\n" +
"Subject: " + subject + "\r\n" +
'Content-Transfer-Encoding: 8bit\r\n' +
'Content-Type: text/html; charset="UTF-8"\r\n' +
"" +
message + "\r\n\r\n";
var sendRequest = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': window.btoa(unescape(encodeURIComponent(email))).replace(/\+/g, '-').replace(/\//g, '_')
}
});
sendRequest.execute();
}
现在我想发送一些文件作为附件并按以下方式尝试:
<input id="inputFile" type="file" onChange="convertToBase64();"/>
<script>
var file_ = '';
function convertToBase64() {
//Read File
var file = document.getElementById("inputFile").files[0];
var reader = new FileReader();
reader.onload = function () {
file_ = reader.result.split('base64,')[1];
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
reader.readAsDataURL(file);
}
function sendmail(to, subject, message, date) {
console.log(date);
if (typeof file_ !== "undefined" && file_ != '') {
var email =
"To: " + to + "\r\n" +
"Subject: " + subject + "\r\n" +
'Content-Transfer-Encoding: 8bit\r\n' +
'Content-Type: text/html; charset="UTF-8"\r\n' +
"" +
message +
"\r\n\r\n" +
"--foo_bar_baz\r\n" +
"Content-Type: application/pdf\r\n" +
"MIME-Version: 1.0\r\n" +
"Content-Disposition: attachment; filename=example.pdf\r\n\r\n" +
file_ + "\r\n\r\n";
var sendRequest = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': window.btoa(unescape(encodeURIComponent(email))).replace(/\+/g, '-').replace(/\//g, '_')
}
});
sendRequest.execute();
} else {
alert('boo');
}
}
</script>
我会收到邮件,但我看不到文字,因为“编码错误!”无法打开文件。
我确实检查了整个gmail文档,但没有用。
有人可以帮帮我吗?
答案 0 :(得分:0)
您是否尝试过:Content-Transfer-Encoding:base64
我在某处读到将pdf编码为base64要求您相应地设置内容传输编码。