Changes in JS files are not reflecting in Node JS Application - node.js

I'm new in node js and
I'm facing a very confusing problem in my NODE JS express application. I created a contact form and I'm using the Amazone Ubuntu server (aws) to host my application.
Previously, I have another domain name now I have different. I changed all the emails in JS file to send an email on the contact form submit. but my bad luck email still going through old emails even I changed.
here is my code
fromEmail="myfromemail#domain.com";
emailto="mytoemail#domain.com";
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp info',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: '', // generated ethereal user
pass: '' // generated ethereal password
}
});
// setup email data with unicode symbols
let mailOptions = {
from: 'Domain Name <myfromemail#domain.com >', // sender address
to: emailto, // list of receivers
subject: 'Test', // Subject line
html:"<h1>Test Body</h1>",
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
});
guide me how I can solve this issue. If this is due to cache please let me to how I can clear it.

I solved my problem by restart app.js on the server.

You can use Nodemon, a launcher that watches for changes in code and auto-restart the application.

Related

this is my nodemailer program Even if i give wrong mail in TO addrress also it is showing message sent how to rectifyy that

const nodemailer = require('nodemailer');
let mailTransporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: '*************',
pass: '*********',
},
});
let mailDetails = {
from: '********',
to: 'uuwdwuvw#', //guguygugiug
subject: 'Test mail',
//text: 'redeem your gift',
html: 'redeem your coupon code ',
};
mailTransporter.sendMail(mailDetails, function (err, data) {
if (err) {
console.log('Error Occurs');
} else {
console.log('Email sent successfully');
}
});
this is my nodemailer program Even if i give wrong mail in TO addrress also it is showing message sent how to rectifyy that
Nodemailer itself can't tell if an email has been delivered or not. It can tell you only if the email has been sent. There's a difference between email being sent and being delivered. Detecting bounced emails is out of scope for the Nodemailer.
In order to do that you need to implement your own bounced email mechanism, probably with your own SMTP server. It's not an easy thing to do, so you should probably use some real email provider that has this functionality instead of Gmail (Google likes to block such applications).
Have a look at the similar question I've found on GitHub and on SO.

Nodemailer taking too much time to send an e-mail

I'm building a notification server, that sends emails.
I'm using the node mailer library, but when I try to send an e-mail, it takes almost 5 minutes to receive the content in my inbox
I already searched why this happens and the only answer I found was to response content to tell the browser that the request has finished. Probably something like that: res.send(xxx).
But I'm using sockets to send e-mails automatically, what do I need to improve my server? My code:
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '...',
pass: '...'
}
});
...
io.on('connection', function(socket){
socket.on('notify', function(object) {
var mailOptions = {
from: '...',
to: object.message.sendTo,
subject: object.message.subject,
html: object.template
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
socket.emit("error", {data : "Error!"})
} else {
socket.emit("sent", {data : "Email successfully sent!"})
}
});
});
});
What do I need to change on my code?
Check the headers of the emails when you receive them to figure out where the email is getting "stuck". It might be in the original server, but it might be farther along (e.g., a spam filter or a forwarder or just a server somewhere in the middle). If it is anywhere except the first hop then there may be nothing you can do in your code to actually speed things up.

How to set up Nodemailer with Angular 4?

I can't find a tutorial anywhere that guides me through the process of setting up nodemailer in Angular 4. I'm not even sure in which file to put the ts from the nodemailer intro on their website:
'use strict';
const nodemailer = require('nodemailer');
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, account) => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Fred Foo ๐Ÿ‘ป" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello โœ”', // Subject line
text: 'Hello world?', // plain text body
html: '<b>Hello world?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#blurdybloop.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
This is a complicated thing and any help would be appreciated! If I knew where to put the code and what it should do, I would be able to piece some things together.
You are confusing client side with server side code. NodeMailer is a server side package for express. Angular 4 would call an API to executed that code.
If you want to to get nodemailer up and running, you need to do so within node.
Are you using a back-end such as express?

Sending email via Node.js using nodemailer is not working

