我在 RFC2822 邮件的“发件人”字段中设置邮件作者时遇到问题(此处为详细信息)。问题是作者没有出现在收到的邮件中。我设置的“来自”字段如下所示:
MyName: name.surname@gmail.com
但我也试过:
MyName <name.surname@gmail.com>
这些都不起作用,在收到的邮件中,查看原始邮件时仍然缺少名称。
这应该可以工作,因为使用 nodemailer(与 Gmail)和“来自”的相同值可以工作。有人可以解释一下发生了什么吗?我该如何解决?
编辑:我按照一条评论的要求报告了我正在使用的代码。
我将对 API 的调用与生成邮件正文的部分分开,因此调用是:
function gmailSend(auth, mail){
const gmail = google.gmail({version: 'v1', auth});
const b64mex=mail.b64enc();
return gmail.users.messages.send(
{auth: auth,
userId: 'me',
resource:
{raw: b64mex}
}
);
}
虽然以这种方式生成参数“mail”:
function genMail(candidate, clerk_email, supervisor_email){
return new Mail({from: `MyName: name.surname@gmail.com`, to: candidate.email,
subject: "Test Mail", bcc: supervisor_email,
"reply-to": clerk_email}, genMessage(candidate));
}
Mail 只是组成一个对象,该对象具有其第一个参数中给出的属性,而 b64enc() 将所有内容放入一个符合 RFC2822 的字符串中并将其编码为 base64。
EDIT2:邮件类的代码。
class Mail{
constructor(headers, body) {
if(!headers.to)
throw Error("Recipient missing");
if(headers.subject){
const b64subject=new Buffer(headers.subject).toString("base64")
.replace(/\+/g, '-').
replace(/\//g, '_');
headers.subject='=?utf-8?B?'+b64subject+"?=";
}
Object.assign(this, headers);
this.body = body;
}
b64enc(){
const fields = ["Content-Type: text/html; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n"];
for(const prop of Object.keys(this)){
if(prop!=="body")
fields.push(`${prop}: ${this[prop]}\n`);
}
fields.push("\n");
fields.push(this.body);
const str=fields.join('');
const encodedMail = new Buffer(str).toString("base64")
.replace(/\+/g, '-').
replace(/\//g, '_');
return encodedMail;
}
}
EDIT3:我添加了所需和实际行为的屏幕截图:
当然,我的电子邮件客户端中显示的内容是基于纯电子邮件中“发件人:”的内容。在第一种情况下它是“我的名字 <电子邮件地址>”,在第二种情况下它只是“电子邮件地址”。