如何使用 Nodemailer 发送保存在 s3 上的 pdf 作为附件

Dev*_*_SP 2 javascript pdf node.js nodemailer

我有一个功能可以发送带有 pdf 附件的电子邮件,但是当我尝试发送本地没有文件的 pdf 时,电子邮件到达时没有任何附件...

async sendEmailWithAtt(email, subject, message, pathAtt) {
    const transporter = nodemailer.createTransport({
        host: process.env.EMAIL_HOST,
        port: process.env.EMAIL_PORT,
        auth: {
            user: process.env.EMAIL_USERNAME,
            pass: process.env.EMAIL_PASSWORD
        },
        attachments: [
            {
                filename: 'Document',
                path: pathAtt, // URL of document save in the cloud.
                contentType: 'application/pdf'
            }
        ]
    });

    const mailOptions = {
        from: process.env.EMAIL_USERNAME,
        to: email,
        subject: subject,
        text: message
    };

    await transporter.sendMail(mailOptions);
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*nde 5

Nodemailer文档指出,如果您想使用 URL,则必须使用hrefpath本地文件保留的 URL。

如果文件不公开,您可以使用httpHeaders属性传递一些标头。

attachments: [
 {
     filename: 'Document',
     href: pathAtt, // URL of document save in the cloud.
     contentType: 'application/pdf'
  }
]
Run Code Online (Sandbox Code Playgroud)

除此之外,您应该attachments.createTransport其放在消息中。

const mailOptions = {
    from: process.env.EMAIL_USERNAME,
    to: email,
    subject: subject,
    text: message,
    attachments: [ /* ... */]
};

await transporter.sendMail(mailOptions);
Run Code Online (Sandbox Code Playgroud)