How to set up Nodemailer with Angular 4? - node.js

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?

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);

How can I create a custom smtp server to send out notification emails in Nodejs?

My requirement is to send a notification email from my application to any email id , eg: a gmail address. I went through some modules like the smtp-server ,smtp-connection and emailjs
This is what I have got till now.
var SMTPServer = require('smtp-server').SMTPServer
var server = new SMTPServer({
name: 'testDomain.com',
authOptional: true,
onAuth: function (auth, session, callback) {
callback(null, {user: 'sample-user'})
}
})
server.on('error', function (err) {
console.log('Error %s', err.message)
})
var port = 1234
server.listen(port, function () {
console.log('SERVER: Listening on port: ' + port)
var opts = {
host: '127.0.0.1',
port: port,
username: 'testUser',
password: 'testUser123',
to: 'someUser#gmail.com'
}
sendEmail(opts,function (err, message) {
server.close()
})
})
where sendEmail is a function using emailjs.
function sendEmail(opts,callback) {
var server = email.server.connect({
user: opts.username || '',
password: opts.password || '',
host: opts.host,
ssl: false
})
server.send({
text: 'i hope this works',
from: 'you <'+opts.username+'#testDomain.com>',
to: ' <'+opts.to+'>',
subject: 'testing emailjs'
}, function (err, message) {
console.log(err || message);
callback(err, message)
})
}
But it seems that the client is not able to connect to the server. It is hanging.
I tried smtp-connection like this initially:
var connection = new SMTPConnection({
port: port,
host: '127.0.0.1',
ignoreTLS: true
})
connection.connect(function () {
var envelope = {
from: opts.username+'#testDomain.com',
to: opts.to
}
var message = "Hello!!!"
connection.send(envelope, message, function(err,message){
callback(err,message)
connection.quit()
})
This seems to work but gives this output
response: '250 OK: message queued'
the smtp-connection documentation says it only queues the messages doesnt deliver it to the recipient.
How can I achieve my requirement? I am attempting to send the notification from a custom mail server because I want to avoid adding the user credentials of an email account in the code in plaintext. I am looking for a simple mailserver which can be spun up when the notification needs to be sent and then shut down.
Am I completely offtrack, not understanding how mail servers work?? Please give some feedback and a best approach to solve this.
Just my opinion but I think its better to take a separate mail server.
like the example from nodemailer:
var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass#smtp.gmail.com');
// setup e-mail data with unicode symbols
var 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 ?', // plaintext body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
For the security:
You can use separate file for storing the username / password.
Use can use a Token based authentication. So you don't need to save the password. An example of this is OAuth. Instead of the password you authenticate with a token. This token u get from the mailserver provider (like gmail).
An example use of oauth and nodemailer you can find here.

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.

sending emails in node js

After watching this video http://vimeo.com/26963384 on vimeo which was about how kue works,i have to ask how the code worked without installing any package to help send emails like node mailer.
Does the latest version of node js come with the capability to send emails?.
The code used looks like
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj#learnboost.com'
, template: 'welcome-email'
}).save();
In the presentation,no package to send emails was added.
var nodemailer = require('nodemailer');
// create SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'xxx#gmail.com',
pass: '******'
}
});
// transporter object for all e-mails
var mail = {
from: 'XXX XXXX <XXX#gmail.com>', // sender address
to: 'XXX#hotmail.com, XXX#gmail.com', // list of receivers
subject: 'Hello ', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mail, function (error, info) {
if (error) {
return console.log('Error : ' + error);
}
console.log('Mail sent: ' + info.response);
});

Resources