2017-06-08 22 views
9

/emailsに何かが書かれているが、電子メールが送信されず、ファンクションログが空である場合は、電子メールにテスト電子メールを送信しようとしています。Firebaseのクラウド機能 - メールを送信onWrite

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

} 

編集***

上記のコードは動作します。正しい名前がemailの間に私はemailsの機能をトリガーしようとしていました。

+0

あなたはsmtpサービスとしてGmailを使用していますか?もしそうなら、Googleのセキュリティのために個人パスワードの代わりにアプリパスワードを使用する必要があるかもしれません。これを行う方法は次のとおりです。https://support.google.com/accounts/answer/185833?hl=ja – Tobidae

+0

はい問題は、ユーザーがアカウントを作成するときに同じ形式で電子メールを送信することです。それはうまくいく。 – Ciprian

+0

sendでエラーをキャッチしよう: 'mailTransport.sendMail(mailOptions).then(...)。catch(error => {console.error( 'Error sending:'、エラー);});' –

答えて

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

} 
関連する問題