Search code examples
javascriptparse-platformsendgridparse-server

Formatting an email in JavaScript and SendGrid


I'm trying to pass a formatted string into my function, but when the email is sent the text does not appear line by line, which is what I want.

var bodySummary = nominatorName + "\n" + nominatorRegion

                // Run email Cloud code
                Parse.Cloud.run("sendEmail", {
                    toEmail: "test@gmail.com",
                    subject: "Email Test",
                    body: bodySummary
                }).then(function(result) {
                    // make sure to set the email sent flag on the object
                    console.log("result :" + JSON.stringify(result));
                }, function(error) {
                    // error
                });

I'm using SendGrid and Parse Cloud Code to generate emails in my web app:

Parse.Cloud.define("sendEmail", function(request, response) {

  // Import SendGrid module and call with your SendGrid API Key
  var sg = require('sendgrid')('API_KEY');

  // Create the SendGrid Request
  var reqSG = sg.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: {
      personalizations: [
        {
          to: [
            {
              // This field is the "to" in the email
              email: request.params.toEmail,
            },
          ],
          // This field is the "subject" in the email
          subject: request.params.subject,
        },
      ],
      // This field contains the "from" information
      from: {
        email: 'no-reply@test.com',
        name: 'Test',
      },
      // This contains info about the "reply-to"
      // Note that the "reply-to" may be different than the "from"
      reply_to: {
        email: 'no-reply@test.com',
        name: 'Test',
      },
      content: [
        {
          // You may want to leave this in text/plain,
          // Although some email providers may accept text/html
          type: 'text/plain',
          // This field is the body of the email
          value: request.params.body,
        },
      ],
    },
  });

  // Make a SendGrid API Call
  sg.API(reqSG, function(SGerror, SGresponse) {
    // Testing if some error occurred
    if (SGerror) {
      // Ops, something went wrong
      console.error('Error response received');
      console.error('StatusCode=' + SGresponse.statusCode);
      console.error(JSON.stringify(SGresponse.body));
      response.error('Error = ' + JSON.stringify(SGresponse.body));
    } 
    else {
      // Everything went fine
      console.log('Email sent!');
      response.success('Email sent!');
    }
  });

});

The end result email should look like this:

nominatorName
nominationRegion

Currently, it looks like this:

nominatorName nominatorRegion

Solution

  • There could be two ways to accomplish.

    Using HTML Body

      var bodySummary = nominatorName + "<br>" + nominatorRegion
    

    Using newline escape characters

       var bodySummary = nominatorName + "\r\n" + nominatorRegion