I have code that send email with nodemailer in nodejs but I want to attach file to an email but I can\'t find way to do that I search on ne
I've tested each of these attachments methods and none is ok for me. Here's my mailer function code without the smtp transport config :
function mailer(from, to, subject, attachments, body) {
// Setup email
var mailOptions = {
from: from,
to: to,
subject: subject,
attachments: attachments,
html: body
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
if(error) console.log(error);
else console.log("Message sent: " + response.message);
// shut down the connection pool, no more messages
smtpTransport.close();
});
}
And then the call :
var attachments = [{ filename: 'test.pdf', path: __dirname + '/pdf/test.pdf', contentType: 'application/pdf' }];
mailer("exped@gmail.com", "mymail@gmail.com", "Test", attachments, "Hello
");
The mail comes successfully but with no attachment. Even if I set a string or buffer attachment it's the same result.