unable to send html text using nodemailer - javascript

I am unable to send html text in mail using nodemailer.
exports.send = function(req, res) {
console.log(req.query);
var mailOptions = {
to: req.query.email,
subject: req.query.sub,
text: 'Date of Interview: ' + req.query.dateOfInterview+ 'Time of Interview: ' + req.query.timeOfInterview + '' + req.query.assignedTechnicalPerson + '' + req.query.typeOfInterview + '' + req.query.interviewLocation
}
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
console.log(error);
res.end("error");
} else {
console.log("Message sent: " + response.message);
res.end("sent");
}
});
};
I am getting mail as continuous text without any line space
How can i send the same text using html tags in it i have also tried keeping html and end up getting lots of errors
Please say me correct syntax
Any help is appreciated

Here is the working code with nodemailer latest version.
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodeMailer.createTransport(
smtpTransport({
service: 'gmail',
auth: {
user: <Your gmail>,
pass: '*****'//ur password
}
})
);
transporter.sendMail({
from: 'sender#gmail.com',
to: "recipient#mail.id",
subject: 'hello world!',
//text:"one"
html: '<html><body>Hello World....</body></html>'
}, function(error, response) {
if (error) {
console.log(error);
} else {
console.log('Message sent');
}
});
Note: To give access for smtp do the following:
For Gmail you may need to configure "Allow Less Secure Apps" in your
Gmail account. Click here
You also may need to unlock your account with "Allow access to your
Google account" to use SMTP.
If you are using 2FA in that case you would have to create an
Application specific password.

Related

Email sent but not recieved, I'm using nodemailer

I'm using nodemailer v^6.4.17 and I'm having trouble getting it to send emails. I do not get the issue cause in the console, it is showing that the emails has been sent. But when I go to client side and check inbox, I don't see the email.
let transporter = nodemailer.createTransport({
name: 'www.example.com',
host: '******************',
port: 465,
secure: true,
auth: {
user: ''sender#email.com'',
pass: '**********',
},
});
let info = await transporter.sendMail({
from: 'sender#email.com',
to: "reciever#email.com",
subject: "Hi",
text: "Hello world!",
html: '<h1>2021</h1>',
});
What I'm I missing here? Thanks.
Make sure you enable the less secure app in your Gmail account.
enable your Gmail account less secure app
Also, make sure to check email in spam emails.
Try doing it like this -
var mailOptions = {
from: "----------",
to: "-------",
subject: "------------",
text: "------------",
html: "-----------"
};
smtpTransport.sendMail(mailOptions, function (error, response) {
if (error) {
console.log(error);
console.log("Cannot connect to SMTP server right now");
} else {
console.log("Success" + response);
}
});

Node Mailer does not work even though it says its successful

I have question about the nodemailer that I am working on. The result returns email sent,
but I do not get any email. I am sure I am doing something wrong and I have no idea what that is.
I will post my code below.
let transporter = nodemailer.createTransport({
//pool: true,
host: "*****.net",
port: ****,
secure: false,
auth: {
user: "****.com",
pass: "*****",
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
});
var mailOptions = {
from: '"xxx"<info#****.com>;', // sender address
to: result.recordset[0].toEmail, // list of receivers
subject: "NEW USER", // Subject line
html: result.recordset[0].content, // plain text body
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
AND THIS IS THE MESSAGE I GET FROM THE TERMINAL BELOW
Server is ready to take our messages
Email sent: 250 uPMSkJg8an7bs mail accepted for delivery
If you are using for example a cheap mail service, mails might be sent after some time. I had that problem before. Services use queue system and prioritize the mail requests based on the pricing. Other than that, you can check your spam folder just in case if that might be a problem.

TypeError - invalid login

I wrote some code to implement nodemailer in my nodejs application to send mails.
i wrote this code:
var express = require("express");
var router = express.Router();
var nodemailer = require("nodemailer");
/* GET contact page. */
router.get("/", function (req, res, next) {
res.render("contact", { title: "Contact" });
});
router.post("/send", function (req, res, next) {
var transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "MyEmailHere",
pass: "password",
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: "MyEmailHere",
to: "myOtherEmailId",
subject: "Website Submission",
text:
"You have a new submission with the following details...Name: " +
req.body.name +
" Email: " +
req.body.email +
" Message: " +
req.body.message,
html:
"<p> You got a new submission with the following details...</p><ul></ul><li>Name: " +
req.body.name +
"</li><li>Email: " +
req.body.email +
"</li><li>Message: " +
req.body.message +
"</li></ul>",
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.redirect("/");
} else {
console.log("Message Sent: " + info.response);
}
});
});
module.exports = router;
But I get an error saying: "Username and password not accepted." Am i supposed to use my real password? Does nodemailer also support Yahoomail besides Gmail? I am still learning and in dev mode by the way. A little help?
It worked flawlessly when I entered my own information correctly and turned on 'less secure apps', please try it this way
NOTE (critical): make sure the 'less secure apps' option is turned on
(If you don't know how to open, you can visit here)
NOTE: Make sure 2-step verification is turned off
var express = require("express");
var router = express.Router();
var nodemailer = require("nodemailer");
const myGmailAccount = {
mail: 'your_gmail_address', // MAIL
passw: 'your_gmail_password', // PASSW
};
/* GET contact page. */
router.get("/", function (req, res, next) {
res.render("contact", { title: "Contact" });
});
router.post("/send", function (req, res, next) {
var transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: myGmailAccount.mail,
pass: myGmailAccount.passw,
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: myGmailAccount.mail,
to: 'emintayfur#icloud.com', // recipient mail
subject: "Website Submission",
text:
"You have a new submission with the following details...Name: " +
req.body.name +
" Email: " +
req.body.email +
" Message: " +
req.body.message,
html:
"<p> You got a new submission with the following details...</p><ul></ul><li>Name: " +
req.body.name +
"</li><li>Email: " +
req.body.email +
"</li><li>Message: " +
req.body.message +
"</li></ul>",
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.redirect("/");
} else {
console.log("Message Sent: " + info.response);
}
});
});
Yes, you need to enter your real information and I recommend you to read this text.
Even though Gmail is the fastest way to get started with sending
emails, it is by no means a preferable solution unless you are using
OAuth2 authentication. Gmail expects the user to be an actual user not
a robot so it runs a lot of heuristics for every login attempt and
blocks anything that looks suspicious to defend the user from account
hijacking attempts. For example you might run into trouble if your
server is in another geographical location – everything works in your
dev machine but messages are blocked in production.
Additionally Gmail has came up with the concept of “Less Secure” apps which is
basically
anyone who uses plain password to login to Gmail, so you might end up
in a situation where one username can send mail (support for “less
secure” apps is enabled) but other is blocked (support for “less
secure” apps is disabled). You can configure your Gmail account to
allow less secure apps here. When using this method make sure to also
enable the required functionality by completing the “Captcha Enable”
challenge. Without this, less secure connections probably would not
work.
And make sure the 'less secure apps' option is turned on
If you don't know how to open, you can visit here
For more information I suggest you to review this page

