Skip to content

Task guide

Label emails from one sender in Gmail

Put a Gmail label on every conversation from one sender, such as a newsletter, a few at a time. The script creates the label if needed and skips conversations that already have it.

1 · Prepare

Put these pieces in place

  • Pick one sender address you receive mail from, such as a newsletter.
  • Type the same search into the Gmail search box first and check that it finds the conversations you expect.
  • Decide on a label name. The script creates the label the first time it runs, so it does not have to exist yet.

Change these values

Change these values

Make the code yours before you run it.
Gmail search
from:newsletter@example.com newer_than:7d

The sender and time-window part of the search. The script automatically excludes conversations that already have your label.

Label name
From newsletter

The label added to every matching conversation. The script creates it if it does not exist.

Conversations per run
20

The largest number of conversations one run touches, which keeps the first test small.

2 · Review before running

What the script can access and change

Permissions you approve

  • Read, compose, send, and permanently delete all your email from Gmail. The code only searches for matching conversations, creates the From newsletter label when it is missing, and adds that label.

Effects of one run

  • Creates the label From newsletter if you do not already have it.
  • Adds that label to each matching conversation that does not already have it. A search returns whole conversations, which Gmail calls threads, so the label covers every message in the conversation.
  • Does not delete, archive, mark as read, move, or reply to anything.

3 · Copy

Paste this into the Apps Script editor

Create or open a script file, replace the example values marked above, then save the project.

Code.gs
const GMAIL_SEARCH = 'from:newsletter@example.com newer_than:7d'; // The sender and time-window part of the search. The script automatically excludes conversations that already have your label.
const LABEL_NAME = 'From newsletter'; // The label added to every matching conversation. The script creates it if it does not exist.
const CONVERSATIONS_PER_RUN = 20; // The largest number of conversations one run touches, which keeps the first test small.

function labelEmailsFromSender() {
  let label = GmailApp.getUserLabelByName(LABEL_NAME);
  if (!label) {
    label = GmailApp.createLabel(LABEL_NAME);
  }

  const escapedLabelName = LABEL_NAME.replace(/(["\\])/g, '\\$1');
  const searchQuery = GMAIL_SEARCH + ' -label:"' + escapedLabelName + '"';
  const threads = GmailApp.search(searchQuery, 0, CONVERSATIONS_PER_RUN);
  threads.forEach((thread) => {
    thread.addLabel(label);
  });

  Logger.log('Labeled ' + threads.length + ' conversations.');
}

4 · Run

Follow these steps in order

  1. Try the search in Gmail first

    Paste from:newsletter@example.com newer_than:7d into the Gmail search box and check the results. The script uses those same operators, then excludes conversations that already have the label.

  2. Paste and save the function

    Go to script.google.com and click New project. Replace the starter function with this one. Change the sender address in GMAIL_SEARCH to the one you picked, then save.

  3. Run one small test

    Choose labelEmailsFromSender in the function dropdown above the code, then click Run. Approve access when Google asks. The run stops after 20 conversations, even when the search matches more.

  4. Check the label in Gmail

    The label From newsletter appears in the Gmail sidebar. Open it and check that the conversations are the right ones. To label older mail, widen the search and run the function again. It skips conversations that already have the label.

5 · Confirm

How to tell it worked

The label From newsletter appears in the Gmail sidebar and holds conversations that matched the search. The Execution log shows how many previously unlabeled conversations were labeled.

If the result is different

Troubleshooting

The Execution log says "Labeled 0 conversations."
Run the same search in the Gmail search box. If Gmail finds nothing either, widen the window to newer_than:30d or check the sender address for a typo. If Gmail finds conversations but the script labels none, they already have the label.
A dialog says Authorization required.
Click Review permissions, pick your account, and click Allow. A script cannot read Gmail until you approve it.
Older mail from the same sender is not labeled.
Change newer_than:7d in GMAIL_SEARCH to a longer window, or remove it, and run the function again. The search looks only inside that window, which means the last seven days. Conversations that already have the label are skipped, so repeated runs reach older mail 20 conversations at a time.

Technical details

Names used in this script

Open these details when you want to adapt the example or look up an exact service or method name.

Show Apps Script symbols and service pages

Service pages

Keep going