使用sendgrid [Node.js]发送电子邮件

时间:2015-01-11 02:29:34

标签: javascript node.js sendgrid

我正在使用以下代码发送两封特定的电子邮件,一封用于预订客户端,另一封用于我:

sendgrid.send({
        to:         "client@client.com",
        from:       "me@me.com",
        subject:    "Register confirmation",
        html:           "Some HTML",
    },
    function(err, json) {
        if (err) {
            return console.error(err);
        } 
        else {
            next();
        }
});

sendgrid.send({
        to:         "me@me.com",
        from:       "newRegister@me.com",
        subject:    "NEW RESERVATION!",
        html:       "SOME TEXT OR HTML",
    },
    function(err, json) {
        if (err) {
            return console.error(err);
        } 
        else {
            next();
        }
});

我能改进吗?有一些重复。

2 个答案:

答案 0 :(得分:1)

您可以将自己指定为收件人,然后您将收到客户正在接收的确切电子邮件。如果您使用SMTPAPI,则用户无法看到您的电子邮件地址,也不会看到他们的电子邮件地址。要在Node.js lib中使用它,您可以这样做:

var sendgrid = require('sendgrid')('api_user', 'api_pwd');
var email = sendgrid.Email();
email.smtpapi.addTo('client@email.com');
email.smtpapi.addTo('your@notification.email');
email.setSubject('Register confirmation');
email.setHtml('booking numbah');
email.setFrom('your@email.com');
sendgrid.send(email, function(err, json) {
   console.log(arguments);
});

希望有所帮助!

答案 1 :(得分:1)

只需替换从https://app.sendgrid.com/settings/api_keys获得的 API密钥。 (创建API密钥)

..并安装@ sendgrid / mail软件包。

npm我@ sendgrid / mail-保存

const sgMail = require("@sendgrid/mail");

const SENGRID_API_KEY = 'Here_Paste_Your_Key'; 
sgMail.setApiKey(SENGRID_API_KEY);

const msg = {
    to: 'to@email.com',
    from: 'from@email.com',
    subject: "Test Subject",
    text: 'Hello World!',
    html: '<strong>Hello World!</strong>',
};

sgMail.send(msg, function(err, info) {
    if (err) {
        console.log("Email Not Sent.");
    } else {
        console.log("Email Sent Success.");
    }
});