nodemailer 不发送邮件
[英] Mail not send by nodemailer
本文介绍了nodemailer 不发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 nodemailer 发送邮件,但代码没有发送邮件,也没有显示任何错误.通过用户输入添加到、来自、主题、来自 ejs 表单的消息并使用 nodemailer 我想将邮件发送到 Gmail 帐户,但它没有发送.请任何帮助
mail.ejs
<div class="card-header"><p><b>发送邮件</b></p>
<br/><form action="/send-email" method="post"><div class="col-md-12" style="overflow-x:auto;"><div class="form-group"><input type="text" class="form-control" placeholder="To" name="to" value="">
<div class="form-group"><input type="text" class="form-control" placeholder="From" name="from" value="">
<div class="form-group"><input type="text" class="form-control" placeholder="Subject" name="message" value="">
<div class="form-group"><textarea class="form-control" id=""rows="4"></textarea>
<button type="submit" class="btn btn-primary">发送</button>
</表单><br/><br/>
Controller.js
router.get('/donation/mail', async (req,res) => {res.render('邮件');});var 传输 = nodemailer.createTransport(smtpTransport({服务:gmail",授权:{//应替换为真实发件人的帐户用户:'xxxxx@gmail.com',通过:'mygmailpassword'}}));router.post('/send-email', function (req, res) {var 邮件选项 = {//应替换为真实收件人的帐户到:req.body.to,来自:'xxxxx@gmail.com',主题:req.body.subject,正文:req.body.message};transport.sendMail(mailOptions, function(error, info) {如果(错误){返回 console.log(error);}console.log('消息发送:' + info.response);});res.redirect("/捐赠");});
运行后得到消息在此处输入图片描述
我的邮件表格[在此处输入图片说明][2]
解决方案
在使用 Gmail 发送电子邮件之前,您必须允许非安全应用访问 Gmail.这可以在您的 Gmail 设置中完成:
https://myaccount.google.com/lesssecureapps?pli=1
我还可以推荐以下文章,了解使用 gmail 设置 nodemailer 的分步说明:https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799
I am trying to send mail using nodemailer but the code is not sending the mail also not showing any error. By user input add to, from, subject, a message from the ejs form and using nodemailer I want to send the mail to Gmail account but it's not sending. Please any help
mail.ejs
<div class="card">
<div class="card-header">
<p>
<b>Send mail</b>
</p>
</div>
<br/>
<form action="/send-email" method="post">
<div class="col-md-12" style="overflow-x:auto;">
<div class="form-group">
<input type="text" class="form-control" placeholder="To" name="to" value="">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="From" name="from" value="">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Subject" name="message" value="">
</div>
<div class="form-group">
<textarea class="form-control" id="" rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</div>
</form>
<br/>
<br/>
</div>
Controller.js
router.get('/donation/mail', async (req,res) => {
res.render('mail');
});
var transport = nodemailer.createTransport(smtpTransport({
service: "gmail",
auth: {
// should be replaced with real sender's account
user: 'xxxxx@gmail.com',
pass: 'mygmailpassword'
}
}));
router.post('/send-email', function (req, res) {
var mailOptions = {
// should be replaced with real recipient's account
to: req.body.to,
from:'xxxxx@gmail.com',
subject: req.body.subject,
body: req.body.message
};
transport.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
res.redirect("/donation");
});
After running get the message
enter image description here
My mail form
[enter image description here][2]
解决方案
Before sending your email using gmail you have to allow non secure apps to access gmail. This can be done in your gmail settings:
https://myaccount.google.com/lesssecureapps?pli=1
I can also recommend the following article for step by step instructions to setup nodemailer with gmail:
https://codeburst.io/sending-an-email-using-nodemailer-gmail-7cfa0712a799
这篇关于nodemailer 不发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文