在firebase函数中设置mailgun

时间:2017-11-01 21:39:33

标签: firebase google-cloud-functions mailgun

按照本教程,我正在努力在firebase函数上使用mailgun: https://www.automationfuel.com/firebase-functions-sending-emails/

我希望在将某些内容推送到我的实时数据库中的特定节点时通过电子邮件通知我。代码看起来像这样:

const functions = require('firebase-functions')
const mailgun = require('mailgun-js')({
  apiKey: 'key-<######>',
  domain: '<mydomain>.com',
})

exports.sendEmail = functions.database
  .ref('someParent/someChild')
  .onWrite(event => {
    // only trigger for new users [event.data.previous.exists()]
    // do not trigger on delete [!event.data.exists()]
    if (!event.data.exists() || event.data.previous.exists()) {
      return
    }

    const post = event.data.val()
    console.log(post)
    const { email, message } = post

    const data = {
      from: 'mailgun@firebase.com <is this free choice???>',
      subject: 'Hello Word!',
      html: `<p>It's alive!!!</p>`,
      'h:Reply-To': mail,
      to: '<myOwn>@gmail.com',
    }

    mailgun.messages().send(data, function(error, body) {
      console.log(body)
    })
  })

日志

中的以下消息一直失败

Function returned undefined, expected Promise or value

不可能看到更多,这使得调试变得困难..

(我在部署到firebase函数之前从ES6进行了一些转换)

更新

我放弃了发送电子邮件的概念,而是去了一些物联网。

但我认为这可能对你有用(或者让你有所帮助)

const functions = require('firebase-functions')
const mailgun = require('mailgun-js')({
  apiKey: 'key-<######>',
  domain: '<mydomain>.com',
})

exports.sendEmail = functions.database
  .ref('someParent/someChild')
  .onWrite(event => {
    // Do some checking of data if needed. 

    const post = event.data.val()
    console.log(post)
    const { email, message } = post

    const data = {
      from: 'mailgun@firebase.com <is this free choice???>',
      subject: 'Hello Word!',
      html: `<p>It's alive!!!</p>`,
      'h:Reply-To': mail,
      to: '<myOwn>@gmail.com',
    }

    return mailgun.messages().send(data, (error, body) => {
      console.log(body)
    })
  })

0 个答案:

没有答案