how to send an email in nodejs - node.js

I read the following, Sending emails in Node.js? but I'm looking for a way to send an email, not through an smtp server. As in the linux envirement you have different options such as sendmail and others
I could ofc use the environment I'm in to make use of the already existing functionality, but I would be interested to learn how one would dispatch the email using only js, if even possible..
I set up an smtp server using the smtp module: https://github.com/andris9/smtp-server why I'm interested in the delivery part of a server I already setup.

Take a look at node-mailer. You can set it up without smtp server. https://github.com/nodemailer/nodemailer

var nodemailer = require('nodemailer');
var send = require('gmail-send');
var mailserverifo = nodemailer.createTransport({
service: 'gmail',
host : "smtp.gmail.com",
port : "465",
ssl : true,
auth: {
user: 'email#gmail.com',
pass: 'password#'
}
});
var Mailinfo = {
from: 'email#gmail.com',
to: 'email#info.com',
subject: 'Testing email from node js server',
text: 'That was easy!'
};
mailserverifo.sendMail(Mailinfo, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email Send Success: ' + info.response);
}
});
Enable less secure app form setting -
https://www.google.com/settings/security/lesssecureapps
Disable Captcha -
https://accounts.google.com/b/0/displayunlockcaptcha

You can use sendmail in Node js. I'v using it and it's working fine for me.
npm install sendmail --save
const sendmail = require('sendmail')();
sendmail({
from: 'no-reply#yourdomain.com',
to: 'test#qq.com, test#sohu.com, test#163.com ',
subject: 'test sendmail',
html: 'Mail of test sendmail ',
}, function(err, reply) {
console.log(err && err.stack);
});
https://www.npmjs.com/package/sendmail

First:Install nodemailernpm install nodemailer
Then put this into your node file:
var nodemailer = require('nodemailer');
var http = require('http');
var url = require('url');
console.log("Creating Transport")
var transporter = nodemailer.createTransport({
service:'Hotmail',
auth: {
user:'salace2008765#outlook.com',
pass: 'alice123#'
}
});
var mailOptions = {
from:'salace2008765#outlook.com',
to: 'jerome20090101#gmail.com',
subject: 'This is a test: test',
text:'TgK'
}
console.log("Sending mail")
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response)
}
})
It usally works
Sources:W3Schools and Nodemailer's official site

Related

How to send email using angular 9

The following code send email from server side. How to I use that code in Angular 9 application.
Could you please help some one for this.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'test#test.com',
pass: 'xxxx'
}
});
var mailOptions = {
from: 'test#test.com',
to: 'test#test.com',
subject: 'Sending Email using Node.js',
text: `Hi, thank you for your nice Node.js Email.`
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Probably, the above code will works i the Angular application, but the big problem doing that, is you will have exposed mail transport credentials in your front app. As mentioned before, best solution is create a simple API that send the email on server side.

Sending email from a non existing mail id (noreply#myserver.com) in Nodejs using nodemailer npm

Below is the sample code am using for sending mail.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
auth: {
user: 'me#myserver.com',
pass: 'mypassword'
}
});
var mailOptions = {
from: 'no-reply#myserver.com', //It will work if i give me#myserver.com but i need no-reply#myserver.com in from option.
to: 'someuser#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 will give me below error if i use no-reply#myserver.com in from option.
Error: Message failed: 554 5.2.0
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;
Failed to process message due to a permanent exception with message
Cannot submit message.
Some email servers do not accept changing FROM address. This is not about nodemailer. You need to check your email server configuration.

can't send the mail using node.js

I cannot able to send the mail using nodejs program I used node-mailer and nodemailer smtp transport module.This program worked perfectly lastweek.but suddenly now it is not working .I tried but i cannot able to find the error.can anyone help me .Thankyou.
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port: 465,
auth: {
user: 'sampleprogrammers#gmail.com',
pass: 'ashbdhbedbudu.'
}
}));
transporter.sendMail({
from: "sampleprogrammers#gmail.com",
subject:" hello ji " ,
text: "I would like to write dialogue",
attachments:[
{
'filename':'link.txt',
'path': 'E:/STUDIES/CORE SUBJECTS/link.txt'
}
],
to: "vikirockz456#gmail.com"
}, function(error, info) {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
console.log("Mail sent successfully");
});
SEE THE ERROR BELOW:
Can you please go through this question : Nodemailer Error Can't Fix
Maybe your version of node changed knowingly or unknowingly. It seems your version of node is not compatible with nodemailer.

How to make nodemailer with SMTP work?

I've already lowered my gmail account's security here:
https://myaccount.google.com/lesssecureapps
I've tried to send mail with, and without smtp, but always get some kind of error.
Error: invalid login: 535.5.....
I checked my "user", and "from" parameters are the same. Here's a bit of my code. I'm working on localhost. May it be the problem?
var nodemailer = require("nodemailer");
var smtpTransport = require("nodemailer-smtp-transport");
var transporter = nodemailer.createTransport(smtpTransport({
service: "gmail",
auth: {
user: "seratothdevelop#gmail.com", // my mail
pass: "*********"
}
}));
console.log('SMTP Configured');
var message = "<b>Kedves Felhasználó!</b>"+
"<p>Sikeresen feliratkoztál a Kutyapplikáció hírlevelére, ahonnan friss információkkal látunk el hétről-hétre.</p>"+
"<br><p>Üdvözlettel, </p><p><i>sethdevelop</i></p>";
var mailOptions = {
from: "seratothdevelop#gmail.com", // sender address
to: request.body.subscribeduser, // list of receivers
subject: "Kutyapp feliratkozás", // Subject line
html: message // You can choose to send an HTML body instead
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Üzenet elküldve: ' + info.response);
}
transporter.close();
});

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