Skip to content

Task guide

Label emails from one sender in Gmail

Search your mailbox with GmailApp.search() for messages from one sender address, then call thread.addLabel() so those conversations are easy to find later.

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 the Gmail conversations that match your search.
  • Create Gmail labels and add them to conversations.

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 safely

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, choose New project, replace the starter function with this one, and save.

  3. Run one small test

    Choose labelEmailsFromSender and press Run. Approve the 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 confirm the conversations are the right ones. To handle 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 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 may already have the label.
A dialog says Authorization required.
Choose Review permissions, pick your account, and allow the access. A script cannot read Gmail until you approve it.
Older mail from the same sender is not labeled.
The search only looks inside the time window you set. newer_than:7d means the last seven days. Change it, or drop it, and run the function again. Conversations that already have the label are skipped, so later small runs can reach older mail.

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