google-apps-script

GmailApp

Remarks#

See also the official API reference for the GmailApp for further details on the available methods.

Get CSV file attached to a mail

Assume that we have a system that sends out daily reports over email in the form of attached CSV files and that we want to access these.

function getCsvFromGmail() {
  // Get the newest Gmail thread based on sender and subject
  var gmailThread = GmailApp.search("from:noreply@example.com subject:\"My daily report\"", 0, 1)[0];
  
  // Get the attachments of the latest mail in the thread.
  var attachments = gmailThread.getMessages()[gmailThread.getMessageCount() - 1].getAttachments();
  
  // Get and and parse the CSV from the first attachment
  var csv = Utilities.parseCsv(attachments[0].getDataAsString());
  return csv;
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow