一尘不染

在Angle 2应用程序中从Cloud Functions for Firebase发送Mailgun电子邮件

node.js

我正在尝试从Firebase云功能使用Mailgun的api发送电子邮件。我曾尝试在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>

谢谢你的协助。


阅读 173

收藏
2020-07-07

共1个答案

一尘不染

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

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

有了这一点,您还应该能够使用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)
  })
})

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

2020-07-07