Javascript 如何将get方法中的JSON数据构造成html并将其发送到电子邮件正文
需要将从GET方法检索到的JSON数据作为电子邮件发送。我需要通过获取的JSON对象定义电子邮件的正文。我不知道怎么解决这个问题 以下是我的示例代码:Javascript 如何将get方法中的JSON数据构造成html并将其发送到电子邮件正文,javascript,node.js,express,Javascript,Node.js,Express,需要将从GET方法检索到的JSON数据作为电子邮件发送。我需要通过获取的JSON对象定义电子邮件的正文。我不知道怎么解决这个问题 以下是我的示例代码: app.get('/userinfo',(req,res)=>{ dbconn.query('exec sp_GetDueList',(err,rows,fields)=>{ if(!err){ // console.log(rows.recordsets[0][5].Instrum
app.get('/userinfo',(req,res)=>{
dbconn.query('exec sp_GetDueList',(err,rows,fields)=>{
if(!err){
// console.log(rows.recordsets[0][5].Instrument)
for(let i =0; i<= rows.recordsets[0].length-1; i++){
console.log(JSON.stringify(rows.recordsets[0][i].Instrument)+ " "+
JSON.stringify(rows.recordsets[0][i].ID_No)+ " "+JSON.stringify(rows.recordsets[0][i].NextDueDate))
}
email.main(subject, body, res).catch((err) => {
console.log(err);
})
} else{
res.send(err);
}
res.end();
})
})
现在,我想将此数据作为电子邮件正文发送。我知道有人认为这个问题很愚蠢或太简单。。我到处找,找不到一个工作模型
如果有人能帮上忙,我会非常感激的我假设您还没有安装像NodeEmailer这样的软件包,请通过npm:
npm-I-S NodeEmailer安装它
var nodemailer = require('nodemailer');
//Here you have to set up your email client, try gmail it's free but otherwise you have to use a service like sendgrid...
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
}
});
然后,您必须在功能中使用电子邮件:
app.get('/userinfo', (req, res) => {
dbconn.query('exec sp_GetDueList', (err, rows, fields) => {
if (!err) {
// console.log(rows.recordsets[0][5].Instrument)
let emailBody;
for (let i = 0; i <= rows.recordsets[0].length - 1; i++) {
console.log(JSON.stringify(rows.recordsets[0][i].Instrument) + " " +
emailBody += JSON.stringify(rows.recordsets[0][i].ID_No) + " " + JSON.stringify(rows.recordsets[0][i].NextDueDate))
}
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
html: `<h1>Hello here\'s the json</h1><p>${emailBody}</p>`
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
} else {
res.send(err);
}
res.end();
})
})
app.get('/userinfo',(req,res)=>{
dbconn.query('exec sp_GetDueList',(err,rows,fields)=>{
如果(!err){
//console.log(rows.recordset[0][5]。仪器)
让我们的身体;
对于(设i=0;i这个问题非常模糊,您是否已经实现了类似NodeEmailer的功能?这就是您要问的——如何通过node发送电子邮件?请澄清您最终要完成的任务。您好,我需要将get请求中的JSON数据作为电子邮件正文发送。是否有任何方法发送它。我在m中编辑了电子邮件正文我收到的所有问题都是用户无法理解的。我唯一的目标是以干净的格式将json数据发送到电子邮件。到目前为止,当我尝试时,json数据在正文中以一句话的形式出现。但我想逐行格式化。感谢您的帮助。。
app.get('/userinfo', (req, res) => {
dbconn.query('exec sp_GetDueList', (err, rows, fields) => {
if (!err) {
// console.log(rows.recordsets[0][5].Instrument)
let emailBody;
for (let i = 0; i <= rows.recordsets[0].length - 1; i++) {
console.log(JSON.stringify(rows.recordsets[0][i].Instrument) + " " +
emailBody += JSON.stringify(rows.recordsets[0][i].ID_No) + " " + JSON.stringify(rows.recordsets[0][i].NextDueDate))
}
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
subject: 'Sending Email using Node.js',
html: `<h1>Hello here\'s the json</h1><p>${emailBody}</p>`
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
} else {
res.send(err);
}
res.end();
})
})