admin管理员组文章数量:1386881
So this is a very odd question and I don't expect any one to really have an answer to this, but I'm here to try and see if anyone has experienced the same issue.
The issue that I'm noticing is that our application seems to be sending duplicate emails. For example, I can send a report from our application, and it will send that email once, and then it looks like another one gets sent exactly a minute later.
I'm using nodemailer
to send the emails from our applications server, and our default email that we use in our office is Outlook v16.0.12130.20272
using IMAP. These emails are being sent by our noreply email which I believe is being hosted through GoDaddy.
I've sent test emails myself and looked in the network tab to see if it might be a timeout issue, but the response pletes with a 200 OK status
and the timing shows up as pleted as well. Also when I console log the response it only occurs once, which makes me believe that it is actually only sending one email. There must be something happening in between when the host sends the email, and when our recipients actually receive them, but I'm not quite sure.
Here is the server.js file. This is where the smtp
request is being made.
var nodemailer = require("nodemailer");
const path = require('path');
const express = require('express');
const http = require('http');
const fs = require('fs');
const socketIO = require('socket.io');
const bodyParser = require('body-parser')
import env from '../env.config.json';
const PORT = require('../env.config.json').SERVER.PORT;
const publicPath = path.join(__dirname, '../public');
import api from './routers/api-routing';
//---------------------------------------------------------------------------
var smtpTransport = nodemailer.createTransport({
service: env.EMAIL.SERVICE,
host: env.EMAIL.HOST,
auth: {
user:env.EMAIL.AUTH.USER,
pass:env.EMAIL.AUTH.PASS
}
});
var mailCounter = 0;
var numPeople = 0;
var app = express();
var server = http.createServer(app);
const port = PORT || 3000;
app.use(express.static(publicPath));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
const io = socketIO(server);
app.use('/api', api(app, io));
// require('./routers/api-routing')(app, io);
//$.get("/send", { to: to, subject: subject).value, text: 'html' }, function (data) { });
app.get('*', function (request, response) {
if (request.get('x-auth')) console.log("x-auth: ", request.get('x-auth'));
const proto = request.get('X-Forwarded-Proto');
if (proto) {
if (proto === 'http') response.redirect(301, "".concat(request.url));
}
response.sendFile(path.resolve(__dirname, '../', 'public', 'index.html'))
if ((request.url).substring(0, 5) == "/send") {
var mailOptions = {
to: request.query.to,
bcc: request.query.bcc,
subject: request.query.subject,
text: request.query.text
}
//console.log(mailOptions); Read up on NodeMailer for details.
smtpTransport.sendMail({ //email options
from: "COMPANY <[email protected]>", // sender address. Must be the same as authenticated user if using Gmail.
to: mailOptions.to,
bcc: "COMPANY <[email protected]>", // sending to itself
subject: mailOptions.subject, // subject
html: mailOptions.text, // body
}, function (error, response) { //callback
if (error) {
console.log(error);
} else {
console.log("Message sent");
//console.log("Amount of people getting this email: " + response.accepted.length);
}
smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
});
}
});
io.on('connection', (socket) => {
require('./middleware/sockets')(socket);
});
server.listen(port, () => {
console.log(`Server is up on port ${port}.`);
});
This is the part of our env.config.file that pertains to the emails.
"EMAIL": {
"SERVICE": "Godaddy",
"HOST": "smtp.gmail",
"AUTH": {
"USER": "[email protected]",
"PASS": "OURPASS"
}
}
If anyone has any ideas or suggestions, I would be very appreciative, thanks!
So this is a very odd question and I don't expect any one to really have an answer to this, but I'm here to try and see if anyone has experienced the same issue.
The issue that I'm noticing is that our application seems to be sending duplicate emails. For example, I can send a report from our application, and it will send that email once, and then it looks like another one gets sent exactly a minute later.
I'm using nodemailer
to send the emails from our applications server, and our default email that we use in our office is Outlook v16.0.12130.20272
using IMAP. These emails are being sent by our noreply email which I believe is being hosted through GoDaddy.
I've sent test emails myself and looked in the network tab to see if it might be a timeout issue, but the response pletes with a 200 OK status
and the timing shows up as pleted as well. Also when I console log the response it only occurs once, which makes me believe that it is actually only sending one email. There must be something happening in between when the host sends the email, and when our recipients actually receive them, but I'm not quite sure.
Here is the server.js file. This is where the smtp
request is being made.
var nodemailer = require("nodemailer");
const path = require('path');
const express = require('express');
const http = require('http');
const fs = require('fs');
const socketIO = require('socket.io');
const bodyParser = require('body-parser')
import env from '../env.config.json';
const PORT = require('../env.config.json').SERVER.PORT;
const publicPath = path.join(__dirname, '../public');
import api from './routers/api-routing';
//---------------------------------------------------------------------------
var smtpTransport = nodemailer.createTransport({
service: env.EMAIL.SERVICE,
host: env.EMAIL.HOST,
auth: {
user:env.EMAIL.AUTH.USER,
pass:env.EMAIL.AUTH.PASS
}
});
var mailCounter = 0;
var numPeople = 0;
var app = express();
var server = http.createServer(app);
const port = PORT || 3000;
app.use(express.static(publicPath));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
const io = socketIO(server);
app.use('/api', api(app, io));
// require('./routers/api-routing')(app, io);
//$.get("/send", { to: to, subject: subject).value, text: 'html' }, function (data) { });
app.get('*', function (request, response) {
if (request.get('x-auth')) console.log("x-auth: ", request.get('x-auth'));
const proto = request.get('X-Forwarded-Proto');
if (proto) {
if (proto === 'http') response.redirect(301, "https://ourdomain.".concat(request.url));
}
response.sendFile(path.resolve(__dirname, '../', 'public', 'index.html'))
if ((request.url).substring(0, 5) == "/send") {
var mailOptions = {
to: request.query.to,
bcc: request.query.bcc,
subject: request.query.subject,
text: request.query.text
}
//console.log(mailOptions); Read up on NodeMailer for details.
smtpTransport.sendMail({ //email options
from: "COMPANY <[email protected]>", // sender address. Must be the same as authenticated user if using Gmail.
to: mailOptions.to,
bcc: "COMPANY <[email protected]>", // sending to itself
subject: mailOptions.subject, // subject
html: mailOptions.text, // body
}, function (error, response) { //callback
if (error) {
console.log(error);
} else {
console.log("Message sent");
//console.log("Amount of people getting this email: " + response.accepted.length);
}
smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
});
}
});
io.on('connection', (socket) => {
require('./middleware/sockets')(socket);
});
server.listen(port, () => {
console.log(`Server is up on port ${port}.`);
});
This is the part of our env.config.file that pertains to the emails.
"EMAIL": {
"SERVICE": "Godaddy",
"HOST": "smtp.gmail.",
"AUTH": {
"USER": "[email protected]",
"PASS": "OURPASS"
}
}
If anyone has any ideas or suggestions, I would be very appreciative, thanks!
Share Improve this question edited Nov 7, 2019 at 15:32 Michael asked Nov 7, 2019 at 15:28 MichaelMichael 1,7345 gold badges24 silver badges52 bronze badges 15- Is the ReactJS tag required here? – Sasha Commented Nov 7, 2019 at 15:32
- probably not, I just removed it. – Michael Commented Nov 7, 2019 at 15:33
- I have faced this same issue (Duplicate mail after exactly a minute). I fixed it by explicitly adding 'status(200)' to the response. Let me know if it works! – Krishna Commented Nov 7, 2019 at 15:34
-
The code is using a lot of dif techniques to import libraries, just a suggestion to try and unify that. Also you're sending a redirect but allowing the rest of the script to run, that could be an issue. The 60 seconds makes me think a request is timing out and hitting a URL again or something. You're also listening for all GET requests (
*
), I'd limit that to the routes that you want this code to actually run on. Finally, are you positive you're only seeing the console output in your node app 1 time? If that's the case, check GoDaddy. Perhaps your BCCing noreply causes an auto-reply? – Charlie Schliesser Commented Nov 7, 2019 at 15:42 - @Krishna you're saying put response.status(200) inside that else statement? – Michael Commented Nov 7, 2019 at 15:46
2 Answers
Reset to default 6Your email is being sent on any request sent to your server so if you access it via a browser, the browser will send two requests, one for the path requested and one for the favicon.ico and you're also sending an email when /favicon.ico
is requested.
This can happen because you're route handler is configured as:
app.get('*', ...);
That means you're attempting to send an email for every single ining http request regardless of path.
So, if you go to your host with a browser at http://yourdomain/
, it will first request /
and then the browser will request /favicon.ico
, causing you to then send a second email.
My suggestion is to change from this:
app.get('*', ...);
to this:
app.get('/', ...);
or, even more specific such as:
app.get('/sendemail', ...);
So, you are only sending the email on one specific path request and it will not send the email no other requests such as the favicon. You probably want to add a generic express 404 handler for any other routes.
Note: In a REST design, you would probably send an email with a POST request, not a GET request. A GET would retrieve a resource in a read-only kind of way that does not change any state so it wouldn't have a side effect of sending an email. Note: this isn't related to your problem at all, just a ment about a typical REST design.
After some time I have finally figured out the reason for this behavior. The issue is partially related to what jfriend00
had posted. I ended up making a separate route handler for the emails themselves so that it didn't interfere with the main route handler. The problem is that every request will still go through that route since it is looking for any request indicated by the *
and if a person is on the http route making the request instead of https, then it creates a second request or in my case a second email.
You can see that happening in this line here:
if (proto) {
if (proto === 'http') response.redirect(301, "https://ourdomain.".concat(request.url));
}
The actual fix for this was to create a separate route handler for the emails themselves and then configure my nginx
server to reroute to https if a person was going to the http route of the application instead. After this, we have not experienced anymore duplicate emails.
Another way of doing it would be to remove the ```*```` route handler pletely and setup the other routes separately. Hopefully this will help somebody in the near future.
本文标签: javascriptWhy is nodemailer sending duplicate emailsStack Overflow
版权声明:本文标题:javascript - Why is nodemailer sending duplicate emails? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744573063a2613455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论