簡體   English   中英

使用 sendgrid [Node.js] 發送電子郵件

[英]Sending email with sendgrid [Node.js]

我正在使用以下代碼發送兩封特定的電子郵件,一封給預訂客戶,另一封給我:

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

我可以改進嗎? 有一些重復。

您可以將自己指定為收件人,您將獲得客戶收到的確切電子郵件。 如果您使用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);
});

希望有幫助!

只需替換您從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.");
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM