You need to enable JavaScript to run this app. 在AWS Lambda上用nodemailer结合SES发邮件时PDF附件空白
优惠活动
大模型
产品
解决方案
定价
更多

在AWS Lambda上用nodemailer结合SES发邮件时PDF附件空白

AWS Lambda 下 nodemailer 对接 SES 发送附件异常问题

异常现象

  • 基础邮件功能正常:通过nodemailer调用Amazon SES发送普通正文邮件时,可正常完成投递
  • 附件功能异常:附件可被接收端正常下载,但内容损坏。例如内置简单文本的PDF附件,下载后打开为空白页面,附件文件大小存在有效数值,但内容完全无法解析读取
  • 后续业务要求:需支持发送PDF、Excel、CSV三类格式的附件

已完成的排查操作

  • 第一种附件配置:直接传入Lambda运行环境内的本地文件路径作为附件path参数
  • 第二种附件配置:通过fs.createReadStream读取本地文件流,作为附件content参数传入
  • 手动指定附件MIME类型:为PDF附件强制配置contentType: 'application/pdf'
  • 以上所有配置组合测试后,附件内容损坏的问题仍未解决

当前使用的核心代码

async function sendEmail() {
   
    const nodemailer = require('nodemailer');
    const fs = require('fs');
    
    const transporter = await nodemailer.createTransport({
        SES: new aws.SES()
    });

    await new Promise((resolve, reject) => {
        
        const mailOptions = {
            from: 'fromemail',
            to: 'toemail',
            subject: 'Message',
            text: 'I hope this message gets sent!',
            attachments: [
                {
                    filename: 'text5.pdf',
                    path: __dirname + '/pdf_dummy.pdf',
                    // content: fs.createReadStream( __dirname + '/pdf_dummy.pdf'), // 已尝试的配置项
                    // contentType: 'application/pdf' // 已尝试的配置项
                }
            ]
        };
       
        transporter.sendMail(mailOptions, (err, info) => {
            if (err) {
                console.log("ERROR | err: " + err);
                return reject(err);
            } else {
                console.log("INFO | info: " + JSON.stringify(info));
                return resolve(info);
            }
        });
   });
}

内容的提问来源于stack exchange,提问作者MJey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 19:24:09