Sendgrid发送没有嵌入代码的html电子邮件

时间:2016-08-02 13:17:32

标签: html node.js api email sendgrid

我只想直接输入./blabla.html发送我的html文件,而不是创建广告系列或模板。有没有办法在不放嵌入代码的情况下发送邮件?如果是这样,我会很高兴,谢谢! 我目前的代码如下:



var helper = require('sendgrid').mail
  from_email = new helper.Email("blabla@hotmail.com")
  to_email = new helper.Email("heyhey@gmail.com")
  subject = "Merhaba !"
  content = new helper.Content("text/plain", "selam")
  mail = new helper.Mail(from_email, subject, to_email, content)
}

var sg = require('sendgrid').SendGrid("mysecretapikey")
  var requestBody = mail.toJSON()
  var request = sg.emptyRequest()
  request.method = 'POST'
  request.path = '/v3/mail/send'
  request.body = requestBody
  sg.API(request, function (response) {
    console.log(response.statusCode)
    console.log(response.body)
    console.log(response.headers)
  })




1 个答案:

答案 0 :(得分:3)

您可能需要更新sendgrid包。基于您的要求的工作示例如下所示:

var fs = require('fs');
var path = require('path');

var filePath = path.join(__dirname, 'myfile.html');

fs.readFile(filePath, {encoding: 'utf-8'}, function(err, data) {
    if ( ! err ) {
      var helper = require('sendgrid').mail;
      from_email = new helper.Email("blabla@hotmail.com");
      to_email = new helper.Email("heyhey@gmail.com");
      subject = "Merhaba !";
      content = new helper.Content("text/html", data);
      mail = new helper.Mail(from_email, subject, to_email, content);

      var sg = require('sendgrid')('your api key');
      var requestBody = mail.toJSON();
      var request = sg.emptyRequest();
      request.method = 'POST';
      request.path = '/v3/mail/send';
      request.body = requestBody;
      sg.API(request, function (error, response) {
        if ( ! error ) {
          console.log(response.statusCode);
          console.log(response.body);
          console.log(response.headers);
        } else {
          console.log(error);
        }
      });
    } else {
        console.log(err);
    }
});

myfile.html文件就在这个.js文件的旁边,看起来像这样:

<html>
<head>
    <title> Test </title>
</head>
<body>
    <h2> Hi! </h2>
    <p> This is a test email </p>
</body>
</html>