У меня есть эта функция электронной почты, которая отлично работает на моем локальном хосте: / 3000, но не на рабочем сервере. Что странно, он отлично работает даже на рабочем сервере для отправки электронной почты, если это простая электронная почта, а не электронная почта с версией вложения. Я не могу сказать, что отличается в производстве, что вызывает его сбой. Я получаю сообщение об ошибке:
502 Bad Gateway nginx / 1.14.0 (Ubuntu)
Функция состоит в том, чтобы отфильтровать базу данных и отправить результаты запроса по электронной почте на адрес Пользователь. Где мне даже начать устранять неполадки?
the file which works contact.handlebars
<form action="/emailfromcustomer" method="post">
<input type="name" class="form-control" name="customerfirst" placeholder="First Name">
<input type="submit" class="btn btn-dark" value="Send Inquiry"></input>
</form>
file which doesnt work email.handlebars
<form action="/email" method="post">
<input id="date1" name="date1">
<input type="submit" class="btn btn-success" value="Send the email"></input>
</form>
routes:
router.get('/contact', (req, res, next) => {
res.render('contact');
});
router.get('/email', function(req, res) {
res.render('email')
});
router.post('/emailfromcustomer', function(req, res) {
var customerfirst = req.body.customerfirst || 'NoFirstNmae'
const nodemailer = require("nodemailer");
main().catch(console.error);
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "myhost.com",
port: 465,
/*587 25*/
secure: true, // true for 465, false for other ports
auth: {
user: 'theuser',
pass: 'somepass'
}
});
let info = await transporter.sendMail({
from: 'someemail@gmail.com',
to: 'myemailgmail.com', // list of receivers
subject: "You got mail", // Subject line
text: "Customer " + customerfirst
});
res.send('Your message has been sent!')
}
});
router.post('/email', function(req, res) {
sendEmail();
function sendEmail() {
var thequery1 = `select top 4 product,units_sold,manufacturing_price
FROM table
where 1 = 1 `
if (req.body.date1) {
thequery1 = thequery1 + ' and Date >= @date1 '
}
var date1 = req.body.date1 || '2000-01-01'
let promise = queries.queryTablewithPararams(thequery1, date1);
promise.then(
data => {
var csv = [];
const fields = ['product', 'units_sold', 'manufacturing_price'];
const json2csvParser = new Json2csvParser({
fields
});
csv = json2csvParser.parse(data);
var path = './public/serverfiledownload/file' + Date.now() + '.csv';
fs.writeFile(path, csv, function(err, data) {
if (err) {
throw err;
} else {
main(csv, path).catch(console.error);
res.send('it sent email')
}
});
}
);
}
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main(csv, path) {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
let transporter = nodemailer.createTransport({
host: "myhost.com",
port: 465,
/*587 25*/
secure: true, // true for 465, false for other ports
auth: {
user: 'theuser',
pass: 'somepass' // ''generated ethereal password
}
});
let info = await transporter.sendMail({
from: 'someemail@gmail.com', // sender address
to: req.body.inputemail, // list of receivers
subject: "dynamic attachment", // Subject line
text: "Hello world?", // plain text body
attachments: [{ // utf-8 string as an attachment
filename: path,
content: csv
}],
html: `<b>Please find your report attached!</b>` // html body
});
console.log("Message sent: %s", info.messageId);
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
});
/etc/nginx/conf.d/server.conf
#
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
}
}
#