nodemailer attachment text file - node.js

I failed to attach a txt file that I created earlier in the script to a mail to be sent via nodemailer. The mail is successfully sent however, it is empty. How would I fix this?
var nodemailer = require ('nodemailer');
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 460,
auth: {
user: 'name#gmail.com',
pass: 'password'
},
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: "name" <'name#gmail.com',
to: 'name#gmail.com',
subject: 'group_1',
attachement: [ {
filename: 'group_1.txt',
path: /Users/derinserbetcioglu/Documents/NodeJS/'group_1.txt' } ]
};
transporter.sendMail(mailOptions, (error, info) => {
if (error){
console.log(error);
}
console.log("the message was successfully sent!")
console.log(info);
})

Try the path as path:
'/Users/derinserbetcioglu/Documents/NodeJS/group_1.txt'
instead of path: /Users/derinserbetcioglu/Documents/NodeJS/'group_1.txt'
And i think it should be attachments not attachment

Related

Can't send mail - all recipients were rejected: 550 HELO using nodejs

We are gettting error Can't send mail - all recipients were rejected: 550 HELO can't be [127.0.0.1]. Please contact your ISP.
The below code was working up to last month and now its throwing error.But same mail configuration working while using c#.
`const transporter = nodemailer.createTransport({
port: 587,
host: "smtp.online.net",
auth: {
user: 'info#domain.com',
pass: 'xxxxx',
},
secure: false
});
const mailData = {
from: 'info#domain.com',
to: "test#test.com",
subject: 'subject',
html: 'message'
};
transporter.sendMail(mailData, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Sent');
});`

Unable to send email through node js, tried few methods

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
secureConnection: false,
port: 587,
tls: {
ciphers: 'SSLv3'
},
requireTLS: true,
auth: {
user: 'mygmail',
pass: 'mypass'
}
});
var mailOptions = {
from: 'mygmail',
to: 'receiver gmail',
subject: 'Sending Email using Nodemailer',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error)
return console.log(error);
console.log('Email sent: ' + info.response);
});
Above is my code for sending email through node js but I keep encounter a timeout error as below
{ Error: connect ETIMEDOUT 74.125.24.109:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
errno: 'ETIMEDOUT',
code: 'ECONNECTION',
syscall: 'connect',
address: '74.125.24.109',
port: 587,
command: 'CONN' }
I also tried another basic way as for the following link: https://www.w3schools.com/nodejs/nodejs_email.asp but it is not working as well.
I've already turned on allow the less secure app on my google account.
Beside of that, I also tried the method like the one in the following the link: https://jsfiddle.net/burawi/1u9m2mou/ and it is still unable to work, I generated all the client_id, client_secret, access_token, and refresh_token.
Does anyone have any latest guide or solution for sending email through node js?
Thanks
This is a network restriction issue.
Please try the same thing in your mobile network and it will work.
router.get('/mailTest', (req, res) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "mrmanagementbbsr#gmail.com", // generated ethereal user
pass: "Test#123" // generated ethereal password
}
});
let mailOptions = {
from: "mrmanagementbbsr#gmail.com", // sender address
to: 'nsubhadipta#gmail.com', // list of receivers
subject: "test subject", // Subject line
text: 'demo text'
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.json({ status: -1, message: 'Error Occured', error: error });
}
else {
res.json({ status: 1, message: "Email Sent" });
}
});
});
Ensure you turn on your Less secure app access, else your emails will not go through.
Then you might want to try this example:
{
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mymail',
pass: 'mypass'
}
});
let mailOptions = {
from: 'mygmail',
to: 'receiver gmail',
subject: 'Sending Email using Nodemailer',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, (error, info)=>{
if (error) {
console.log(error);
} else {
console.log('Email sent ' + info.response');
}
});
res.json({success: 'whatever message you plan on writing'});
}

Nodemailer ECONNREFUSED

I am trying to use nodemailer to send an email but I keep getting this error:
Here is my code:
const nodemailer = require('nodemailer');
...
app.post('/api/SendEmail', (req, res) => {
var transporter = nodemailer.createTransport({
auth: {
user: 'x#gmail.com',
pass: 'x'
}
});
var mailOptions = {
from: 'x#gmail.com',
to: 'x#incasu.co.za',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(err, info) {
if (err) return res.status(400).send(err);
return res.status(200).send({response: 'success'});
});
})
Could someone please help me with this. I basically followed this process: https://www.w3schools.com/nodejs/nodejs_email.asp
UPDATE:
I added service: 'gmail', in the transporter as suggested by Janith Kasun:
service: 'gmail',
auth: {
user: 'x#gmail.com',
pass: 'x'
}
My error now changed to this:
I found this answer:
NodeJs Error When Submitting Email using Nodemailer
I have no idea what any of these settings do, but my emails are now being sent correctly with this code:
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});

Request is going on pending upon sending email using nodemailer

I am using nodemailer to send email using nodejs. Here is my code
export function createOrder(req, res) {
var transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'fahadsubzwari924#gmail.com',
pass: 'Jumpshare1'
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: 'Fahad <fahadsubzwari924#gmail.com>',
to: 'shan4usmani#hotmail.com',
subject: 'Order Status ',
text: 'Your order has finished'
}
transporter.sendMail(mailOptions, function (err, res) {
if (res) {
console.log(res);
res.json({
message: res,
status: 200
})
} else {
console.log(err, 'Error');
}
})
}
When i am trying to send email then email is sending successfully but request is going on pending for a long time which is shown in network section of chrome.
How can i get rid of this request pending issue?

How to send base64 string as image in Node.js using nodemailer?

I have nodejs get request which need to send the base 64 string as image in the email which as shown below
router.post('/sendEmailNotification', function(req, res, next){
var inlineBase64 = require('nodemailer-plugin-inline-base64');
//here i have big image
let img ='/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBx'
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
// secure: true,
secureConnection: false,// true for 465, false for other ports
auth: {
user: 'dhanalakshmi.07k#gmail.com',
pass: '23'
},
tls:{
rejectUnauthorized:false
}
});
transporter.use('compile', inlineBase64({cidPrefix: 'somePrefix_'}));
var mailOptions = {
from: 'dhanalakshmi.06k#gmail.com',
to: 'dhanalakshmi.06k#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!',
html: '<img src=data:image/png;base64,'+img+'/>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
res.send(info.response);
}
});
});
after the email send this should received as image but it showing the entire tag with image string please say
html: '<img src=data:image/png;base64,'+img+'/>' how should i send this to get the actual image
Use it like this:
html: '<html><body><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA9QAAADmCAIAAAC77FroAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAO..."></body></html>'

Resources