在 reactjs 中使用 gmail api 发送带有纯文本、附件和 html 的电子邮件
Sending email with plain text, attachments and html using gmail api in reactjs
我目前正在尝试发送由 3 个部分组成的电子邮件:纯文本、附件和 html,在 reactjs 中使用 gmail api。现在使用我的代码发送电子邮件,我只能收到附件和 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 UI 并查看原始邮件,然后按照邮件右上角的 ⋮ > Show original
,您会看到您的纯文本是在消息中应该是这样。
因为您也有 HTML,可以显示 HTML 的电子邮件客户端将显示此 而不是 纯文本,而不是与它一起显示。
您的代码运行良好,如果您想查看纯文本,您需要使用非 HTML 显示客户端,或者从消息中删除 HTML。
我目前正在尝试发送由 3 个部分组成的电子邮件:纯文本、附件和 html,在 reactjs 中使用 gmail api。现在使用我的代码发送电子邮件,我只能收到附件和 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 UI 并查看原始邮件,然后按照邮件右上角的 ⋮ > Show original
,您会看到您的纯文本是在消息中应该是这样。
因为您也有 HTML,可以显示 HTML 的电子邮件客户端将显示此 而不是 纯文本,而不是与它一起显示。
您的代码运行良好,如果您想查看纯文本,您需要使用非 HTML 显示客户端,或者从消息中删除 HTML。