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
- Gmail search
from:newsletter@example.com newer_than:7dThe sender and time-window part of the search. The script automatically excludes conversations that already have your label.
- Label name
From newsletterThe label added to every matching conversation. The script creates it if it does not exist.
- Conversations per run
20The 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.
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
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.
Paste and save the function
Go to script.google.com, choose New project, replace the starter function with this one, and save.
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.
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
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Log new emails to a Google Sheet
Search your recent mail and add one row per message to a tab named Email log. A hidden ID column keeps a second run from adding the same message twice.
- Task guide Save labeled Gmail attachments to Drive
Choose the email threads yourself by adding a Gmail label, then copy their attachments into an existing Drive folder.
- Object reference Gmail thread
A GmailThread holds one Gmail conversation. Reach threads with GmailApp.search() or label.getThreads(), then call getMessages() or addLabel().
- Object reference Gmail label
A Gmail label is a tag you can apply to conversations. A script can look one up by name, and can add or remove it from threads.
- Google app Gmail
GmailApp scripts can send messages, search threads, apply labels, and read attachments after you approve Gmail access for the project.