如何在nodemailer中将PDF附加到电子邮件?

Sah*_*ana 4 pdf jquery node.js nodemailer mean-stack

我想通过nodemailer将pdf报告附加到邮件给用户。我在前端使用jquery。

<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>
Run Code Online (Sandbox Code Playgroud)

API 看起来像这样。而且效果很好。我想知道如何动态添加附件

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");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Ada*_*dam 7

您可以通过以下方式添加附件:

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'
    }]
};
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看示例: https: //github.com/nodemailer/nodemailer/blob/master/examples/full.js