我遇到了问题。 我不知道sequelize.js。 我有一个HTML表单如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Forget Password</title>
</head>
<body>
Forget Password: <br/>
<form action="/forgetpasswordpartner" method="POST">
Enter PartnerID:
<input type="text" name="partnerid">
<button type="submit" value="Send Email">Send Email</button>
</form>
</body>
</html>
我有控制器:
exports.sendresetlinkpartner = function (req, res, next) {
var partnerid = req.body.partnerid;
var token = randomstring.generate({
length: 100,
charset: 'alphanumeric',
capitalization: 'uppercase'
});
var link = "http://localhost:3000/forgetpasswordpartner/" + token;
forgetpasswordmodel.sendresetemailpartner(partnerid, token, function (err, result) {
if (err) throw err;
else {
res.send('Email not found in database');
}
}, function (err, result) {
if (err) throw err;
else {
let transporter = nodemailer.createTransport({
host: 'xxxx',
port:xxx,
secure: false, // true for 465, false for other ports
auth: {
user: 'xxxxxx@xxxx.com', // generated ethereal user
pass: 'xxxxxxxxx' // generated ethereal password
},
//if email is sent via localhost
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: '"xxxxxx" <xxxxxxx@xxxxx.com>', // sender address
to: email, // list of receivers
subject: 'Reset Password', // Subject line
text: 'A password reset for your account was requested', // plain text body
html: "Please click the button below to change your password.<br><a href=" + link + ">" + "<h2>Change your password</h2>" + "</a> "
// html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.send('Couldnot send Reset Password email.Please try again later');
return console.log(error);
}
else {
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
});
res.send('To Change your password, a link has been sent to your email.Check your mail');
}
})
}
这是我的模特:
exports.sendresetemailpartner=function(partnerid,token,callback1,callback2){
partner.findOne({where:{'partnerid':partnerid}}).then(partner.update({'token':token},{where:{'companyemail':email}})).then((partner)=>{
if(!partner){
console.log('Email not registered');
callback1();
}
else{
console.log('Token Updated');
callback2();
}
})
我知道我的模特是错的 我想要的是首先检查数据库中是否存在'partnerid',如果存在,请在同一行记录中插入一个令牌并检索该合作伙伴ID的电子邮件,并将该emailid发送回控制器以存储在'email'变量中并发送该邮件
任何人都可以帮助我。
答案 0 :(得分:1)
使用单独的js文件发送电子邮件。因为电子邮件也会被其他一些方法访问。把它写成接受两个参数的通用。
//utils.js
module.exports = {
sendEmail: function(toEmail, link) {
return new Promise(function(resolve, reject) {
let transporter = nodemailer.createTransport({
host: 'xxxx',
port: xxx,
secure: false, // true for 465, false for other ports
auth: {
user: 'xxxxxx@xxxx.com', // generated ethereal user
pass: 'xxxxxxxxx' // generated ethereal password
},
//if email is sent via localhost
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: '"xxxxxx" <xxxxxxx@xxxxx.com>', // sender address
to: toEmail, // list of receivers
subject: 'Reset Password', // Subject line
text: 'A password reset for your account was requested', // plain text body
html: "Please click the button below to change your password.<br><a href=" + link + ">" + "<h2>Change your password</h2>" + "</a> "
// html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.send('Couldnot send Reset Password email.Please try again later');
//return console.log(error);
reject(error)
} else {
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
resolve("success")
}
});
});
}
}
//Require util.js
import Utils from '../utils'
exports.sendresetlinkpartner = function(req, res, next) {
var partnerid = req.body.partnerid;
var token = randomstring.generate({
length: 100,
charset: 'alphanumeric',
capitalization: 'uppercase'
});
var link = "http://localhost:3000/forgetpasswordpartner/" + token;
partner.findOne({
where: {
'partnerid': partnerid
}
}).then((fetchedPartner) => {
fetchedPartner.update({
token: token
}).then((postUpdate) => {
if (!postUpdate) {
console.log('Email not registered');
} else {
Utils.sendEmail('xyz@gmail.com', link).then((postEmail) => {
// your logic for success
});
}
});
});
}