【问题标题】:How to attach PDF to email in nodemailer?如何在 nodemailer 中将 PDF 附加到电子邮件?
【发布时间】:2016-06-13 13:43:17
【问题描述】:

我想通过 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");
        }
    });
});

【问题讨论】:

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


    【解决方案1】:

    您可以这样添加附件:

    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'
        }]
    };
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2020-09-03
      • 2014-07-01
      • 2016-01-18
      • 2017-11-07
      相关资源
      最近更新 更多