2016-08-02 10 views
1

私はちょうど./blabla.htmlと入力して直接HTMLファイルを送信し、キャンペーンやテンプレートは作成しません。埋め込みコードを入れずにメールを送信する方法はありますか?もしそうなら、私はとても幸せになるでしょう、ありがとう!あなたのsendgridパッケージを更新する必要があるかもしれません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

をこのように:http://stackoverflow.com/questions/18386361/read-a-file-in-node-js?また、HTMLとして送信する場合は、 'content'に' text/html'が必要です。 –

+0

それは大丈夫ですが、私はその読み込み操作をどこに置くのか見つけることができません。読んだfuncを呼び出すと、cmdの内容が読み込まれ、受信者に関数名を記述したメールが送信されます。どんな助け? @ Sebastian-LaurenţiuPlesciuc – MeganLondon

答えて

2

: 私の現在のコードは次のようになります。あなたの要件に基づいて実施例はこのようなものになります。

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ファイルを読んでいないのはなぜ

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

ありがとうございました!これは本当にあなたに何千ものおかげで働いた! – MeganLondon

関連する問題