we can send email using nodemailer package in protractor
Sending emails is one of the common tasks in real-life applications, we send emails when something is failed or when something is complete or just for information sake also we may need to send emails to provides updates to other people.
When we are executing test cases in protractor, you may want to send an email to yourself with the status of the execution, or sometimes you may need to send the email to the client of the project to keep them updated.
We will how to send emails in protractor in the next topic, to send an email we may need extra packages in nodejs.
Protractor doesn't have the capability to send emails, but a package called NodeMailer send emails with nodejs. I hope you know that nodejs is pre-requisite to install protractor, so we can use the same package to send emails.
NodeMailer package send emails in nodejs, but the package does not come along with protractor or typescript. So we have to install the nodemailer package for sending emails. use below npm command to install nodemailer.
npm install nodemailer
What does nodemailer do :
Node mailer will send the emails based on the details we provide to the nodemailer in nodejs, below are things run behind the run of nodemailer.
Unfortunately, I was not able to send the mail using a test block or with typescript, so please do follow the below steps to send the email with nodeMailer. Below are the steps to configure nodemailer
var nodemailer = require("nodemailer");
var transport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "[email protected]",
pass: "[email protected]"
}
});
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email', // host address
port: 587, // mostly same number, rarely changes
secure: false, // true for 465, false for other ports
auth: {
user: "username or email, // generated ethereal user
pass: "password" // generated ethereal password
}
});
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Report Result', // Subject line
//text: info.body,
text: 'Contains barcode image', // plaintext body
attachments: [
{
'path': 'D:/bar-code.png',
}]
};
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
response.send(err);
} else {
console.log("Message sent: " + info.response);
response.send(info);
}
node sendmail.js
The Complete code for sending mail in protractor
var nodemailer = require("nodemailer");
var transport = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: "[email protected]",
pass: "[email protected]"
}
});
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Reporest Result', // Subject line
//text: info.body,
text: 'Contains the test result for the smoke test in html file', // plaintext body
attachments: [
{
'path': 'D:/bar-code.png',
}]
};
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
response.send(err);
} else {
console.log("Message sent: " + info.response);
response.send(info);
}
});
The Output of the nodemailer program 
As I said earlier, we cannot write the nodemailer code, and even you write, it not will send any mail, or it will not throw any error.
I can understand your question, then how the hell we automate it ?
Just think this, I said we could not automate nodemailer code because of this we are executing in cmd/terminal. What about automating cmd/terminal command.
I hope you got my point, let's automate the cmd/terminal command for nodemailer.
import { browser} from 'protractor'
describe('Protractor Typescript Demo', function() {
browser.ignoreSynchronization = true; // for non-angular websites
browser.manage().window().maximize()
it('Find Operations', function() {
// set implicit time to 30 seconds
browser.manage().timeouts().implicitlyWait(30000);
browser.get("https://google.com")
var execSync = require('child_process').execSync;
var cmd = "node ./specs/abcc.js";
var options = {
encoding: 'utf8'
};
console.log(execSync(cmd, options));
});
});
the output of automated mail sender
If you don't use options in the output you may see something like this:
browser.get("https://google.com")
var execSync = require('child_process').execSync;
var cmd = "node ./specs/abcc.js";
console.log(execSync(cmd));
Output in buffer format 
SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so it's truly universal.
Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.
We can install nodemailer-smtp-transport package using below npm command, but if you have installed nodemailer with --save option, then we don't have to install this as a separate package.
npm install nodemailer-smtp-transport
We cannot run the nodemailer using the protractor or using conf file, so we have to run from cmd or from terminal only. use below node command to run nodemailer
node target-file.js
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.
I'm receiving the below error while using the above code ? Cucumber HTML report E:MSTWebsite eportshtml/report.html generated successfully ? Error: Greeting never received at SMTPConnection._formatError (E:MSTWebsite ode_modules odemailerlibsmtp-connectionindex.js:784:19) at SMTPConnection._onError (E:MSTWebsite ode_modules odemailerlibsmtp-connectionindex.js:770:20) at Timeout.<anonymous> (E:MSTWebsite ode_modules odemailerlibsmtp-connectionindex.js:704:22) at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7) { code: 'ETIMEDOUT', command: 'CONN' } [13:54:26] E/launcher - BUG: launcher exited with 1 tasks remaining npm ERR! Test failed. See above for more details.