2017-06-08 29 views
9

我試圖在/emails中寫入任何內容時向我的電子郵件發送測試電子郵件,但電子郵件永遠不會發送,功能日誌爲空。Firebase的雲端函數 - 發送電子郵件給我寫信

exports.sendTestEmail = functions.database.ref('/emails') 
    .onWrite(event => { 
    return sendTestEmail('[email protected]'); 
    }) 
// [END onWrite] 

// Sends a welcome email to the given user. 
function sendTestEmail(email) { 
    const mailOptions = { 
    from: `${APP_NAME} <[email protected]>`, 
    to: email 
    }; 
    // The user subscribed to the newsletter. 
    mailOptions.subject = `Welcome to ${APP_NAME}!`; 
    mailOptions.text = `Hey there! Welcome to ${APP_NAME}. I hope you will enjoy our service.`; 
    return mailTransport.sendMail(mailOptions).then(() => { 
    console.log('New welcome email sent to:', email); 
    }); 

} 

編輯***

上面的代碼工作。我試圖在emails上觸發該功能,而正確的名稱是email

+0

您是否使用Gmail作爲您的smtp服務?如果您是的話,由於Google的安全性,您可能需要使用應用密碼而不是個人密碼。以下是https://support.google.com/accounts/answer/185833?hl=zh_CN – Tobidae

+0

是的方法。問題是我在用戶創建帳戶時使用相同的格式發送電子郵件。這是有效的。 – Ciprian

+0

嘗試捕獲發送錯誤:'mailTransport.sendMail(mailOptions).then(...)。catch(error => {console.error('Error sending:',error);});' –

回答

3

使用Firebase Cloud功能發送電子郵件的正確方法!

exports.sendTestEmail = functions.database.ref('/emails') 
    .onWrite(event => { 
    return sendTestEmail('[email protected]'); 
    }) 
// [END onWrite] 

// Sends a welcome email to the given user. 
function sendTestEmail(email) { 
    const mailOptions = { 
    from: `${APP_NAME} <[email protected]>`, 
    to: email 
    }; 
    // The user subscribed to the newsletter. 
    mailOptions.subject = `Welcome to ${APP_NAME}!`; 
    mailOptions.text = `Hey there! Welcome to ${APP_NAME}. I hope you will enjoy our service.`; 
    return mailTransport.sendMail(mailOptions).then(() => { 
    console.log('New welcome email sent to:', email); 
    }); 

}