I've set up a basic NodeJS server (using the nodemailer module) locally (http://localhost:8080) just so that I can test whether the server can actually send out emails.
If I understand the SMTP option correctly (please correct me if I'm wrong), I can either try to send out an email from my server to someone's email account directly, or I can send the email, still using Node.js, but via an actual email account (in this case my personal Gmail account), i.e using SMTP. This option requires me to login into that acount remotely via NodeJS.
So in the server below I'm actually trying to use NodeJs to send an email from my personal email account to my personal email account.
Here's my simple server :
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport("SMTP", {
service: 'Gmail',
auth: {
user: '*my personal Gmail address*',
pass: '*my personal Gmail password*'
}
});
var http = require('http');
var httpServer = http.createServer(function (request, response)
{
transporter.sendMail({
from: '*my personal Gmail address*',
to: '*my personal Gmail address*',
subject: 'hello world!',
text: 'hello world!'
});
}).listen(8080);
However, it's not working. I got an email by Google saying :
Google Account: sign-in attempt blocked
If this was you
You can switch to an app made by Google such as Gmail to access your account (recommended) or change
your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no
longer protected by modern security standards.
I couldn't find a solution for the above problem on the nodemailer GitHub page. Does anyone have a solution/suggestion ?
Thanks! :-)
The answer is in the message from google.
Go to : https://www.google.com/settings/security/lesssecureapps
set the Access for less secure apps setting to Enable
For the second part of the problem, and in response to
I'm actually simply following the steps from the nodemailer github page so there are no errors in my code
I will refer you to the nodemailer github page, and this piece of code :
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user#gmail.com',
pass: 'userpass'
}
});
It differs slightly from your code, in the fact that you have : nodemailer.createTransport("SMTP".
Remove the SMTP parameter and it works (just tested). Also, why encapsulating it in a http server? the following works :
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'xxx',
pass: 'xxx'
}
});
console.log('created');
transporter.sendMail({
from: 'xxx#gmail.com',
to: 'xxx#gmail.com',
subject: 'hello world!',
text: 'hello world!'
});
Outdated: refreshToken and accessToken no longer exist in JSON file output
For those who actually want to use OAuth2 / don't want to make the app "less secure", you can achieve this by
Search "Gmail API" from the google API console and click "Enable"
Follow the steps at https://developers.google.com/gmail/api/quickstart/nodejs. In the quickstart.js file, changing the SCOPES var from ['https://www.googleapis.com/auth/gmail.readonly'] to ['https://mail.google.com/'] in the quickstart js file provided as suggested in troubleshooting at https://nodemailer.com/smtp/oauth2/
After following the steps in (2), the generated JSON file will contain the acessToken, refreshToken, and expires attributes needed in the OAuth2 Examples for Nodemailer
This way you can use OAuth2 authentication like the following
let transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
type: 'OAuth2',
user: 'user#example.com',
clientId: '000000000000-xxx0.apps.googleusercontent.com',
clientSecret: 'XxxxxXXxX0xxxxxxxx0XXxX0',
refreshToken: '1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx',
accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x',
expires: 1484314697598
}
});
instead of storing your gmail password in plaintext and downgrading the security on your account.
i just set my domain to: smtp.gmail.com and it works. I am using a VPS Vultr.
the code:
const nodemailer = require('nodemailer');
const ejs = require('ejs');
const fs = require('fs');
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: 'xxx#gmail.com',
pass: 'xxx'
}
});
let mailOptions = {
from: '"xxx" <xxx#gmail.com>',
to: 'yyy#gmail.com',
subject: 'Teste Templete โœ”',
html: ejs.render( fs.readFileSync('e-mail.ejs', 'utf-8') , {mensagem: 'olรก, funciona'})
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
my ejs template (e-mail.ejs):
<html>
<body>
<span>Esse รฉ um templete teste</span>
<p> gerando com o EJS - <%=mensagem%> </p>
</body>
</html>
Make sure:
install ejs: npm install ejs --save
install nodemailer: npm install nodemailer --save
ping to smtp.gmail.com works: ping smtp.gmail.com
change xxx#gmail.com to your gmail email
change yyy#gmail.com to the email that you want to send a email
Enable less secure apps
Disable Captcha temporarily
have a nice day ;)
While the above answers do work, I'd like to point out that you can decrease security from Gmail by the following TWO steps.
STEP #1
Google Account: sign-in attempt blocked If this was you You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.
STEP #2
In addition to enabling Allow less secure apps, you might also need to navigate to https://accounts.google.com/DisplayUnlockCaptcha and click continue.
You only need App password for google auth, then replace your google password in your code.
go here https://myaccount.google.com/apppasswords
sample code:
const nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: 'example#gmail.com',
pass: 'app password here'
}
});
transporter.sendMail(option, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
For debugging purpose it is handy to implement a callback function (they never do on the nodemailer github page) which shows the error message (if there is one).
transporter.sendMail({
from: from,
to: to,
subject: subject,
html: text
}, function(err){
if(err)
console.log(err);
})
It helped me solve my problem... Turns out newer versions are not working properly:
"Looks like nodemailer 1.0 has breaking changes so 0.7 must be used instead: http://www.nodemailer.com/
Message posted on nodemailer as of 12/17/15:
Do not upgrade Nodemailer from 0.7 or lower to 1.0 as there are breaking changes. You can continue to use the 0.7 branch as long as you like. See the documentation for 0.7 here."
I found this answer here
And install smtp module as dependency:
npm install smtp
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
type: "SMTP",
host: "smtp.gmail.com",
secure: true,
auth: {
user: 'writeYourGmailId#gmail.com',
pass: 'YourGmailPassword'
}
});
var mailOptions = {
from: 'xyz.khan704#gmail.com',
to: 'azran.khan704#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');
}
});
Go to https://myaccount.google.com/lesssecureapps
and change it ON because
Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks.
You should not use gmail password for it anymore!
Recently google has provided a new method to use in 3rd party apps or APIs. You need to use App Password instead of the gmail password. But for creating it, you need to enable 2-step Authentication mode in your google account:
You can find steps here: https://support.google.com/mail/answer/185833?hl=en
try this code its work for me.
var http = require('http');
var nodemailer = require('nodemailer');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var fromEmail = 'akarthi#xyz.com';
var toEmail = 'akarthi#xyz.com';
var transporter = nodemailer.createTransport({
host: 'domain',
port: 587,
secure: false, // use SSL
debug: true,
auth: {
user: 'fromEmail#xyz.com',
pass: 'userpassword'
}
});
transporter.sendMail({
from: fromEmail,
to: toEmail,
subject: 'Regarding forget password request',
text: 'This is forget password response from your app',
html: '<p>Your password is <b>sample</b></p>'
}, function(error, response){
if(error){
console.log('Failed in sending mail');
console.dir({success: false, existing: false, sendError: true});
console.dir(error);
res.end('Failed in sending mail');
}else{
console.log('Successful in sending email');
console.dir({success: true, existing: false, sendError: false});
console.dir(response);
res.end('Successful in sending email');
}
});
}).listen(8000);
console.log('Server listening on port 8000');
response:
Successful in sending email
{ success: true, existing: false, sendError: false }
{ accepted: [ 'akarthi#xyz.com' ],
rejected: [],
response: '250 2.0.0 uAMACW39058154 Message accepted for delivery',
envelope:
{ from: 'akarthi#xyz.com',
to: [ 'akarthi#xyz.com' ] },
messageId: '1479809555147-33de4987-29d605fa-6ee150f1#xyz.com' }
Adding to xShirase answer just providing screenshots where to enable. Also confirm in security that previous attempt was from you.
Xshirase deserves all upvotes.Iam just showing screenshot.
Here is best way send email using gmail
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '******#gmail.com',
pass: '**********',
},
});
use two authentication from google => security => app password and do fill some stuff get app password

