【问题标题】:Sending mailgun emails from Cloud Functions for Firebase in an angular 2 app在 Angular 2 应用程序中从 Cloud Functions for Firebase 发送 mailgun 电子邮件
【发布时间】:2017-10-30 18:16:13
【问题描述】:

我正在尝试使用 Mailgun 的 api 从 firebase 云功能发送电子邮件。我已经尝试在 Cloud Function 中实现相同的 nodejs 教程,但我总是得到“错误:无法处理请求”。请问我做错了什么。

云函数代码如下:

 <pre>
  <code>
 var functions = require('firebase-functions');

 var nodemailer = require('nodemailer');

  var auth = {
  auth: {
      api_key: '###################',
       domain: 's###############g'
   }
 }
 exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
  });

   var nodemailerMailgun = nodemailer.createTransport(auth);

 exports.sendEmail = functions.https.onRequest((request, response) =>{
  //app.get('/', function(req, res) {
   test();
 });

  function test(){
     const mailOptions = {
        //Specify email data
            from: "info@xyz.com",
            //The email to contact
        to: "xyz@yahoo.com",
        //Subject and text data  
        subject: 'Hello from Mailgun',
        text: 'Hello, This is not a plain-text email, I wanted to test        some spicy Mailgun sauce in NodeJS! <a href="http://0.0.0.0:3030/validate?' +     req.params.mail + '">Click here to add your email address to a mailing     list</a>'
   };
    return smtpTransport.sendMail(mailOptions).then(() => {
    console.log("It works");
  });
}
</pre>

谢谢你的协助。

【问题讨论】:

  • 感谢编辑@AL
  • 您是 Firebase 的付费订阅者之一吗? Firebase 仅在付费计划中允许非 Google 出站网络请求。
  • @GokulKathirvel。还没有。会不会是这个问题?
  • 是的@D_Edet ... firebase 将只允许在付费计划中访问非谷歌服务。可悲的是,您需要升级到任何付费计划...
  • @GokulKathirvel。谢谢指出。

标签: node.js angular firebase mailgun google-cloud-functions


【解决方案1】:

正如@GokulKathirvel 所述,只有付费帐户才会发送出站电子邮件。但我能够证明功能仪表板中的功能。触发该函数时,您将收到以下消息:

未配置结算帐户。外部网络无法访问并且 名额受到严格限制。配置结算帐户以删除这些 限制

除此之外,您还应该能够使用节点包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)
  })
})

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

【讨论】:

  • 非常感谢@Marcos
  • 如果您不想从免费计划升级但仍想发送电子邮件,您仍然可以通过 Cloud Functions 进行出站网络呼叫,但仅限 Google 服务。这意味着您可以使用 gmail 帐户在 nodemailer 软件包的帮助下发送电子邮件。
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 2021-03-30
  • 2017-09-16
  • 2016-07-15
  • 2017-08-28
相关资源
最近更新 更多