我创建了一个小页面,用于将通知发送到邮件。
我正在使用nodejs和nodemailer发送通知。我的代码可以很好地发送通知。
require('dotenv').config();
const express = require('express')
const router = express.Router()
const {Pool,Query} = require('pg')
const path = require('path')
const nodemailer = require('nodemailer');
var config=require('../../config.json')
nodemailer.createTestAccount((err,account)=>{
const bodyMsg = `
<h3>New Contact Request</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.msg}</p>
`
let transporter = nodemailer.createTransport({
host: config.SMTP.host,
port: config.SMTP.port,
secure: false,
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
},
tls:{
rejectUnauthorized:false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: `"Contact us " ${config.SMTP.email}`,
to: process.env.TO,
subject: 'New report',
text: req.body.msg,
html: bodyMsg
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
//res.render('contact', {msg:'Email has been sent'});
});
})
})
})
如何使用Jest或酶测试nodemailer?