Sending mailgun emails from Cloud Functions for Firebase in an angular 2 app

非 Y 不嫁゛ 提交于 2019-11-29 07:11:13

As stated by @GokulKathirvel, only paid accounts will send outbound emails. But I was able to attest the functionality in the functions dashboard. You'll receive the following message when the function is triggered:

Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

With that out of the way, you should also be able to do that using the node package mailgun-js.

var functions = require('firebase-functions')
var mailgun = require('mailgun-js')({apiKey, domain})

exports.sendWelcomeEmail = functions.database.ref('users/{uid}').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
  }

  var user = event.data.val()
  var {email} = user

  var data = {
    from: 'app@app.com',
    subject: 'Welcome!',
    html: `<p>Welcome! ${user.name}</p>`,
    'h:Reply-To': 'app@app.com',
    to: email
  }

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

Source https://www.automationfuel.com/firebase-functions-sending-emails/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!