2016-08-02 135 views
1

我只是想直接发送我的html文件,输入./blabla.html并且不创建广告系列或模板。有没有一种方法可以发送邮件而不需要添加嵌入的代码?如果是这样,我会非常高兴,谢谢! 我当前的代码如下所示:Sendgrid发送无需嵌入代码的HTML电子邮件

var helper = require('sendgrid').mail 
 
    from_email = new helper.Email("[email protected]") 
 
    to_email = new helper.Email("[email protected]") 
 
    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) 
 
    })

+0

为什么不能阅读HTML文件转换成字符串像这样:http://stackoverflow.com/questions/18386361/read-a-file-in-node-js?另外请注意,如果你想以HTML的形式发送,你会希望为'content'设置'text/html'。 –

+0

没关系,但我无法找到放置该读取操作的位置。当我调用读取函数时,它只是读取cmd上的内容并将邮件发送给写入函数名称的接收方。任何帮助? @ Sebastian-LaurenţiuPlesciuc – MeganLondon

回答

2

您可能需要更新您的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("[email protected]"); 
     to_email = new helper.Email("[email protected]"); 
     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> 
+0

谢谢!这个真的很感谢你! – MeganLondon

相关问题