如何将 nodemailer 与 Cloud Functions for Firebase 一起使用? [英] How can I use nodemailer with Cloud Functions for Firebase?

查看:20
本文介绍了如何将 nodemailer 与 Cloud Functions for Firebase 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 nodemailer/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiy3IXgpZfTAhXhB5oKHX3XDP8QFggaMAA&url=https%3A%2F%2Ffirebase.google.com%2Fdocs%2Ffunctions%2F&usg=AFQjCNFdWm1QGO5vqjgc-GQ9UjC_XotMlQ&sig2=dHWfybYbAMvBtLHLU7c3Bw&bvm=bv.152174688,d.bGs" rel="nofollow noreferrer">Cloud Functions for Firebase是无法访问或找到 smpt 服务器.我试过 gmail、outlook 和正常的托管 smpt 服务.它在我的本地节点服务器上运行良好.

I'm trying to use nodemailer in a Cloud Functions for Firebase but keep getting errors seeming to be that the smpt server cant be reached or found. Iv'e tried gmail, outlook and a normal hosted smpt service. It works well from my local node server.

这是我在尝试发送电子邮件失败时收到的记录错误:

This is the logged error I receive from the failed attempt to send email:

{
  Error: getaddrinfoENOTFOUNDsmtp-mail.outlook.comsmtp-mail.outlook.com: 587aterrnoException(dns.js: 28: 10)atGetAddrInfoReqWrap.onlookup[
    asoncomplete
  ](dns.js: 76: 26)code: 'ECONNECTION',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'smtp-mail.outlook.com',
  host: 'smtp-mail.outlook.com',
  port: '587',
  command: 'CONN'
}

推荐答案

我创建了一个云函数(http 事件),通过以下方式从我的网站的联系表单部分发送电子邮件:

I created a cloud function (http event) to send emails from the contact form section of my site with the following way:

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
const rp = require('request-promise');

//google account credentials used to send email
const mailTransport = nodemailer.createTransport(
    `smtps://user@domain.com:password@smtp.gmail.com`);

exports.sendEmailCF = functions.https.onRequest((req, res) => {

  //recaptcha validation
  rp({
        uri: 'https://recaptcha.google.com/recaptcha/api/siteverify',
        method: 'POST',
        formData: {
            secret: 'your_secret_key',
            response: req.body['g-recaptcha-response']
        },
        json: true
    }).then(result => {
        if (result.success) {
            sendEmail('recipient@gmail.com', req.body).then(()=> { 
              res.status(200).send(true);
            });
        }
        else {
            res.status(500).send("Recaptcha failed.")
        }
    }).catch(reason => {
        res.status(500).send("Recaptcha req failed.")
    })

});

// Send email function
function sendEmail(email, body) {
  const mailOptions = {
    from: `<noreply@domain.com>`,
    to: email
  };
  // hmtl message constructions
  mailOptions.subject = 'contact form message';
  mailOptions.html = `<p><b>Name: </b>${body.rsName}</p>
                      <p><b>Email: </b>${body.rsEmail}</p>
                      <p><b>Subject: </b>${body.rsSubject}</p>
                      <p><b>Message: </b>${body.rsMessage}</p>`;
  return mailTransport.sendMail(mailOptions);
}

这篇关于如何将 nodemailer 与 Cloud Functions for Firebase 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