



function saveGmailToGoogleDriveAsPDF() {
var gmailLabels = "Export ";
var driveFolder = "My Gmail";
//Search Gmail for our Export Label
//GMail will return the threads of the email conversations. This is different than the raw emails. This is all of the emails within a thread.
var threads = GmailApp.search("label:" + gmailLabels, 0, 3);
if (threads.length > 0) {
/*GetFolders returns a FolderIterator. See if we have any folders. If not, then create one.
var folders = DriveApp.getFoldersByName(driveFolder);
var folder;
if ( folders.hasNext() ) {
folder = folders.next();
}
else
folder = DriveApp.createFolder(driveFolder);
/*Iterate through the threads of emails*/
for (var t=0; t<threads.length; t++) {
var msgs = threads[t].getMessages();
var html = "";
var attachments = [];
//Get the Subject of the first email in the thread. We'll use the Subject in the name of the email
var subject = threads[t].getFirstMessageSubject();
/* We're going to stick all of the messages in this thread into one pdf */
for (var m=0; m<msgs.length; m++) {
//designate what the first date is in the thread, we'll use this in the name as well
var firstDate;
var msg = msgs[m];
var date;
if ( m == 0 )
{
firstDate = msg.getDate();
date = firstDate;
}
else
date = msg.getDate()
//Build the HTML, you may wany to introduce some hrs. The wordpress pre tag was behaving oddly with html tags.
html += "From: " + msg.getFrom() + "";
html += "To: " + msg.getTo() + "";
html += "Date: " + date + "";
html += "Subject: " + msg.getSubject() + "";
html += msg.getBody().replace(/<img[^>]*>/g,"");
//Snag the attachments so we can save them for later
var atts = msg.getAttachments();
for (var a=0; a<atts.length; a++) {
attachments.push(atts[a]);
}
}
/* Iterate through the attachment files and create links in the pdf's footer */
if (attachments.length > 0) {
var footer = "Attachments:";
for (var z=0; z<attachments.length; z++) {
var attachment = attachments[z]
var filename = attachment.getName();
//You may want to treat different ContentType's differently...
var contentType = attachment.getContentType();
var iter = folder.getFilesByName( filename );
var file;
if ( iter.hasNext() )
file = iter.next()
else
file = folder.createFile(attachment);
footer += "" + file.getName() + "";
}
html += footer";
}
//Format the name to differentiate between Emails and attachments. Otherwise, everything starts to look the same.
//Write the formatted firstDate into the name
var filename = "Email: " + getFormattedDate( firstDate ) +": "+ subject;
var tempFile = DriveApp.createFile(filename+".html", html, "text/html");
/* Convert the Email Thread into a PDF File */
folder.createFile(tempFile.getAs("application/pdf")).setName(filename + ".pdf");
/*Get rid of the tmp file*/
tempFile.setTrashed(true);
}
}
}
Now to format the date use the following bit of code
function getFormattedDate( date )
{
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
month = (month < 10 ? "0" : "") + month;
day = (day < 10 ? "0" : "") + day;
hour = (hour < 10 ? "0" : "") + hour;
min = (min < 10 ? "0" : "") + min;
sec = (sec < 10 ? "0" : "") + sec;
var str = date.getFullYear() + "-" + month + "-" + day + "_" + hour + ":" + min + " ";
/*alert(str);*/
return str;
}
Next, run the code in Google App Scripts by hitting the play button.

Execute Google App Script Project to save Gmails to Google Drive as PDF

Export Emails to Google Drive as PDF

A self-motivated digital marketing specialist with 3+ years of experience advertising in the financial services industry.
While I wear several marketing hats, my primary focus is on content strategy and curation.
I aim to consistently challenge myself and position my skills toward personal and professional endeavors that lead to measurable results.