I am generating a PDF client side with jsPDF. I need to send it to my Express server using Axios. At the end I need to send it via email with Nodemailer. Where am I wrong?
Client side code:
//doc creation ....
var res = doc.output('datauristring'); //this line!!!!
axios.post('/mailsender', res).then((res) => {
if(res.status === 'ok') console.log("Yeah!");
else console.log(":(");
});
Server side code:
...
api_router.post('/mailsender', (req, res) => {
mail.send(req.body, (res) => {
res.status(200).json({"status": res ? 'ok' : 'error' });
});
});
mail.js that is:
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.mail.yahoo.com',
port: 465,
secure: true,
auth: {
user: 'example@yahoo.it',
pass: 'password'
}
});
exports.send = function (data, callback) {
let mailOptions = {
from: '"My application" <example@myapp.com>',
to: "receiverAddress",
subject: "Attachment experiment",
text: "My <3",
attachments: [
{
filename: 'attachment.pdf',
content: data,
contentType: 'application/pdf',
encoding: 'base64' //this line!!!!
}
]
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
callback(false);
}
callback(true);
});
}
All is working fine, except that if I try to open the attachment in the received mail, Preview says that the file is damaged. The same if I try to open it with google chrome or with other PDF readers. Probably, the two lines with a comment has to be changed. Thank you for your attention!