I need to send emails from my application for any exceptions, so I am using nodemailer for that but I cannot provide username and password for the account and without which It is not letting me send any emails. Any solutions? This is how my code looks as of now:
import nodemailer from 'nodemailer';
//Create reusable transport object using the SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secureConnection: false,
requireTLS: true,
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});
module.exports = function sendEmail(to, subject, message) {
let mailOptions = {
from: '********',
to,
subject,
html: message
};
transporter.sendMail(mailOptions, function(error) {
if (error) {
console.log(error);
}
});
};
I don't quite know about nodemailer, but with sendmail, you can send emails without a SMTP server, but for that to work you have to use dkim authentication, for short your email must be sent from the same domain as the email you're trying to send, take a look at this article, hope that helps.
Related
I am working on a small service that allows users to send emails in a similar fashion to mailchimp.
How is mailchimp able to send an email from my account without asking me for my password but by simply sending me a confirmation email?
I was wondering if it was possible to replicate this effect using something like nodemailer?
Previously I wrote some code
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'test#gmail.com',
pass: 'APP_PASS'
}
});
var mailOptions = {
from: 'test2#gmail.com',
to: 'recipent#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I was hoping that the "from" part would say for example "test2#gmail.com" but it didn't, for understandable reason.
How do I prompt a user to giving my app access to sending emails from there account or on behalf of their account and then put it into nodemailer, or if nodemailer can not handle this, what could be a similar approach?
I am using nodemailer to send emails to user with office 365 account email and password are all correct but every time i am getting error - Authentication unsuccessful
Error: Invalid login:Authentication unsuccessful[BL0PR01CA0033.prod.exchangelabs.com]
code: 'EAUTH',
response: '535 5.7.3 Authentication unsuccessful [BL0PR01CA0033.prod.exchangelabs.com]',
response Code: 535,
command: 'AUTH LOGIN'**
You have to enable SMTP login for the O365 mail box or user in the admin settings
Go to Mail Settings
Turn On Authenticated SMTP
once that is done use
var transport = nodemailer.createTransport({
service: "Outlook365",
auth: {
user: 'O365email',
pass: 'O365password'
},
});
var mailOptions = {
from: 'o365email',
to: 'exaple#gmail.com', // list of receivers
subject: "Password reset requested for your account", // Subject line
text: 'reset password',
html: "<h1>Mail Testing</h1>" // html body
};
transport.sendMail(mailOptions, function(error, response){
if(error){
resp.status(500);
resp.send(error);
}else{
resp.send({message:'done'});
}
});
I have faced the same problem and after a lot of research and following few document. I came to this solution that
As an Office365 (now Microsoft365) customer I need to go to portal.azure.com, then "Manage", "Properties" and set "Manage Security defaults" to "no". I think this is messed up, but it works.
https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/concept-fundamentals-security-defaults
https://github.com/nodemailer/nodemailer/issues/1071
Here is the setting what I am using
host: "domain.com",
port: 587,
secure: false,
auth: {
user: "email#domain.com",
pass: "password"
},
tls: { ciphers: 'SSLv3' },
service: "Outlook365",
The easy way to fix this is here: https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission#use-the-microsoft-365-admin-center-to-enable-or-disable-smtp-auth-on-specific-mailboxes
Under the title: Use the Microsoft 365 admin center to enable or disable SMTP AUTH on specific mailboxes
You need to enable SMTP AUTH for the email you are adding creds for.
In my case, I logged on to my outlook email account and changed the POP settings to let devices and apps use POP, I also verified my account.
Everything worked well afterward.
I am trying to make a contact page with react and I'm struggling with sending the e-mail part.
I'm trying to use nodemailer, and my code for that is:
var nodemailer = require('nodemailer');
var xoauth2=require('xoauth2');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
xoauth2:xoauth2.createXOAuth2Generator({
user: 'mymail#gmail.com',
clientId: '',
clientSecret: '',
refreshToken:''
})
}
});
var mailOptions = {
from: 'Name <mymail#gmail.com>',
to: 'mymail#gmail.com',
subject: 'Sending Email to test Node.js nodemailer',
text: 'That was easy to test!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent');
}
});
I have put the clientId, clientSecret and refreshToken from google API and oauth2 playground and enabled the non secure apps thing. But when I'm trying to send the e-mail I get
TypeError: net.isIP is not a function
EDIT: I have tried adding after service: 'gmail'
type: 'SMTP',
host: 'smtp.gmail.com',
Still not working
I was facing the same issue when trying to implement the nodemailer code client side. The reason was because nodemailer doesn't seem to work in the browser (only in node). Moving it serverside (into an express app) solved the issue.
This post regarding the same error (but affecting a different library) also suggests that running the files in the browser is the problem: http://www.ganzhoupress.com/github_/cypress-io/cypress/issues/1981
Hope this helps.
replace ==>
var nodemailer = require('nodemailer');
with this ==>
var nodemailer = window.require('nodemailer');
this will solve your problem.
I am using nodemailer module to send mail from my nodejs application.
I am getting Error: connect ETIMEDOUT xxx.xxx.xx.xxx:465. Can any one help me in solving this.
Here I am pasting my code.
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'my_mail_id#gmail.com',
pass: 'my_gmail_password'
}
});
console.log('created');
transporter.sendMail({
from: 'my_mail_id#gmail.com',
to: 'my_mail_id#gmail.com',
subject: 'hello world!',
text: 'hello world!'
});
This may be firewall problem. I faced similar problem in Ubuntu (Digital Ocean server). Tried to fix the issue for 3 days, tried using auth2 also, tried with inactive firewall using ufw inactive command, but no luck. Finally I checked Digital Ocean admin panel and created firewall for the droplet. Problem solved by enabling TCP inbound and outbound in firewall settings.
Have you looked at this answer.
It turns out that in order for Google to authorize a third party server to access your account via SMTP now, you have to enable “Less Secure Apps” on your gmail account, if you want to use username/password (more info here).
So you have two option:
use OAuth
make your account less secure
// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
service: 'Hotmail',
auth: {
user: "username",
pass: "paasweord"
}
});
console.log('SMTP Configured');
// Message object
var message = {
// sender info
from: 'abc#hotmail.com',
// Comma separated list of recipients
to: req.query.to //'aadityashukla9#hotmail.com',
// Subject of the message
subject:req.query.subject //'Nodemailer is unicode friendly ✔',
// plaintext body
text: req.query.text //'Hello to myself!',
// HTML body
/* html:'<p><b>Hello</b> to myself <img src="cid:note#node"/></p>'+
'<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'*/
};
console.log('Sending Mail');
transport.sendMail(message, function(error){
if(error){
console.log('Error occured');
console.log(error.message);
return;
}
console.log('Message sent successfully!');
//transport.close(); // close the connection pool
});
I experienced this same issue today, found this documentation...
https://nodemailer.com/usage/using-gmail/
Had to do a capcha process from the server, by visiting a url while logged into gmail.
Hopefully it helps others.
There are the only reasons of this error:
Less Secure Apps: you have to Enable the "Less Secure Apps" from your Gmail account.
Use OAuth
Besides the already mentioned reference to the information at https://nodemailer.com/usage/using-gmail/, in my case the Internet Router (Speedport W724V) was still a problem. This keeps a list of all allowed SMTP servers. After I had extended the list accordingly, it worked perfectly. I had to do the same with smtp.ethereal.email.
I'm not sure if should be posting this answer but I've faced the same problem while using GMAIL and the reason behind the error for me was being connected to a vpn. I disabled it and now it works.
I'm using an application password
open port inbound outbound rule 587 or others, whichever you are using on server aws/google etc.
host: host,
secureConnection: false,
port: 465,
secure: true,
auth: {
user: user,
pass: pass
}
I'm trying few days to composing mail sending with node.js to gmail with no success :-(
at the beginning of tries I try to send form submittion to my gmail but first I need to understand how do I sent simple mail from body to gmail,
perhaps I wrong with syntax or missing something?
actually I uses "heroku" as storage and domain for my site
I tried several plugins (such as mailgun, send grid and more) but the process was too complicated integrate their API's into my site.
actually,
I find this article in stack overflow that describe how to send the via nodemailer in relation to my error - URL: node.js nodemailer gmail error
when i copy the answer1 to my code i still receiving error.
I' also pass trough all gmail setup how to turn off security block for less secured apps but i'm still receiving error.
all the requires installed over my npm
npm install --save
** code **
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
function handleSayHello(req, res) {
var transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: 'myGmail#gmail.com', // my mail
pass: 'myPassword'
}
}));
var mailOptions = {
from: 'myGmail#gmail.com', // sender address
to: 'myGmail#gmail.com', // list of receivers
subject: 'Email Example' // Subject line
//text: text //, // plaintext body
//html: '<b>Hello world ?</b>' // You can choose to send an HTML body instead
};
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.json({
yo: 'error',
err: error
});
} else {
console.log('Message sent: ' + info.response);
res.json({
yo: info.response,
info: info
});
};
});
}
router.get('/', function (req, res, next) {
handleSayHello(req, res);
});
then i receive this error:
{
"yo": "error",
"err": {
"code": "EAUTH",
"response": "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtx\n534-5.7.14 e8jnXXDaDg-dpFfc3H5ljeaBdmnxbXBDXMh-aXS-mmV4gQsXMSZLiiJHDXuOr3wy-IR2Zp\n534-5.7.14 xnznWLf5y5e3xTbJNlZBNcxBMajM9-SFQGy1MQM2XZRpHgtywuDtEj5iPiP0b2T6Wjbsxg\n534-5.7.14 hgehfzufG6h13qhjQK5IgRPNQsSICRQBtRCl3E1J62wFo8bnvZv4peY5aK55JMpwhSavJb\n534-5.7.14 ho-b9ExGGsXFmw_Er6lc8m3vCmO_Q> Please log in via your web browser and\n534-5.7.14 then try again.\n534-5.7.14 Learn more at\n534 5.7.14 https://support.google.com/mail/answer/78754 t35sm887733qtc.40 - gsmtp",
"responseCode": 534,
"command": "AUTH PLAIN"
}
}
if someone can take my code and fix it (if needed) and explain simply what do i need to modify in my google account I'll be great-full so much!
to allow from google/gmail, you should follow these links and allow to less secure apps access:
unlock account
and
Less secure apps
Let me know if still have issue, I'll check/debug your code.