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

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

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

We will never send any spam emails. Promise.