Reputation: 41
I want to attach pdf reports to mail to users via nodemailer.I am using jquery in front end.
<script>
$(document).ready(function () {
var from, to, subject, text;
$("#send_email").click(function () {
to = $("#to").val();
subject = $("#subject").val();
text = $("#content").val();
$("#message").text("Sending E-mail...");
$.get("http://localhost:8080/send", {to: to, subject: subject, text: text}, function (data) {
if (data == "sent") {
$("#message").empty().html("Email is sent " + to + " .");
}
});
});
});
</script>
And API looks like this. And it works fine. I want to know how to add attachments to this dynamically
app.get('/send', function (req, res) {
var mailOptions = {
to: req.query.to,
subject: req.query.subject,
text: req.query.text
};
console.log(mailOptions);
smtpTransport.sendMail(mailOptions, function (error, response) {
if (error) {
console.log(error);
res.end("error");
}
else {
console.log("Message sent: " + response.message);
res.end("sent");
}
});
});
Upvotes: 4
Views: 9544
Reputation: 5253
You can add attachment this way:
var mailOptions = {
to: req.query.to,
subject: req.query.subject,
text: req.query.text,
attachments:[{
filename: 'filename.pdf',
content: new Buffer(FILE_CONTENT, 'base64'),
contentType: 'application/pdf'
}]
};
You can see examples here: https://github.com/nodemailer/nodemailer/blob/master/examples/full.js
Upvotes: 7