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

时间:2016-03-01 08:22:37

标签: jquery node.js pdf mean-stack nodemailer

我想通过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>

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");
        }
    });
});

1 个答案:

答案 0 :(得分:2)

您可以这样添加附件:

&#xA;&#xA;
  var mailOptions = {&#xA; to:req.query.to,&#xA; subject:req.query.subject,&#xA; text:req.query.text,&#xA;附件:[{&#XA; filename:'filename.pdf',&#xA; content:new Buffer(FILE_CONTENT,'base64'),&#xA; contentType:'application / pdf'&#xA; }]&#xA;};&#xA;  
&#xA;&#xA;

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

& #xA;