Javascript 使用sendgrid的模板电子邮件不支持换行符(\r\n)
我正在使用sendgrid通过我的应用程序发送自动电子邮件。当我构建了一个定制的动态模板来处理自动电子邮件的各种情况时,换行符在电子邮件正文或“文本”上停止工作,我正在通过API发送。以下是我的流程:Javascript 使用sendgrid的模板电子邮件不支持换行符(\r\n),javascript,node.js,sendgrid,line-breaks,Javascript,Node.js,Sendgrid,Line Breaks,我正在使用sendgrid通过我的应用程序发送自动电子邮件。当我构建了一个定制的动态模板来处理自动电子邮件的各种情况时,换行符在电子邮件正文或“文本”上停止工作,我正在通过API发送。以下是我的流程: 当用户注册应用程序时,my/signup api路由将确定发送给用户的电子邮件类型: 等待的getEmailType函数根据情况返回以下JSON对象——在我的示例中,情况是“注册”: “文本”中的换行符应该是我的电子邮件正文,但在实际电子邮件模板中没有正确应用 JSON对象通过sendgrid发送
const msg = await getEmailType('signup', user)
const getEmailType = async (emailType, user, token, req) => {
switch (emailType) {
case 'signup':
return {
to: user.email,
from: 'no-reply@app-address.com',
templateId: templates.default,
dynamic_template_data: {
subject: 'Welcome to our app!',
name: user.firstName,
text:
' Here is the email body with some line breaks. Confirm your account at the link
below:\r\n' +
' http://app-address.herokuapp.com/\r\n'
}
}
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const sendEmail = async msg => {
try {
const response = await sgMail.send(msg)
console.log('Response', response)
return response
} catch (error) {
console.error(error)
}
}