从angular色2应用程序的Firebase云端函数发送mailgun电子邮件

我正尝试使用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> 

感谢你的协助。

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

帐单帐户未configuration。 外部networking不可访问,配额受到严重限制。 configuration结算帐户以删除这些限制

mailgun-js ,你还应该可以使用节点包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/