簡體   English   中英

想要使用sendgrid通過雲功能發送電子郵件

[英]want to send an email through cloud functions using sendgrid

我正在使用雲功能向存儲在google firestore中的數據已更新的人發送電子郵件。 我正在使用sendgrid發送電子郵件。

每當我更新數據時,我的雲功能就可以正常工作。 但是我無法發送電子郵件。

const sendgridemail = require('@sendgrid/mail');
const MY_SENDGRID_API_KEY = '<API key>'
sendgridemail.setApiKey(MY_SENDGRID_API_KEY);
exports.helloFirestore = (event, callback) => {
const triggerResource = event.resource;
console.log('Function triggered by change to: ' +  triggerResource);
console.log(JSON.stringify(event));

 const msgbody = {
                    to: 'laaaf09@gmail.com',
                    from: 'abc@nnn.co',
                    subject:  'database updated - xyzshopping.com',
                    templateId: '<template ID>',
 }
return helloFirestore.send(msgbody)

            .then(() => console.log('payment mail sent success') )
            .catch(err => console.log(err) )
             callback();


};

我已經使用嵌入式編輯器和zip上傳從控制台部署了代碼。 它被觸發但沒有發送電子郵件,這引發了我錯誤:

Error: getaddrinfo ENOTFOUND api.sendgrid.com api.sendgrid.com:443 at errnoException (dns.js:28:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'api.sendgrid.com', host: 'api.sendgrid.com', port: 443

您應該在所有異步工作完成后才調用callback() 您是在工作開始之前就調用它的。 那肯定是行不通的。 文檔中

打回來

表示函數執行完成的回調。

您已提前通知函數結束。 您只應在工作完成后調用它,無論在then還是catch異步工作的回調中。

暫無
暫無

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

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