When sending emails through my form in my React frontend it works as it should locally in development, but when I host the webapp on A2hosting in production no emails are being sent or received and I am not getting any errors, on the contrary I am getting an ok status request. Anyone know what the issue and solution might be?
Server.js:
const transport = nodemailer.createTransport({
host: "nl1-ss102.a2hosting.com",
port: 465,
secure: true,
auth: {
user: process.env.USER_SENDER,
pass: process.env.PASS
},
tls: {
rejectUnauthorized: true
}
});
app.post("/send", cors(), async (req, res) => {
let {
name,
//etc...
} = req.body;
await transport.sendMail({
from: `${name}`,
to: process.env.USER_RECEIVER,
subject: "Subject",
html: `
<ul>
<li></li>
</ul>
`
}, (err) => {
if (err) {
console.log(err);
res.status(400).send('Error');
} else {
res.status(200).send('Success');
}
})
});
I had a similar problem when I setup #sendGrid/mail. I would recieve the emails, but the values in the email would all be undefined. Then I saw [that] ("https://www.a2hosting.com/kb/getting-started-guide/setting-up-e-mail/using-external-smtp-servers-to-send-e-mail") a2hosting blocks external SMTP servers for clients who aren't paying for managed account's
Related
I am trying to send mail using nodemailer node module and configured like this.
... config.js ...
smtp: {
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: "***#gmail.com",
pass: "****"
}
}
... mail.js ...
const config = require("../config");
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport(config.smtp);
const sendEmail = async (data, res) => {
// if(!data.to || !data.subject)
// return false;
data = { ...data, from: config.smtp.auth.user };
console.log(data)
transporter.sendMail(data, (error, info) => {
console.log(error);
res.send(error);
});
res.send(true)
}
module.exports = sendEmail;
... controller ...
router.get('/testEmail', async (req, res) => {
let emailData = {
to: "***#gmail.com",
subject: "Welcome to Musical World",
text: "test test"
};
let result = sendEmail(emailData, res);
console.log(result)
// res.send(result);
})
When I try to send email, it returns
{"code":"EAUTH","response":"535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/?p=BadCredentials p25-20020a056638217900b0033c14d2386bsm898629jak.75 - gsmtp","responseCode":535,"command":"AUTH PLAIN"}
So I did googling and found that's why I didn't turn on less secure app in my google account.
But when I went there, google said
I tried to turn on less secure apps , but Gless secure apps feature isn't available.
What do I have to do , then?
I finally resolved by myself!.
Google no longer supports less secure app feature from 05/20/2022.
It will help other developers who get stuck with this issue.
So we can't access to gmail using only emal and password.
Instead we have to use OAuth2 to get access to gmail.
... config.js ...
smtp: {
service: "gmail",
auth: {
type: "OAuth2",
user: "*****#gmail.com",
pass: "******",
clientId: "********",
clientSecret: "***********",
refreshToken: "************"
}
}
You have to have OAuth2 client in your google cloud console for this.
Here's full details you can refer to.
https://www.freecodecamp.org/news/use-nodemailer-to-send-emails-from-your-node-js-server/
I use a Microsoft 365 Outlook account hosted on GoDaddy.
I am attempting to have my Node server send an email to my Outlook account when user clicks the [contact us] button. The code is below:
const nodemailer = require('nodemailer');
app.post('/contact', function(req, res){
res.send(JSON.stringify(req.body));
//generate email
let transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
secure: false,
auth: {
user: 'myemail#mysite.com',
pass: 'mypass'
}
});
const mailOptions = {
from: '"Administrator" <myemail#mysite.com>',
to: 'myemail#mysite.com',
subject: "Someone has messaged your website",
html: "<p>Name: " + req.body.fullName + "<br>Email: " + req.body.email + "<br>Message: " + req.body.msg + "</p>"
};
transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info.messageId);
});
});
The error that is being logged to console is:
'451 4.7.0 Temporary server error. Please try again later. PRX4 [CO2PR04CA0181.namprd04.prod.outlook.com]'
UPDATED 7/13
Another error message which may be more useful (as is first line in error stack) is the following:
at SMTPConnection._formatError
I am using this function to send mail to outlook, try and see if it works.
The only missing thing I can suspect is tls.
module.exports.sendEmail = function (options) {
if(!options){
throw new Error("invalid options");
} else if(!options.auth) {
throw new Error("missing user and pass");
}
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com', // Office 365 server
port: 587, // secure SMTP
secure:false, // false for TLS -
auth: options.auth,
tls: {ciphers: 'SSLv3'} // <<< try adding it
});
transporter.sendMail({
from: options.from,
sender: options.sender,
replyTo: options.replyTo,
to: options.to,
subject: options.subject,
cc:options.cc,
bcc:options.bcc,
text:options.text,
html:options.html,
attachments:options.attachments,
}, function (err, info) {
if (err && options.onError) {
options.onError(err);
}
else if(options.onSuccess) {
options.onSuccess(info);
}
});
}
****** caller ******
nodeoutlook.sendEmail({
auth: {
user: "user#outlook.com",
pass: "********"
},
from: 'from#outlook.com',
to: 'to#gmail.com',
subject: 'subject',
html: '<b>html</b>',
text: 'text',
replyTo: 'replyTo#gmail.com',
attachments: [],
onError: (e) => console.log(e),
onSuccess: (i) => console.log(i)
}
Understand this is a pretty lame answer....but its the truth!!
Im changed the password (mypass) for my email account then it worked.
I am using Google Cloud Functions to send E-Mail via Nodemailer, the mails are authenticated via Oauth2. It seems to work without any problems, the emails get sent and are also shown in the sent e-mails of my gmail account, but they actually never arrive at my other email address... Does someone know why?
This is my Code
const mailTransport = nodemailer.createTransport({
name: "gmail.com",
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: gmailEmail,
pass: gmailPassword,
clientId: clientid,
clientSecret: clientsecret,
refreshToken: clientrefreshtoken,
accessToken: clientaccesstoken,
expires: 3599,
},
});
exports.submit = functions.https.onRequest((req, res) => {
const corsMiddleware = cors(corsOptions);
corsMiddleware(req, res, () => {
if (req.method !== "POST") {
return;
}
const mailOptions = {
from: req.body.email,
replyTo: req.body.email,
to: "myemail#gmx.at",
subject: `Absender: ${req.body.email}`,
html: req.body.message,
};
mailTransport.sendMail(mailOptions);
res.status(200).send({isEmailSend: true});
});
});
Thanks in Advance
Update!
Somehow i get some e-mails really late, but not all of them. More like 1 out of 10 and i also got this in my Mail inbox:
The recipient server did not accept our requests to connect.
Learn more at https://support.google.com/mail/answer/7720 [mx00.emig.gmx.net. 212.227.15.9: 421-gmx.net (mxgmx017)
Nemesis ESMTP Service not available 421-Service unavailable 421-Reject due to policy restrictions.
421 For explanation visit https://postmaster.gmx.net/en/error-messages?ip=209.85.222.194&c=sp ] [mx01.emig.gmx.net. 212.227.17.5: 421-gmx.net (mxgmx114)
Nemesis ESMTP Service not available 421-Service unavailable 421-Reject due to policy restrictions.
421 For explanation visit https://postmaster.gmx.net/en/error-messages?ip=209.85.222.196&c=sp ]
So after 2 days of trying i think the problem has nothing to do with my code....
Seems to be a problem with gmx servers, maybe i am on any blacklist? i dont know, account is new actually...
Nevermind sending it to my private email from my domain worked.
It is maybe not the reason of your problem, but you should wait that the asynchronous sendMail() execution is completed before sending back the response. If you don't do that you indicate to the Cloud Function platform that it can clean up the instance running your Cloud Function, without waiting for the asynchronous task to be completed.
exports.submit = functions.https.onRequest((req, res) => {
const corsMiddleware = cors(corsOptions);
corsMiddleware(req, res, () => {
if (req.method !== "POST") {
return;
}
const mailOptions = {
from: req.body.email,
replyTo: req.body.email,
to: "myemail#gmx.at",
subject: `Absender: ${req.body.email}`,
html: req.body.message,
};
mailTransport.sendMail(mailOptions)
.then(() => {
res.status(200).send({isEmailSend: true});
})
});
});
I am having contiunual woes with getting nodemailer to send. I have now found the lowest security email account I can to see if I can get the thing working with a view to increasing security once something is getting through. My code is:
app.post("/contact-us", function(req, res){
var mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: 'smtp.123-reg.co.uk',
port: 25,
secure: false, // upgrade later with STARTTLS
auth: {
user: 'enquiries#*********.co.uk',
pass: '**********'
}
});
mailOpts = {
from: "enquiries#*******.co.uk",
to: "**********#mac.com",
subject: 'Website contact form',
text: "test email"
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
console.log(error);
res.render('contact', { title: '********', msg: 'Error occured, message not sent.', err: true, page: 'contact-us' });
}
//Yay!! Email sent
else {
console.log("message sent");
res.render('/');
}
});
});
Neither of the console.logs are coming back, instead the request is met with a timeout & 502 message,
Thanks
Just in case anyone searches for this in the future; the problem was within cloud 9 which wouldn't allow access to the email account to prevent spam.
I keep getting this issue tls.connect is not a function.
I am using nodemailer with my Reactjs and Nextjs application.
sendmail.js?71c0a4a:33 Error: TypeError: tls.connect is not a function
at SMTPConnection.connect (index.js?49b6f52:228)
at getSocket (index.js?55ebb9c:234)
at setImmediate (index.js?55ebb9c:70)
at run (setImmediate.js?d2412a6:40)
at runIfPresent (setImmediate.js?d2412a6:69)
at onGlobalMessage (setImmediate.js?d2412a6:109)
The weird part is if I run the script in the console it will work, but running though the browser I get the tls.connect is not a function error.
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 465,
auth: {
user: '',
pass: ''
},
});
class mail {
options (to, text, subject, html) {
console.log(to)
var mailOptions = {
from: '',
to: to,
subject: subject,
text: text,
html: html
}
return(mailOptions);
}
sendMail(mailOptions) {
console.log(mailOptions)
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.error('Error:', error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
}
}
export default mail;
You're using nodemailer inside a bundler, on Client Side,
NodeMailer should only be used at ServerSide.
This issue is environmental and also be aware that even if that was working, you would be exposing that google account and password by using it at client side.
Consider creating a serverside nodejs api to send your emails
issue