javascript - 在 reactjs 中使用 gmail api 发送带有纯文本、附件和 html 的电子邮件
问题描述
我目前正在尝试使用 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('');
内容变量是我附件的 base64 编码字符串。
最终的邮件变量将如下所示:
Content-Type: multipart/mixed; boundary="012boundary01"
MIME-Version: 1.0
To: angyangcheng99@gmail.com
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。
推荐阅读
- javascript - vue 将图片转换为base64
- google-cloud-platform - 错误无效的实例 URL:资源“google_compute_instance_group”“t-compute-instance-group”
- python - lxml 并请求抓取 javascript 表
- powershell - 如何处理 powershell 中属性名称中的破折号
- python - Discord.py 机器人不会显示带有前缀的文本
- aws-sdk - AWS Cognito,创建用户时未显示必填字段
- c# - C# Linq 将表达式转换为相似类的相同属性
- linux - Delphi Rem Objects Pascal 在 Linux 64 中编译
- javascript - 导航离开然后返回时在单页网站上加载 javascript 的问题
- c# - 如何在 Razor/.NET Core PageModel 中绑定动态长度列表<>,而不依赖 JavaScript 重新索引每个输入?