NodeJs and nodemailer - hide query parameters in the Email-Link and URL?

How can I hide the query-string parameters in the URL when sending an email to user with link and be able in the same time to read grab them when page is loaded in the browser?
Using: NodeJs, Angular1, MongoDb.
Code:
exports.newPass = function (req, res) {
User.findOne({
email: req.body.email
}, function (err, email) {
console.log('Email: ' + email);
if (err) throw err;
if (!email) {
console.log('Email: ' + email);
return res.json({ message: 'This Email does not exist!' });
}
var transport = mailer.createTransport({
host:'localhost',
port: 25,
secure: false,
auth: {
user: "userName",
pass: "passWord"
}
});
var mail = {
from: "DevOps <dev#localhost>",
to: "dev#localhost",
subject: "Set New Password",
text: "To set new Password: ",
html: '<strong>To set new Password: </strong>'
+ ' <a href="http://localhost:4000/endPoint#!/signup?m='
+ email.email
+ '&fn='
+ email.fullName
+ '&_id='
+ email._id
+ '">Restore Password</a>'
}
transport.sendMail(mail, function(error, response){
if(error){
console.log( 'Server Error: ' + error );
}else{
console.log( 'Message sent: ' + JSON.stringify(response.messageId) );
}
transport.close();
});
return res.json({ Ok: 'New Password will be send to you... please check your Emails' });
});
};
I'm able to manage this in the Angular Controller by adding the following:
$location.search({});
but the parameters and the values are still visible in the email-link.
They disappear after the user clicks on the email-link and goes to browser.
I would like to hide them also in the email which being sent to the recipient.
If safety is not a concern, you could create an object with the data you need, stringify it and then encode it in base 64. You can easily decode this back to the JSON string.
You'd do something like this in Node:
var data = { email: email.email, fn: email.fullName, _id: email._id };
data = JSON.stringify(data);
data = Buffer.from(data).toString('base64');
var mail = {
from: "DevOps <dev#localhost>",
to: "dev#localhost",
subject: "Set New Password",
text: "To set new Password: ",
html: '<strong>To set new Password: </strong>'
+ ' Restore Password'
}
Note that anyone can decode this back.
Depending on where you want to get this data back, you'd have to check different ways on how to decode this data. It's pretty similar in Node:
Buffer.from(b64string, 'base64');
However, if you don't want people to be able to decode this, then you'd have to take a different approach.
What I would do is generate a random hash, and store it in the database somewhere, indicating that it's connected to some user that wants a password change. You can then send that hash to the user, and look it up when the URL you sent is requested.

How to change sender name different form authenticated email?

var nodemailer = require('nodemailer');
// Not the movie transporter!
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '*****#gmail.com', // Your email id
pass: '*******' // Your password
}
});
var mailOptions = {
from: varfrom_name, // sender address
to: varto, // list of receivers
subject: varsubject, // Subject line
text: vartext, // plaintext body
html: varhtml // html body
};
console.log(mailOptions);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}else{
return console.log(info);
}
});
I want different sender address from the authenticated one ?
Suppose I authenticated data with abc#gmail.com, but I want to send the mail from xyz#gmail.com to def#gmail.com.
How to do that in node-mailer ?
// Using send mail npm module
var sendmail = require('sendmail')({silent: true})
sendmail({
from: ' xyz#gmail.com',
to: 'def#gmail.comh',
subject: 'MailComposer sendmail',
html: 'Mail of test sendmail ',
attachments: [
]
}, function (err, reply) {
console.log(err && err.stack)
console.dir(reply)
})
But the mails coming in the span box and the mails that we are sending is won't showing in the sent mail of sender mail address ?
I hope i will able to elaborate my question
I don't think you can. Gmail does not allow the change of the sender address.
However, you can look for another service, like postmark, sendgrid, or create your own smtp server.

Categories