You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script中如何为MailApp指定邮件附件名称?

如何在Google Apps Script中正确指定邮件附件名称?

你当前的代码虽然能正常发送邮件,但部分邮件客户端无法识别Blob对象的setName设置的名称,导致附件显示通用名称(如mime-attachment.pdf)。问题核心在于attachments字段需要接收数组格式的附件定义,且部分客户端对Blob的name属性兼容性不佳。

解决方法一:将Blob对象放入数组并明确扩展名

把单个Blob包装进数组,同时给文件名加上.pdf后缀,确保客户端能正确识别:

var message = {
  to: recipient,
  subject: subject,
  body: body,
  attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Sales Report.pdf")]
};

MailApp.sendEmail(message);

解决方法二:显式定义附件对象(兼容性最优)

通过对象显式指定文件名、内容和MIME类型,这种方式对所有邮件客户端的兼容性最好:

var pdfBlob = SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF);
var message = {
  to: recipient,
  subject: subject,
  body: body,
  attachments: [
    {
      fileName: "Sales Report.pdf",
      content: pdfBlob.getBytes(),
      mimeType: MimeType.PDF
    }
  ]
};

MailApp.sendEmail(message);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 15:45:04