如何将nodemailer与云端函数一起用于Firebase? [英] How can I use nodemailer with 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 Fireworks的云端函数,但不断出现错误,但似乎仍然不断因为smpt服务器无法到达或找到。我试过gmail,outlook和一个正常的托管smpt服务。它可以从我的本地节点服务器上运行。
这是从失败的尝试发送邮件时收到的记录错误:
{
错误: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',
主机:'smtp-mail.outlook.com',
端口:'587',
命令:'CONN'
}
如果有人有足够的名声,请用 firebase-cloud-functions 我创建了一个云端函数(http事件),通过以下方式从我的网站的联系表单部分发送电子邮件:http://blog.csdn.net/shenzhen/article/details :
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
const rp = require('request-promise');
//用于发送电子邮件的Google帐户凭证
const mailTransport = nodemailer.createTransport(
`smtps://user@domain.com:password@smtp.gmail.com `);
exports.sendEmailCF = functions.https.onRequest((req,res)=> {
//验证码验证
rp({
uri:'https://recaptcha.google.com/recaptcha/api/siteverify',
方法:'POST',
formData:{
秘密:'your_secret_key',
response:req.body ['g-recaptcha-response']
},
json:true
})。then(result => {
if(result。 (()=> {
res.status(200).send(true);
){
sendEmail(iscipient@gmail.com',req.body) });
}
else {
res.status(500).send(Recaptcha failed。)
}
})。catch(reason => ; {
res.status(500).send(Recaptcha req failed。)
})
});
发送邮件函数
函数sendEmail(email,body){
const mailOptions = {
from:`< noreply@domain.com>`,
到:email
};
// hmtl消息结构
mailOptions.subject ='联系表单消息';
mailOptions.html =`< p>< b>名称:< / b> $ {body.rsName}< / p>
< p>< b>电子邮件:< / b> $ {body.rsEmail}< / p>
< p>< b> Subject:< / b> $ {body.rsSubject}< / p>
< p>< b>讯息:< / b> $ {body.rsMessage}< / p>`;
返回mailTransport.sendMail(mailOptions);
}
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'
}
Could someone with enough reputation please tag this question with firebase-cloud-functions?
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与云端函数一起用于Firebase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!