0

我是 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(...)

4

1 回答 1

1

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

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',   
            }
        }); 
    });
};
于 2020-06-11T22:14:07.097 回答