Send email in Node.js with nodemailer

My target is to be able to send email without setting credentials. For this purpose I've picked up the nodemailer module. Here is my code:
var nodemailer = require('nodemailer');
var message = {
from: "test#gmail.com",
to: "test1#gmail.com",
subject: "Hello โœ”",
text: "Hello world โœ”",
html: "<b>Hello world โœ”</b>"
};
nodemailer.mail(message);
According to documentation the "direct" transport method should be used (actually I don't know nothing about transport methods at all). But unfortunately this method is absolutely unstable - sometimes it works sometimes it doesn't.
Could anyone shed some light on it? How can I send email without configuring SMTP transport credentials?
Sure, but it totally depends on server's configuration. Most email servers will not work unless you use authentication. It is 2017 ๐Ÿ™‚
Well, AFAIK nodemailer detects the correct configuration based on the email domain, in your example you have not set the transporter object so it uses the default port 25 configured. To change the port specify in options the type. And I highly recommend you to specify it explicitly.
Probably windows firewall or antivirus preventing outgoing access. Try to get debug/error messages. We need something to help you more.
Here is a new version of the nodemailer and here is an example how to use it:
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 465,
secure: true, // secure:true for port 465, secure:false for port 587
auth: {
user: 'username#example.com',
pass: 'userpass'
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Fred Foo ๐Ÿ‘ป" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello โœ”', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
I hope it helps.

Resources