Reputation: 37
I am trying to send PDF file as attachment to user but when opening that file error is coming
"Failed to load PDF document."
I am using node mailer to send emails.
I am not getting the problem with it.
Please help!
Here is my code:-
router.get('/file',function(req, res){
var fromName = "Ikshit";
var mailOptions={
to: '[email protected]',
subject: 'Test',
from: "[email protected]",
headers: {
"X-Laziness-level": 1000,
"charset" : 'UTF-8'
},
attachments: [
{
raw: 'Content-Type: application/pdf; charset=utf-8;\r\n' +
'Content-Disposition: attachment;filename=text.pdf;\r\n' +
'\r\n' +
'Hello world!'
}
],
html: 'Text'
}
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '[email protected]',
pass: 'password'
}
});
transporter.sendMail(mailOptions, function(error, response){
if(error){
return res.send(err);
}
else{
res.send({
state:'success',
message:"Send"
});
}
});
})
Upvotes: 1
Views: 2229
Reputation: 1448
Try this code and it definitely works
fs.readFile("E:/syed/nodejs/tasks/mail/mailwithdb/sheet.pdf",function(err,data){
var mailOptions={
from:' <[email protected]>',
to:'[email protected]',
subject:'Sample mail',
text:'Hello !!!!!!!!!!!!!',
attachments:[
{
'filename':'sheet.pdf',
'content': data,
'contentType':'application/pdf'
}]
}
transporter.sendMail(mailOptions,function(err,res){
if(err){
console.log('Error');
}
else{
console.log('Email Sent');
}
})
});
Upvotes: 1
Reputation: 2156
Try this way...
var mailOptions = {
from: '[email protected]'.
to: '[email protected]',
subject: 'an attached file',
text: 'check out this attached pdf file',
attachments: [{
filename: '<fileName>',
path: '<PDF file absolute path>'
contentType: 'application/pdf'
}]};
transporter.sendMail(mailOptions, function (err, info) {
if(err){
console.error(err);
res.send(err);
}
else{
console.log(info);
res.send(info);
}
}
);
Upvotes: 0