Nodemailer does not Send Mail Text - javascript

I can send my mail. But I can not send mail text. Where is problem, I do not know. if I write it like this "mail text ........", it is running. but if I write as variable, mail text does not send.
function sendAlarmMail(emails, alarmMessage) {
let mailAddresses = emails;
let mailText = alarmMessage;
try {
let testAccount = nodemailer.createTestAccount();
let transport = nodemailer.createTransport({
maxConnections: 3,
pool: true,
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'xxxx#gmail.com',
pass: 'xxxx'
}
});
let message = {
from: 'xxx#gmail.com',
to: "xxxx#xxxxx.com",
subject: 'Yüksek Enerji Kullanım Uyarısı',
text: `${mailText}`
}
transport.sendMail(message, function (err, info) {
if (err) {
console.log(err);
console.log("Mail Gönderilemedi !");
} else {
console.log(info);
console.log("Mail Gönderildi !");
}
});
}
sendAlarmMail("mail1, mail2, mail3", "mail texts");
How can I send my text in my mail. help please.

My problem had been solved When I remove text:. I wrote my variable into html code instead text:.
let message = {
from: 'xxxx#gmail.com',
to: emails,
subject: 'Yüksek Enerji Kullanım Uyarısı',
html: "<div><p>" + alarmMessage + "</p></div>"
}

Related

How to send mail using nodemailer and Zoho mail?

I'm trying to setup ZOHO mail with Nodemailer. The mail is configured correctly and I'm using following code to send the mail, but still getting error in sending mail:
const nodemailer = require('nodemailer');
let from = `Company Name <contact#company.com>`
let transporter = nodemailer.createTransport({
// host: "smtp-mail.gmail.com",
host: 'smtp.zoho.com',
port: 465,
secure: true,
auth: {
user: "contact#company.com",
pass: "mypassword"
}
});
// Mail response to User
const mailResponse = {
from: from,
to: `userName`,
subject: "📞 Thanks For Connecting With Company Name",
html: // mail body
}
try {
await transporter.sendMail(mailResponse);
res.status(200).json({ message: "Message Sent" });
} catch (err) {
res.status(400).json({ message: "Unexpected Error!!! Please try again" });
}
Please let me know how can I fix this issue. i have tried every possible solution given on website.
Try this -
service: "gmail",
host: "smtp.gmail.com",
auth: {
user: "username#company.com",
pass: "yourpassword"
}
At the place of user put your email and in pass but your password or you can get these from env to be safe.

How to send emails using smtp node mailer

I was trying to send a test email using SMTP on node mailer but it says connection timed out. the snippet I was using is down below.
const nodemailer = require("nodemailer");
async function main() {
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
pool:true,
host: '213.55.96.132',
port: 25,
auth: {
user: "user#ethionet.et",
pass: "drafgthsjaid321##"
},
pool: true,
logger :true,
debug:true,
secure: false,
})
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take our messages');
}
});
let mailOptions = {
from: "user#ethionet.et",
to: ["someemail#gmail.com",],
subject: 'Test email',
text: `Hello world`
};
transporter.sendMail(mailOptions, function(err, data) {
if (err) {
console.log("Error " + err);
} else {
console.log("Email sent successfully");
}
});
}
main().catch(console.error);
I don't mind leaking the credentials and it works when i try and send emails through SMTP from here.
why is this faliing?
You need to read a little more than the first page of documentation :)
Create your message
let message = {
...,
from: 'mailer#nodemailer.com', // listed in rfc822 message header
to: 'daemon#nodemailer.com', // listed in rfc822 message header
envelope: {
from: 'Daemon <deamon#nodemailer.com>', // used as MAIL FROM: address for SMTP
to: 'mailer#nodemailer.com, Mailer <mailer2#nodemailer.com>' // used as RCPT TO: address for SMTP
}
}
Send the message through the transporter
transporter.sendMail(...).then(info=>{
console.log('Preview URL: ' + nodemailer.getTestMessageUrl(info));
});
Turns out the problem was that my ISP blocks port 25.

how to send emails node mailer without smtp

I am trying to send emails through nodemailer, and I've used it multiple times with my gmail account... the problem is, I don't want to use my gmail account now, and I want to use my business email so I could send out emails to clients on a regular...
I have it set up like this right now, but not sure how to do it without gmail / smtp:
app.post('/sendBatchEmail', (req, res) => {
var emails = [];
var emailSubject = req.body.emailSubject;
var emailMessage = req.body.emailMessage;
//perform db2 send
var sendEmail = "select * from testEmails"
ibmdb.open(ibmdbconnMaster, function (err, conn) {
if (err) return console.log(err);
conn.query(sendEmail, function (err, rows) {
if (err) {
console.log(err);
}
for (var i = 0; i < rows.length; i++) {
emails.push(rows[i].EMAIL)
}
//send email
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: "x",
pass: "x",
},
});
// send mail with defined transport object
let sendBatch = await transporter.sendMail({
from: "", // sender address
to: "xxxxx#gmail.com",
bcc: emails, // list of receivers
subject: emailSubject, // Subject line
text: emailMessage, // plain text body
});
console.log("Message sent: %s", sendBatch.messageId);
}
main().catch(console.error);
res.redirect("/index");
conn.close(function () {
console.log("closed the function app.get(/account)");
});
});
});
I am not sure how to not use smtp server so I can use biz email, or even if this is possible! Thanks in advance for help :)
})
As you can do this using nodemailer, but sending an email without a SMTP is not recommended as there are much higher chances of being rejected or put into spam folder. I'm pretty sure gmail like services will put the emails in Spam folder. here is how you can implement it:
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: "x",
pass: "x",
},
});
// send mail with defined transport object
let sendBatch = await transporter.sendMail({
from: "<foo#example.com>", // sender address
to: "xxxxx#gmail.com",
bcc: emails, // list of receivers
subject: emailSubject, // Subject line
text: emailMessage, // plain text body
});
console.log("Message sent: %s", sendBatch.messageId);
}
For more details you can check here1 and here2

