如何将ajax与NodeEmailer一起用于发送邮件?
我需要发送邮件与nodemailer使用ajax显示消息确认,没有了我的页面 另一个问题是,如果在前端使用代码ajax发送两封邮件 ============================ app.js如何将ajax与NodeEmailer一起用于发送邮件?,ajax,node.js,frontend,nodemailer,Ajax,Node.js,Frontend,Nodemailer,我需要发送邮件与nodemailer使用ajax显示消息确认,没有了我的页面 另一个问题是,如果在前端使用代码ajax发送两封邮件 ============================ app.js app.post('/enviar', function(req,res){ var name = req.body.nombre; var mail = req.body.correo; var messege = req.body.mensaje; var mail_from =
app.post('/enviar', function(req,res){
var name = req.body.nombre;
var mail = req.body.correo;
var messege = req.body.mensaje;
var mail_from = "servicios@fractalservicios.com";
var subject_from = "Contact web fractal nodejs";
var transporter = nodemailer.createTransport(smtpTransport({
host: "*****",
port: ***,
auth: {
user: "****",
pass: "****"
}
}));
var mailOptions = {
from: name + ' ' + mail, // sender address
to: mail_from, // list of receivers
subject: subject_from , // Subject line
html: messege // html body
};
transporter.sendMail(mailOptions,function(error,result){
if(error){
console.log(error);
console.log("salio mal");
//res.end("error");
res.render('error',{titulo: 'error al enviar menmsaje'});
}else{
console.log("Message sent: " + res.message);
console.log("correcto");
res.redirect('/');
//res.render('enviado',{titulo: 'mensaje enviado'});
}
//res.redirect('/');
});
})
build.js=>前端
var nombre = $('#nombre').val();
var correo = $('#correo').val();
var mensaje = $('#mensaje').val();
var enviar_info = {
"nombre": nombre,
"correo": correo,
"mensaje": mensaje
};
$('.send_mail').on('click',function(){
$.ajax({
type: "POST",
url: "/enviar",
data: JSON.stringify(enviar_info),
contentType:"application/json; charset=utf-8",
dataType: 'json',
success: function(e){
alert("genial se envio tu mensaje");
}
});
});
我最近遇到了同样的问题,我尝试了以下方法。这对我来说就像一个魔术,迟来的回答,但我相信其他人可能需要它
$(function() {
$('#contact-form').on('submit', function(event) {
event.preventDefault();
let name = $('input[name="name"]').val(),
company = $('input[name="company"]').val(),
email = $('input[name="email"]').val(),
phone = $('input[name="phone"]').val(),
message = $('textarea[name="message"]').val();
$.ajax({
url: '/',
method: "POST",
contentType: 'application/json',
data: JSON.stringify({
name,
company,
email,
phone,
message
}),
success: function(response) {
console.log(response);
},
fail: function(error) {
console.log(error);
}
});
});
});
在server.js中
app.post('/', function(req, res) {
const output = `
<p>You have a new contact request</p>
<h3>contact details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Company: ${req.body.company}</li>
<li>Email: ${req.body.email}</li>
<li>Phone: ${req.body.phone}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
let transporter = nodemailer.createTransport({
service: 'gmail',
host: 'mail.domain.com',
port: 465,
tls: {
rejectUnauthorized: false, //NOTE: you only need to set rejectUnauthorized to false if you are running on a local server, you can remove it after testing
}
});
let mailOptions = {
from: `nodemailer contact ${req.body.email}`,
to: 'info@domain.com',
subject: 'User Form Contact',
html: output
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
res.send({
msg: 'Email has been sent!'
});
});
});
app.post('/',函数(req,res){
常量输出=`
您有一个新的联系请求
联系方式
- 名称:${req.body.Name}
- 公司:${req.body.Company}
- 电子邮件:${req.body.Email}
- 电话:${req.body.Phone}
消息
${req.body.message}
`;
让transporter=nodeEmailer.createTransport({
服务:“gmail”,
主机:“mail.domain.com”,
港口:465,
tls:{
rejectUnauthorized:false,//注意:如果在本地服务器上运行,则只需将rejectUnauthorized设置为false,测试后即可将其删除
}
});
让邮件选项={
发件人:`nodeEmailer联系人${req.body.email}`,
致:'info@domain.com',
主题:“用户表单联系人”,
html:输出
};
transporter.sendMail(邮件选项,函数(错误,信息){
如果(错误){
返回console.log(错误);
}
console.log('发送的消息:%s',info.messageId);
log('预览URL:%s',nodeEmailer.getTestMessageUrl(信息));
res.send({
消息:“电子邮件已发送!”
});
});
});
谢谢,我觉得这很有用。