我目前正在尝试使用 reactjs 中的 gmail api 发送由 3 个组件组成的电子邮件:纯文本、附件和 html。现在使用我的代码发送电子邮件,我只能接收附件和 html。下面是我用来发送电子邮件的代码:
for (i=0; i<email.attachment.length; i++) {
let content = email.attachment[i].data.split(",")[1];
let attachment = ['--012boundary01\r\n',
'Content-Type: ' + email.attachment[i].type + '\r\n',
'MIME-Version: 1.0\r\n',
'Content-Transfer-Encoding: base64\r\n',
'Content-Disposition: attachment; filename=' + email.attachment[i].name + '\r\n\r\n',
content, '\r\n\r\n'].join('');
console.log(attachment)
attachments += attachment
};
let mail = [
'Content-Type: multipart/mixed; boundary="012boundary01"\r\n',
'MIME-Version: 1.0\r\n',
'To: ' + email.to + '\r\n',
'Subject: ' + email.subject + '\r\n\r\n',
'--012boundary01\r\n',
'Content-Type: multipart/alternative; boundary=012boundary02\r\n\r\n',
'--012boundary02\r\n',
'Content-Type: text/plain; charset=UTF-8\r\n\r\n',
email.body + '\r\n\r\n',
'--012boundary02\r\n',
'Content-Type: text/html; charset=UTF-8\r\n',
'Content-Transfer-Encoding: quoted-printable\r\n\r\n',
'<h1>HELLO</h1>\r\n\r\n',
'--012boundary02--\r\n',
attachments,
'--012boundary01--'
].join('');
content 变量是我的附件的 base64 编码字符串。
最终的邮件变量将如下所示:
Content-Type: multipart/mixed; boundary="012boundary01"
MIME-Version: 1.0
To: [email protected]
Subject: test again
--012boundary01
Content-Type: multipart/alternative; boundary=012boundary02
--012boundary02
Content-Type: text/plain; charset=UTF-8
testing123
--012boundary02
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<h1>HELLO</h1>
--012boundary02--
--012boundary01
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=1x1_red_pixel.jpeg
(base64 encoded string here)
你的纯文本 被 发送,但邮件应用程序能够显示HTML,所以你看不到它。
电子邮件的纯文本部分显示在无法显示更复杂内容(在您的情况下为 HTML)的电子邮件客户端中。
如果您转到 Gmail 用户界面并查看原始邮件,然后按照邮件⋮ > Show original
右上角的 ,您将看到您的纯文本在邮件中应有的样子。
由于您也有 HTML,因此可以显示 HTML 的电子邮件客户端将显示它而不是纯文本,而不是在它旁边显示。
您的代码运行良好,如果您想查看纯文本,您需要使用非 HTML 显示客户端,或从消息中删除 HTML。
我希望这对你有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句