I am working on using sendgrid to send automated emails through my app. When I built out a custom dynamic template to handle various cases for automated emails, line breaks stopped working on the email body, or 'text', I am sending through to the API. Here is my process flow:
- When a user signs up for the app, the my /signup api route determines the type of email to send the user:
const msg = await getEmailType('signup', user)
- The awaited getEmailType function returns the following JSON object depending on the case - in my example, the case is 'signup':
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'
}
}
The line breaks in 'text', which is supposed to be my email body, are not being applied properly within the actual email template.
- The JSON object is sent through sendgrid as follows and then formatted as an email to send to the user (I've also included some dependencies at the top here for some context in case anyone has used sendgrid):
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)
}
}
The template I've created looks nice and works, but for some reason, since the actual email sent to the user is returning HTML generated by the WYSIWYG editor, the line breaks do not work as a result of a deprecated setting in sendgrid's api.
There also doesn't seem to be any updated resources for this issue, so I'm wondering if anyone else has encountered this before and can help.
')" – Andrew Sep 22 '21 at 17:01