mlocati / tbfilters2gmail
一个PHP库,允许您解析Thunderbird规则文件(msgFilterRules.dat)并将其轻松导出到Google Gmail过滤器
0.3.0
2021-03-24 08:45 UTC
Requires
- php: ^7.4 || ^8
- google/apiclient: ^2.9
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18.2
README
这个库帮助您将Thunderbird过滤器复制到Gmail。
简介
Thunderbird将邮件过滤器保存在名为 msgFilterRules.dat
的文件中。这个库可以读取此文件,并自动在Gmail账户中创建所需的文件夹和过滤器。
要求
定位 msgFilterRules.dat
文件
您需要知道Thunderbird将 msgFilterRules.dat
文件保存在哪里。为了做到这一点,打开Thunderbird并
- 显示菜单(使用
ALT-F
键) - 在
工具
菜单下,选择账户设置
- 在左侧列表中,选择账户名称下的
服务器设置
项 - 在右侧窗格中,您将看到
消息存储
部分:包含msgFilterRules.dat
文件的文件夹应该是本地目录
字段中指定的文件夹
创建Gmail服务账户
为了使用此库,您需要一个Gmail服务账户。以下是正确创建和配置它的完整步骤列表
- 创建Google项目
- 转到 Google Cloud Platform 控制台页面
- 创建一个新项目
- 启用Gmail API
- 转到 Google Cloud Platform 控制台页面
- 从菜单中,选择 APIs & Services → Library
- 搜索 Gmail API 并为创建的项目启用它们
- 创建服务账户
- 转到 Google Cloud Platform 控制台页面
- 从菜单中,选择 IAM & Admin → Service Accounts
- 点击 + Create service account 按钮
- 在第1步中,输入您喜欢的任何名称/描述
- 在第2步中,选择 Owner 角色(完全访问所有资源。)
- 在第3步中,您可以保留默认(空)值
- 在服务账户列表中,为新建的服务账户选择 Create key 操作
- 以 JSON 格式下载密钥
- 在安全位置保存
.json
文件
- 在服务账户列表中,为新建的服务账户选择 Edit 操作
- 在 Service account status 部分,点击 Show domain-wide delegation
- 选中 Enable G Suite Domain-wide Delegation 选项
- 保存
- 授予所需的OAuth作用域
- 转到 Google Admin 页面
- 从菜单中,选择 Security → API Controls
- 在 Domain wide delegation 部分,点击 Manage domain wide delegation 链接
- 添加新的API客户端
- 在 Client ID 字段中输入您在上传的
.json
文件中关联的client_id
关键字 - 您必须指定以下 OAuth作用域 (在此处 可以找到所有可用的作用域)
https://www.googleapis.com/auth/gmail.labels
(需要管理Gmail账户的文件夹)https://www.googleapis.com/auth/gmail.settings.basic
(需要管理Gmail账户的过滤器)
- 在 Client ID 字段中输入您在上传的
示例用法
假设
- 您将服务账户的JSON密钥下载到
path/to/key.json
- Thunderbird过滤器文件位于
path/to/msgFilterRules.dat
- gmail账户的电子邮件地址为
me@my.domain
有了以上数据,您可以使用以下几行代码将Thunderbird过滤器复制到Gmail
// Parse the Thunderbird filters $filters = \TBF2GM\MsgFilterRulesDatParser::fromFile('path/to/key.json')->parse(); // Initialize the Google API Client $client = new \Google\Client(); $client->setAuthConfig('path/to/key.json'); $client->setScopes([\Google_Service_Gmail::GMAIL_LABELS, \Google_Service_Gmail::GMAIL_SETTINGS_BASIC]); $client->setSubject('me@my.domain'); // Create the Gmail filters $writer = new \TBF2GM\Gmail\FilterWriter($client); $problems = $writer->ensureFilters($filters); // Let's print any error that may occur foreach ($problems as $problemIndex => $problem) { echo 'Problem ', $problemIndex + 1, (string) $problem, "\n"; }
到这一点,你应该已经在Gmail中有了Thunderbird过滤器。
已知问题
- 看起来无法使用
以...开头
、以...结尾
、正好是
等规则创建Gmail过滤器。Gmail过滤器总是搜索文本的部分。所以,例如,如果Thunderbird的条件是主题以"某些文本"开头
,那么生成的Gmail过滤器将是主题包含"某些文本"
。 - "停止过滤器执行"的Thunderbird动作被忽略(看起来在Gmail过滤器中无法实现此类功能)
- 看起来无法使用针对特定标题的准则创建Gmail过滤器
- 并非每个准则/动作都已在此库中实现。我们只实现了我们真正需要的。
如何删除所有Gmail过滤器和/或文件夹
如果您想测试此库,您可能需要删除所有现有的Gmail过滤器和/或文件夹。您可以使用此代码来完成(我们假设$client
变量已按上述示例用法部分中的描述初始化)
$service = new \Google_Service_Gmail($client); // Delete all the existing Gmail filters foreach ($service->users_settings_filters->listUsersSettingsFilters('me')->getFilter() as $filter) { $service->users_settings_filters->delete('me', $filter->getId()); } // Delete all the existing Gmail folders foreach ($service->users_labels->listUsersLabels('me') as $label) { if ($label->getType() === 'user') { $service->users_labels->delete('me', $label->getId()); } }