Skip to content

Instantly share code, notes, and snippets.

@alexkuang0
Created December 29, 2020 14:33
Show Gist options
  • Save alexkuang0/2ee802041f1d807dd94f0b5401c473db to your computer and use it in GitHub Desktop.
Save alexkuang0/2ee802041f1d807dd94f0b5401c473db to your computer and use it in GitHub Desktop.
Apps Script for Gmail - Label mails by address
const PARENT_LABEL = 'example.com'
const LABEL_SEPARATOR = '-'
const LEVEL_SEPARATOR = '.'
const getLabelOrCreate = (labelName) => GmailApp.getUserLabelByName(labelName) || GmailApp.createLabel(labelName)
function labelMailsByAddress() {
const allTargetThreads = GmailApp.search(`label:${PARENT_LABEL} `)
allTargetThreads.forEach(thread => {
if (thread.isUnread()) {
Logger.log(`SUBJECT: ${thread.getFirstMessageSubject()}`)
const toAddress = thread.getMessages()[0].getTo()
const localPart = /(.*)@/g.exec(toAddress)[1]
localPart.split(LABEL_SEPARATOR).forEach(labelName => {
const allLabelParts = labelName.split(LEVEL_SEPARATOR)
const currentLabelParts = [PARENT_LABEL]
allLabelParts.forEach(labelPart => {
let currentLength = currentLabelParts.push(labelPart)
let fullLabelName = currentLabelParts.join('/')
let currentLabel = getLabelOrCreate(fullLabelName)
if (currentLength === allLabelParts.length + 1) {
thread.addLabel(currentLabel)
Logger.log(`ADDED LABEL: ${fullLabelName}`)
}
})
})
}
})
}
@alexkuang0
Copy link
Author

alexkuang0 commented Dec 30, 2020

English | 简体中文

Instructions

This script is inspired by 0x4447/0x4447_product_s3_email. It achieves similar automation of organizing mails in Gmail.

Prerequisites

  • A Gmail account
  • A domain name

Usage

Suppose that your domain name is example.com.

  1. Create a filter that automatically apply a parent label for emails sent to *@example.com. (Default label: example.com, you can customize by modifying the value of PARENT_LABEL)
  2. Forward all emails sent to *@example.com to your Gmail. (My choice of email forwarding provider is ImprovMX).
  3. Create a new project in Google Apps Script, paste the code and authorize the script to access your Gmail.
  4. Set up a trigger to periodically execute the script.
    • ⚠️ Note that Google does not provide an event-based trigger, and limits the minimal triggering interval to 1 minute)

Examples

Find all emails in the parent label.

  • Mails sent to [email protected] can be found under sublabel newsletter.
  • Use - to separate multiple labels. i.e. Mails sent to [email protected] can be found under sublabels newsletter and github.
  • Use . to create hierachy. i.e. Mails sent to [email protected] can found under sublabel accounts > github.

Configurations

  • PARENT_LABEL: You can customize this as long as it is consistent with the parent label you manually set up in Gmail.
  • LABEL_SEPARATOR and LEVEL_SEPARATOR:
    • Valid seperators: !#$%&'*+-/=?^_`{|}~.
    • ⚠️ Note that some characters, although valid as a part of an email address, may not be well supported by certain mail providers, may not pass certain email address validations, or may have a higher chance being deemed as spammer.

@alexkuang0
Copy link
Author

alexkuang0 commented Dec 30, 2020

English | 简体中文

如何使用

此脚本灵感来源于 0x4447/0x4447_product_s3_email,在 Gmail 中实现了类似的自动整理邮件。

前提条件

  • 拥有一个 Gmail 账户
  • 拥有一个域名

如何使用

假设你的域名是 example.com

  1. 在 Gmail 中,创建一个过滤器以自动将所有发送至 *@example.com 的邮件打上一个父标签(默认父标签名: example.com ,你可以通过改变 PARENT_LABEL 的值来自定义)
  2. 将所有发送至 *@example.com 的邮件转发至你的 Gmail(我选择的邮件转发服务提供商是 ImprovMX)。
  3. Google Apps Script 中新建一个项目(Project),粘贴这个脚本,并授权其访问你的 Gmail
  4. 设置一个触发器(Trigger)以周期性地执行脚本
    • ⚠️ 注意:Google 未提供基于事件的触发器,且最小触发间隔为 1 分钟。

示例

在父标签中找到所有邮件

  • 发送至 [email protected] 的邮件会出现在子标签 newsletter
  • - 分隔多个标签。例如,发送至 [email protected] 的邮件会出现在子标签 newslettergithub
  • . 创建层级。例如,发送至 [email protected] 的邮件会出现在子标签 accounts > github

配置

  • PARENT_LABEL: 你可以自定义这个参数,保持其与你在 Gmail 中手动创建的父标签一致即可。
  • LABEL_SEPARATORLEVEL_SEPARATOR:
    • 可用的分隔符: !#$%&'*+-/=?^_`{|}~.
    • ⚠️ 注意:某些字符尽管可用于电子邮件地址,但可能不被某些邮件服务商支持,可能通过不了某些电子邮件地址验证,或是更有可能被当作垃圾邮件

@hellodword
Copy link

非常好,但域名邮箱似乎或多或少都有一些问题,或许可以稍微改一改,例如:

利用 gmail 的 dots addresses,然后可以想个简单的办法创建/记录对应关系

[email protected] - accounts
[email protected] - news
...

@alexkuang0
Copy link
Author

@hellodword 是的,Gmail 地址还可以可以用 + 号扩展(发给 [email protected] 的邮件会发送到 [email protected]),有时间写个给用在这种情况下的。不过不知道你说的域名邮箱的问题大概有什么,我目前只打算用这个收邮件,用 ImprovMX 转发,都有免费额度,感觉体验还行暂时没有遇到什么问题。

@hellodword
Copy link

@hellodword 是的,Gmail 地址还可以可以用 + 号扩展(发给 [email protected] 的邮件会发送到 [email protected]),有时间写个给用在这种情况下的。不过不知道你说的域名邮箱的问题大概有什么,我目前只打算用这个收邮件,用 ImprovMX 转发,都有免费额度,感觉体验还行暂时没有遇到什么问题。

比如注册时的接受度...总会遇到一些只接受主流邮箱后缀却又不得不注册的场景

@alexkuang0
Copy link
Author

@hellodword 是的,Gmail 地址还可以可以用 + 号扩展(发给 [email protected] 的邮件会发送到 [email protected]),有时间写个给用在这种情况下的。不过不知道你说的域名邮箱的问题大概有什么,我目前只打算用这个收邮件,用 ImprovMX 转发,都有免费额度,感觉体验还行暂时没有遇到什么问题。

比如注册时的接受度...总会遇到一些只接受主流邮箱后缀却又不得不注册的场景

确实,不过 Gmail 的加号也有这个问题

@alexkuang0
Copy link
Author

alexkuang0 commented Aug 28, 2023

This gist has some bugs and has been discontinued. Following updates will be in the repo here: https://github.com/alexkuang0/mail-organizer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment