0

I am trying to specify the mail file attachment name in my Apps Script code. The email sends correctly, however, the attachment is only named a generic name 'mime-attachment.pdf' or 'mail-attachment.pdf' depending on the mail client. I'm currently using the following code:

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

  MailApp.sendEmail(message);

How can I specify the name of the attachment?

MeesterZee
  • 99
  • 7
  • I think that in your script, "Sales Report" is used as the filename of the attachment file. So, I cannot replicate your situation of `however, the attachment is only named a generic name 'mime-attachment.pdf' or 'mail-attachment.pdf' depending on the mail client.`. When I tested your script, it seems that the attachment file has the filename of "Sales Report". For example, when you get an email using another client, the same situation as your current situation will be obtained? – Tanaike Mar 17 '23 at 00:18
  • I have also tested your script and it worked fine. I applied it to a sample spreadsheet and your script managed to convert it into a `PDF` file with `sales report` as title. Can you please further elaborate what happened so that we may replicate the issue that you have encountered? – PatrickdC Mar 17 '23 at 01:26
  • Thank you both for testing. I initially only tested this in both the iOS default mail app and the macOS default mail client. Perhaps it's an issue with how Apple handles the attached file? When checking the message through Gmail, the attachment name appears correct. – MeesterZee Mar 17 '23 at 04:29
  • Thank you for replying. From your reply, it was found that your current issue depends on the mail client. In your mail client, for example, are there parameters like the header for giving the custom filename of the attachment file? If such a parameter can be used, I thought that your goal might be able to be achieved. – Tanaike Mar 17 '23 at 06:26
  • I was able to test a few other mail clients and they all handle the attached file naming correctly. This is definitely seems to be an issue with the Apple mail clients. I do not see a way to set header parameters as Tainake has suggested. – MeesterZee Mar 17 '23 at 14:12

1 Answers1

0

Have you tried to set attachments as an array of files? "BlobSource [ ] - an array of files to send with the email" - from documentation .

Instead of

attachments: SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Sales Report")

change to

attachments: [SpreadsheetApp.getActiveSpreadsheet().getAs(MimeType.PDF).setName("Sales Report")]

More info here

  • Nikola, thank you for your suggestion, unfortunately, the file name is still incorrect when using Apple's mail clients. – MeesterZee Mar 17 '23 at 14:05