Save Gmail Messages as Google Documents

The Google Script will save any Gmail message (or thread) in your Google Drive as a native Google Document with proper formatting. Unlike the Save Gmail as PDF script that downloads the email threads as PDF files in your Google Drive, this Google Script create a Google Docs file for your Gmail message and these do not count against the storage quota.

function saveGmail(msgID) {
  // Based on Drive Scoop
  // Available at https://github.com/google/gfw-deployments

  var message = GmailApp.getMessageById(msgID);

  // Grab the message's headers.
  var from = message.getFrom();
  var subject = message.getSubject();
  var to = message.getTo();
  var cc = message.getCc();
  var date = message.getDate();
  var body = message.getBody();

  // Begin creating a doc.
  var document = DocumentApp.create(subject);
  var document_title = document.appendParagraph(subject);
  document_title.setHeading(DocumentApp.ParagraphHeading.HEADING1);

  var style = {};
  style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
  document_title.setAttributes(style);

  var headers_heading = document.appendParagraph('Gmail Message Headers');
  headers_heading.setHeading(DocumentApp.ParagraphHeading.HEADING2);

  AddGmailHeaderToDoc(document, 'From', from);
  AddGmailHeaderToDoc(document, 'To', to);
  AddGmailHeaderToDoc(document, 'Cc', cc);
  AddGmailHeaderToDoc(document, 'Date', date);
  AddGmailHeaderToDoc(document, 'Subject', subject);

  var body_heading = document.appendParagraph('Body (without Markup)');
  body_heading.setHeading(DocumentApp.ParagraphHeading.HEADING2);

  var sanitized_body = body.replace(/<\/div>/, '\r\r');
  sanitized_body = sanitized_body.replace(/<br.*?>/g, '\r');
  sanitized_body = sanitized_body.replace(/<\/p>/g, '\r\r');
  sanitized_body = sanitized_body.replace(/<.*?>/g, '');
  sanitized_body = sanitized_body.replace(/&#39;/g, "'");
  sanitized_body = sanitized_body.replace(/&quot;/g, '"');
  sanitized_body = sanitized_body.replace(/&amp;/g, '&');
  sanitized_body = sanitized_body.replace(/\r\r\r/g, '\r\r');

  var paragraph = document.appendParagraph(sanitized_body);

  document.saveAndClose();

  return document.getUrl();
}

function AddGmailHeaderToDoc(document, header_name, header_value) {
  if (header_value === '') return;
  var paragraph = document.appendParagraph('');
  paragraph.setIndentStart(72.0);
  paragraph.setIndentFirstLine(36.0);
  paragraph.setSpacingBefore(0.0);
  paragraph.setSpacingAfter(0.0);
  var name = paragraph.appendText(header_name + ': ');
  name.setBold(false);
  var value = paragraph.appendText(header_value);
  value.setBold(true);
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