【问题标题】:Formatting an email in JavaScript and SendGrid用 JavaScript 和 SendGrid 格式化电子邮件
【发布时间】:2018-03-15 04:30:26
【问题描述】:

我正在尝试将格式化的字符串传递给我的函数,但是当发送电子邮件时,文本没有逐行显示,这正是我想要的。

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
                });

我正在使用 SendGrid 和 Parse Cloud Code 在我的网络应用程序中生成电子邮件:

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!');
    }
  });

});

最终结果电子邮件应如下所示:

nominatorName
nominationRegion

目前,它看起来像这样:

nominatorName nominatorRegion

【问题讨论】:

    标签: javascript parse-platform sendgrid parse-server


    【解决方案1】:

    可能有两种方法可以完成。

    使用 HTML 正文

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

    使用换行符转义符

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

    【讨论】:

    • 你可以用第一个,很简单
    • 是的,但我还必须在云代码功能中将type: 'text/plain',更改为type: 'text/html',。
    【解决方案2】:

    您需要将换行符\n 替换为HTML 换行符&lt;br&gt;。

    例如:

    var bodySummary = nominatorName + '<br>' + nominatorRegion;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      相关资源
      最近更新 更多