User Tools

Site Tools


gmail_setup

Step-by-Step: Gmail to PDF by Specific Sender ✅ Step 1: Open Google Apps Script Go to https://script.google.com.

Click “New Project.”

✅ Step 2: Paste this optimized sender-specific script: Replace “sender@example.com” with your target sender’s email address:

function emailsFromSenderToPDF() {

var senderEmail = "sender@example.com"; // Replace with sender's email
// Search Gmail specifically by sender
var threads = GmailApp.search("from:" + senderEmail);
var folderName = "Gmail PDFs - " + senderEmail;
var folder, folders = DriveApp.getFoldersByName(folderName);
if (folders.hasNext()) {
  folder = folders.next();
} else {
  folder = DriveApp.createFolder(folderName);
}
for (var i = 0; i < threads.length; i++) {
  var messages = threads[i].getMessages();
  for (var j = 0; j < messages.length; j++) {
    var message = messages[j];
    // Confirm sender explicitly matches
    if (message.getFrom().includes(senderEmail)) {
      var subject = message.getSubject().replace(/[^a-zA-Z0-9 ]/g, '');
      var date = Utilities.formatDate(message.getDate(), "GMT", "yyyy-MM-dd");
      var filename = date + " - " + subject + ".pdf";
      // Convert email body to PDF
      var blob = Utilities.newBlob(message.getBody(), "text/html", filename).getAs("application/pdf");
      // Save to Google Drive
      folder.createFile(blob);
    }
  }
}

}

What this does clearly:

✅ Finds all emails matching exactly your chosen sender.

✅ Converts each email into a PDF.

✅ Stores PDFs in a folder labeled specifically for that sender.

✅ Step 3: Run your script: Click the ▶️ Run button at the top.

The first time, authorize the script (click “Review permissions” and allow).

📂 Step 4: View your PDFs: Go to your Google Drive.

Open the folder named Gmail PDFs - sender@example.com.

All sender-specific emails are organized as PDFs, neatly timestamped.

🔄 Optional Enhancements: Date-specific search:

javascript Copy Edit var threads = GmailApp.search(“from:” + senderEmail + “ after:2024/01/01 before:2024/03/01”); Subject line filters:

javascript Copy Edit var threads = GmailApp.search(“from:” + senderEmail + “ subject:Invoice”); ✅ Ready to proceed? Just let me know if you need additional adjustments!

gmail_setup.txt · Last modified: 2025/03/22 20:03 by lwattsii