Nodemailer: Greeting never received

When trying to send email within Node using Nodemailer (https://github.com/nodemailer/nodemailer), the call to the sendMail of the Nodemailer transporter is raising the error Greeting never received when using in conjunction with an Ethereal test email account.
I have tried using both a "callback approach" and also an "async/await" approach, but the same error is thrown in both scenarios. Both examples are pretty much straight from the working examples in the Nodemailer documentation. Maybe I'm missing something simple? :)
Here is the "callback approach" code that is producing the error:
it('can send email with a dynamic test account', done => {
nodemailer.createTestAccount((err, account) => {
const transporter = nodemailer.createTransport({
host: 'smtp.ethereal.email',
port: 587,
auth: {
user: account.user, // generated ethereal user
pass: account.pass // generated ethereal password
}
});
const mailOptions = {
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
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
done();
});
});
}).timeout(10000);
And here is the stacktrace of the error:
{ Error: Greeting never received
at SMTPConnection._formatError (/Users/<username>/projects/personal/learning-tests/javascript/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
at SMTPConnection._onError (/Users/<username>/projects/personal/learning-tests/javascript/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:579:20)
at Timeout._greetingTimeout.setTimeout (/Users/<username>/projects/personal/learning-tests/javascript/nodemailer/node_modules/nodemailer/lib/smtp-connection/index.js:520:22)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5) code: 'ETIMEDOUT', command: 'CONN' }
And some additional info:
node version: 8.11.2
nodemailer version: 4.6.4
operating system: OSX version 10.12.6
In my case I needed to set the secure key to true on the transporter object and then it worked.
let transporter = nodemailer.createTransport({
host: "mail.hostname.com",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: 'user#hostname.com', // generated ethereal user
pass: 'password', // generated ethereal password
}
});
In my case, when I have changed port 586 to 587, then it worked.
Check your internet connection probably its down .
below is an example with Etheral Email with typescript
import * as nodemailer from "nodemailer";
export const sendEmail = async (recipient: string, url: string, linkText: string) => {
nodemailer.createTestAccount((err, account) => {
if (err) {
console.log(err);
}
const transporter = nodemailer.createTransport({
host: account.smtp.host,
port: account.smtp.port,
secure: account.smtp.secure,
auth: {
user: account.user,
pass: account.pass
}
});
const message = {
from: "Sender Name <sender#example.com>",
to: `Recipient <${recipient}>`,
subject: "Nodemailer is unicode friendly ✔",
text: "Hello to myself!",
html: `
<html>
<body>
<p>Testing sparkpost API</p>
${linkText}
</body>
</html>`
};
transporter.sendMail(message, (err, info) => {
if (err) {
console.log("Error occurred. " + err.message);
}
console.log("Message sent: %s", info.messageId);
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
});
});
};
In my case the smtpd_recipient_restrictions in /etc/postfix/main.cf was causing this issue.
Changed it to:
smtpd_recipient_restrictions =
permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
check_policy_service unix:private/policyd-spf
and now it works!
const transporter = nodemailer.createTransport({
service: 'config.mail.service',
port: 8000,
auth: {
user: 'config.mail.username',
pass: 'config.mail.password'
}
});
module.exports = {
activationsMail: function (req) {
// setup email data with unicode symbols
const mailOptions = {
from: '"Ecommerce" <noreply#ecommerce.com>', // sender address
to: req.body.email, // list of receivers
subject: 'Ecommerce Account Activate', // Subject line
html: '<div>Please click here to active your account.</div>' // html body
};
console.log('PORT', req.headers.host);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log('Email Error', error);
} else {
console.log('Email sent: ' + info.response);
}
})
}
};
const transporter = nodemailer.createTransport({
service: config.mail.service,
port: 8000,
auth: {
user: config.mail.username,
pass: config.mail.password
}
});
module.exports = {
activationsMail: function (req, data) {
// setup email data with unicode symbols
const link = 'http://' + req.headers.host + '/user/activate/' + data.verifyCode;
console.log('CODE :', data.verifyCode);
const mailOptions = {
from: '"Ecommerce" <noreply#ecommerce.com>', // sender address
to: req.body.email, // list of receivers
subject: 'Please confirm your Email account', // Subject line
html: '\n\n' + 'Please Click here to verify <a href=' + link + '> Click here</a>'
};
//console.log('PORT', req.headers.host);
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log('Email Error', error);
} else {
// callback(true);
console.log('Email sent: ' + info.response);
}
})
};

unable to email in nodejs

I am using nodemail npm package.
I configured the options like this:
function feedback(req, res, next){
console.log('feed back given....', req.body);
smtpTrans = nodemailer.createTransport('SMTP', {
service: 'Gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
ignoreTLS: false,
tls: { rejectUnauthorized: true },
debug: false,
auth: {
user: "xxxxxx#gmail.com",
pass: "xxxxxx"
}
});
//Mail options
mailOpts = {
from: from: req.body.email,
to: 'xxxxxx#gmail.com',
subject: 'EMAIL FROM Rsc-student: ' + req.body.subject,
text: req.body.message
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
res.send(error);
console.log('error sending mail');
}
else {
res.send(response);
console.log('success sending mail');
}
});
}
If I am not wrong, I configured correctly but still unable to send mail. Its printing the error case
Remove extra from: from mailOpts.
Follow this 3 steps:
Login to your Gmail account.
Follow this link allow gmail to send mail over less secure app.
Select on option.
It works for me. I hope it will help you.

Categories

Resources