NodeJS 使用 pdf 创建器通过电子邮件发送 PDF

NodeJS send a PDF by Email using pdf creator

我是 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() class 只是我对 nodemailer 的设置,它 returns 来自 transporter = nodemailer.createTransport(...)

的传输器

您可以将其作为缓冲区发送。 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',   
            }
        }); 
    });
};