node.js - 在 Angular 2 应用程序中从 Cloud Functions for Firebase 发送 mailgun 电子邮件

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

我正在尝试使用来自 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 所述,只有付费帐户才会发送出站电子邮件。但我能够证明功能仪表板中的功能。触发该功能时,您将收到以下消息:

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

除此之外,您还应该能够使用 Node 包 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/

关于node.js - 在 Angular 2 应用程序中从 Cloud Functions for Firebase 发送 mailgun 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44259405/

相关文章:

javascript - Express 将在本地主机上正确设置 CORS,但不会在实时服务器上设置

node.js - Angular/TypeScript 编译的初学者问题

java - 从 Android Firebase 中删除值会删除整个节点

android - 在我的项目中没有使用 firebase 但仍然抛出 Firebase API 初始化失败

javascript - 如何检测初始页面加载是否来自重定向

javascript - 当函数需要访问外部范围变量时处理 Promise 流的更好方法

javascript - gulp-sass 和 gulp-watch,保存 scss 时找不到文件

javascript - 从 electron-vue 前端启动 docker 后端

javascript - 在产品模式下构建时如何以 Angular 显示 package.json 中的版本

angular - 如何在Ionic 4中刷新页面