【问题标题】:NodeJS send a PDF by Email using pdf creatorNodeJS 使用 pdf 创建器通过电子邮件发送 PDF
【发布时间】:2020-06-11 22:04:59
【问题描述】:

我是 NodeJS 的新手。我正在尝试从 HTML 生成 PDF 并使用 nodemailer 通过电子邮件发送。

exports.testReqID = async (req, res, next) => {
    const ID = req.params.myID;

    const htmlData = `
        <html>
            <head></head>
            <body>
                <h1>PDF Test. ID: ${ID}</h1>
            </body>
        </html>
    `; // Just a test HTML

    pdf.create(htmlData).toBuffer(function(err, buffer){
        let emailBody = "Test PDF";
        const email = new Email();
        const transporter = email.getTransporter();
        await transporter.sendMail({
            from: 'support@myemail.com', 
            to: 'me@myemail.com', 
            subject: "TEST", 
            html: emailBody, 
            attachments: {
                filename: 'test.pdf',
                contentType: 'application/pdf',   
                path: // How can I do it?
            }
        }); 
    });
};

Email() 类只是我对 nodemailer 的设置,它从 transporter = nodemailer.createTransport(...) 返回一个传输器

【问题讨论】:

    标签: node.js nodemailer html-pdf


    【解决方案1】:

    您可以将其作为缓冲区发送。 docs

    exports.testReqID = async (req, res, next) => {
        const ID = req.params.myID;
    
        const htmlData = `
            <html>
                <head></head>
                <body>
                    <h1>PDF Test. ID: ${ID}</h1>
                </body>
            </html>
        `; // Just a test HTML
    
        pdf.create(htmlData).toBuffer(function(err, buffer){
            let emailBody = "Test PDF";
            const email = new Email();
            const transporter = email.getTransporter();
            await transporter.sendMail({
                from: 'support@myemail.com', 
                to: 'me@myemail.com', 
                subject: "TEST", 
                html: emailBody, 
                attachments: {
                    filename: 'test.pdf',
                    content: buffer',   
                }
            }); 
        });
    };
    

    【讨论】:

    • 完美!谢谢
    猜你喜欢
    • 2011-08-20
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2011-04-01
    • 2018-02-21
    • 2015-06-21
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多