Send email in Node.js with nodemailer - node.js

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.

Related

nodemailer - 454 4.7.0 Too many login attempts, please try again later. j13-20020a170903024d00b0016d1b70872asm9193619plh.134 - gsmtp

I am using Nodemailer. Actually, I was using in Strapi app. I already went through many forum answers for this. Even though I have done many things I am getting this error repeatedly.
454 4.7.0 Too many login attempts, please try again later. j13 20020a170903024d00b0016d1b70872asm9193619plh.134 - gsmtp
It's happening only in my corporate email. Google(Gmail) Domain is the email provider of my corporate email. But this error is not there when I try with my Personal Gmail.
"use strict";
const nodemailer = require("nodemailer");
// 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();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: 'support#mycorporate.com', // working fine with my personal email
pass: '<App password>',
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <example#gmail.com>', // sender address
to: "getting#mycopr2.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
Earlier, when I try this with no-reply#mycorporate.com, I was getting the same error. That's why I created a new email as support#mycorporate.com and tried this new one. Initially, it was working fine with support#mycorporate.com. But again I am getting the same error for the newly created email too.
I really can't understand why it is happening only in the corporate emails.

How to send an auto reply email with a html page as email in NodeJS

I recently started programming in NodeJS. I can't find any way how can I send an auto-reply email. When another person sends me an email, in that time an auto-reply email will be sent to his/her email, which is in HTML email format.
How can I do this?
First you need to poll or monitor your account for the incoming mail, using a generic smtp or imap client, such as https://www.npmjs.com/package/imap, or even better, a module that actually monitors new email, such as https://www.npmjs.com/package/mail-notifier
The posted example is
Start listening new mails :
const notifier = require('mail-notifier');
const imap = {
user: "yourimapuser",
password: "yourimappassword",
host: "imap.host.com",
port: 993, // imap port
tls: true,// use secure connection
tlsOptions: { rejectUnauthorized: false }
};
notifier(imap)
.on('mail', mail => console.log(mail))
.start();
Keep it running forever :
const n = notifier(imap);
n.on('end', () => n.start()) // session closed
.on('mail', mail => console.log(mail.from[0].address, mail.subject))
.start();
Next, its hard to beat nodemailer to send your response.
https://nodemailer.com/about/
Here is their example. (Half this code is comments and logs)
"use strict";
const nodemailer = require("nodemailer");
// 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();
// 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: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <foo#example.com>', // sender address
to: "bar#example.com, baz#example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);

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.

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?

Nodemailer does'nt work properly

The email comes from my e-mail address rather than the sender. Any ideas?
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'mail',
pass: 'pass'
}
});
var mailOptions = {
from: req.body.email , // sender address
to: 'mail', // list of receivers
subject: ' ', // Subject line
text: req.body.message,
html: '<p>'+req.body.message+'</p>'// plain text body
};
This is how Gmail works. It won't let you to send emails as anyone but you, for security reasons.
If you need more flexibility then you should use a transactional email service like Mailgun, Mandrill or SendGrid - which you can easily use with Nodemailer (there are Nodemailer transports available for them, just like for Gmail).
See: https://www.npmjs.com/browse/keyword/nodemailer

Resources