我按照一个示例从我的联系表发送电子邮件,但是它不起作用。代码的最后一行包括端口#3000。我无法在服务器上使用它。
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const nodemailer = require('nodemailer');
const app = express();
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false}));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.render('contact');
});
app.post('/send', (req, res) => {
const output = '
<p>You have a new contact request</p>
<h3>Contant Details</h3>
<ul>
<li>First Name: ${req.body.firstName}</li>
<li>Last Name: ${req.body.lastName}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.emailInfo}</p>
';
let transporter = nodemailer.createTransport({
host: "mail.tinycakesbakery.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'support@tinycakesbakery.com', // generated ethereal user
pass: 'Left out on purpose' // generated ethereal password
}
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Tiny Cakes Bakery" support@tinycakesbakery.com', // sender address
to: "Left out on purpose", // list of receivers
subject: "Form Submission", // Subject line
text: "Hello world?", // plain text body
html: output // html body
});
transporter.sendMail(mainOptions, (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'});
});
});
app.listen(3000, () => console.log('Server Started...'));
我不确定是否需要最后一行。