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 net but I could't find something useful.Is there any way that I can attach files to with that or any resource that can help me to attach file with nodemailer?
var nodemailer = require('nodemailer');
var events = require('events');
var check =1;
var events = new events.EventEmitter();
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "gmail",
auth: {
user: "example@gmail.com",
pass: "pass"
}
});
function inputmail(){
///////Email
const from = 'example<example@gmail.com>';
const to = 'example@yahoo.com';
const subject = 'example';
const text = 'example email';
const html = '<b>example email</b>';
var mailOption = {
from: from,
to: to,
subject: subject,
text: text,
html: html
}
return mailOption;
}
function send(){
smtpTransport.sendMail(inputmail(),function(err,success){
if(err){
events.emit('error', err);
}
if(success){
events.emit('success', success);
}
});
}
///////////////////////////////////
send();
events.on("error", function(err){
console.log("Mail not send");
if(check<10)
send();
check++;
});
events.on("success", function(success){
console.log("Mail send");
});
If you are passing options object in mail composer constructor and attachment is on http server then it should look like:
Include in the var mailOption the key attachments, as follow:
}
Choose the option that adjust to your needs.
Link:Nodemailer Repository GitHub
Good Luck!!
The alternative solution is to host your images online using a CDN and link to the online image source in your HTML, eg.
<img src="list_image_url_here">
.(I had problems with nodemailer's image embedding using nodemailer version 2.6.0, which is why I figured out this workaround.)
An added benefit of this solution is that you're sending no attachments to nodemailer, so the sending process is more streamlined.
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 :
And then the call :
The mail comes successfully but with no attachment. Even if I set a string or buffer attachment it's the same result.