【问题标题】:Sending email with sendgrid [Node.js]使用 sendgrid [Node.js] 发送电子邮件
【发布时间】:2015-01-11 02:29:33
【问题描述】:

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

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();
        }
});

我可以改进吗?有很多重复。

【问题讨论】:

  • 一个包装器是我能想到的所有东西,它可以管理承诺的返回等等,比如 send_message(params);
  • Sendgrid API V3 希望对您有所帮助stackoverflow.com/a/50479562/2392211

标签: javascript node.js sendgrid


【解决方案1】:

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

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);
});

希望有帮助!

【讨论】:

    【解决方案2】:

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

    ..当然还要安装@sendgrid/mail 包。

    npm i @sendgrid/mail --save

    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.");
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      相关资源
      最近更新 更多