2016-10-17 30 views
5

Tôi đang sử dụng sendgrid để gửi email. nhưng khi tôi cố gắng để tạo ra các đối tượng email như sausendgrid gửi lỗi sendgrid.Email không phải là một nhà xây dựng

let email = new sendgrid.Email(); 
email.addTo("[email protected]"); 
email.setFrom("[email protected]"); 
email.setSubject("New Unit Added"); 
email.setHtml("New unit addded </br> Unit Id =" + savedUnit._id); 
sendgrid.send(email, function(err, json) { 
    if (err) { 
     console.log("Error: " + err); 
    } else { 
     console.log(json); 
    } 
}); 

Nhưng nó đưa ra lỗi

enter image description here

https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/nodejs.html

Trả lời

0

này nên làm việc cho bạn

var helper = require('sendgrid').mail; 

from_email = new helper.Email("[email protected]"); 
to_email = new helper.Email("[email protected]"); 
subject = "Sending with SendGrid is Fun"; 
content = new helper.Content("text/plain", "and easy to do anywhere, even with Node.js"); 
mail = new helper.Mail(from_email, subject, to_email, content); 

var sg = require('sendgrid')(process.env.SENDGRID_API_KEY); 
var request = sg.emptyRequest({ 
    method: 'POST', 
    path: '/v3/mail/send', 
    body: mail.toJSON() 
}); 

sg.API(request, function(error, response) { 
    console.log(response.statusCode); 
    console.log(response.body); 
    console.log(response.headers); 
}) 

Nguồn: https://sendgrid.com/docs/Integrate/Code_Examples/v3_Mail/nodejs.html

1

Hãy thử điều này - nó làm việc cho tôi ...

Trước cài đặt 'sendgrid-web' sử dụng:

npm install sendgrid-web

Sau đó, thực hiện các mã như thế này:

router.get('/email2',function(req,res,next){ 
    var Sendgrid = require("sendgrid-web"); 
     var sendgrid = new Sendgrid({ 
     user: "Your_login_username_for_Sendgrid",//provide the login credentials 
     key:"Your_Api_Key_OR_password" 
     }); 

    sendgrid.send({ 
    to: '[email protected]', 
    from: '[email protected]', 
    subject: 'Hello world!', 
    html: '<h1>Hello world!</h1>' 
    }, function (err) { 
    if (err) { 
     console.log(err); 
     res.json({Error:'Error in sending mail'}); 
    } else { 
     console.log("Success."); 
     res.json({Success:'sucessful'}); 
    } 
    }); 
}) 
+0

rực rỡ, cũng làm việc cho tôi. Bạn có thể chỉnh sửa câu trả lời của mình để bao gồm một liên kết đến các tài liệu mà bạn tìm thấy nó không? bởi vì tôi không thể tìm thấy giải pháp này trong tài liệu – rakan316

Các vấn đề liên quan