PHPackages                             mlocati/tbfilters2gmail - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. mlocati/tbfilters2gmail

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

mlocati/tbfilters2gmail
=======================

A PHP library that allows you to parse Thunderbird rule files (msgFilterRules.dat) and easily export them to Google Gmail filters

0.3.0(5y ago)613[1 PRs](https://github.com/mlocati/tbfilters2gmail/pulls)MITPHP ^7.4 || ^8

Since Feb 9Compare

[ Source](https://github.com/mlocati/tbfilters2gmail)[ Packagist](https://packagist.org/packages/mlocati/tbfilters2gmail)[ Docs](https://github.com/mlocati/tbfilters2gmail)[ RSS](/packages/mlocati-tbfilters2gmail/feed)WikiDiscussions Synced today

READMEChangelog (2)Dependencies (2)Versions (6)Used By (0)

Thunderbird to Gmail library
============================

[](#thunderbird-to-gmail-library)

This library helps you copying Thunderbird filters to Gmail.

Introduction
------------

[](#introduction)

Thunderbird saves email filters in a file named `msgFilterRules.dat`. This library can read this file, and create automatically the required folders and filters in a Gmail account.

Requirements
------------

[](#requirements)

### Locate the `msgFilterRules.dat` file

[](#locate-the-msgfilterrulesdat-file)

You need to know where Thunderbird saves the `msgFilterRules.dat` file. In order to do that, open Thunderbird and:

1. show the menu (use the `ALT-F` keys)
2. under the `Tools` menu, choose `Account Settings`
3. in the left list, choose the `Server Settings` item under the account name
4. in the right pane, you'll see the `Message Storage` section: the folder containing the file `msgFilterRules.dat` should be the one specified in the `Local Directory` field

### Create a Gmail Service account

[](#create-a-gmail-service-account)

You need a Gmail service account in order to use this library. Here's the complete list of steps required to correctly create and configure it:

1. create a Google project
    1. go to the [Google Cloud Platform](https://console.cloud.google.com/) dashboard page
    2. [create a new project](https://console.cloud.google.com/projectcreate)
2. enable the Gmail API
    1. go to the [Google Cloud Platform](https://console.cloud.google.com/) dashboard page
    2. from the menu, choose *APIs &amp; Services* → *Library*
    3. search for *Gmail API* and enable them for the project created above
3. create a Service Account
    1. go to the [Google Cloud Platform](https://console.cloud.google.com/) dashboard page
    2. from the menu, choose *IAM &amp; Admin* → *Service Accounts*
    3. click the *+ Create service account* button
        1. in the Step 1, enter any name/description you like
        2. in the Step 2, choose the *Owner* Role (*Full access to all resources.*)
        3. in the Step 3, you can leave the default (empty) values
    4. in the Service Account list, choose the *Create key* action for the newly created service account
        1. download the key in *JSON* format
        2. save the `.json` file in a secure location
    5. in the Service Account list, choose the *Edit* action for the newly created service account
    6. in the *Service account status* section, click the *Show domain-wide delegation*
    7. check the *Enable G Suite Domain-wide Delegation* option
    8. save
4. grant the required OAuth scopes
    1. go to the [Google Admin](https://admin.google.com/) page
    2. from the menu, choose *Security* → *API Controls*
    3. in the *Domain wide delegation* section, click the *Manage domain wide delegation* link
    4. add a new API client
        1. in the *Client ID* field enter the number associated to the `client_id` key in the `.json` file you downloaded above
        2. you have to specify these *OAuth scopes* ([here](https://developers.google.com/identity/protocols/oauth2/scopes) you can find all the available scopes):
            - `https://www.googleapis.com/auth/gmail.labels`(required to manage the folders of the Gmail accounts)
            - `https://www.googleapis.com/auth/gmail.settings.basic`(required to manage the filters of the Gmail accounts)

Sample usage
------------

[](#sample-usage)

Let's assume that:

- you downloaded the JSON key for the service account to `path/to/key.json`
- the Thunderbird filters files is available at `path/to/msgFilterRules.dat`
- the email address of the Gmail account is `me@my.domain`

With the above data, you can copy the Thunderbird filters to Gmail with a few lines of code like these:

```
// 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";
}
```

Ad this point, you should have the Thunderbird filters available in Gmail.

Known issues
------------

[](#known-issues)

- It seems it's not possible to create Gmail filters with rules like `begins with`, `ends with`, `is exactly`, ... Gmail filters always search for *parts* of the text. So, for example, a Tunderbird criteria like `If the subject starts with "Some text"` will generate a Gmail filter like `If the subject contains "Some text"`.
- The "Stop Filter Execution" Tunderbird action is ignored (it seems there's no way to implement such feature in Gmail filters)
- It seems it's not possible to create Gmail filters with header-specific criterias
- Not every criteria/action has been implemented in this library. We implemented only the ones we actually needed.

How to delete all the Gmail filters and/or folders
--------------------------------------------------

[](#how-to-delete-all-the-gmail-filters-andor-folders)

If you want to test this library, you may need to delete all the existing Gmail filters and/or folders. You can do that with this code (we assume that the `$client` variable is initialized as described in the *Sample usage* section above)

```
$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());
    }
}
```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

5

Last Release

1926d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6e9d7ece045fc89575e083ee4852edf31218df403c84f41a0de01863dae982f5?d=identicon)[mlocati](/maintainers/mlocati)

---

Top Contributors

[![mlocati](https://avatars.githubusercontent.com/u/928116?v=4)](https://github.com/mlocati "mlocati (15 commits)")

---

Tags

rulesfiltersgmailthunderbirdmsgFilterRules.datmsgFilterRules

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/mlocati-tbfilters2gmail/health.svg)

```
[![Health](https://phpackages.com/badges/mlocati-tbfilters2gmail/health.svg)](https://phpackages.com/packages/mlocati-tbfilters2gmail)
```

###  Alternatives

[ruler/ruler

A simple stateless production rules engine for modern PHP.

1.1k835.4k3](/packages/ruler-ruler)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[mi-schi/phpmd-extension

Contains extra phpmd rules from clean code book and the best practices of my experiences.

40908.0k5](/packages/mi-schi-phpmd-extension)[ballen/plexity

A password complexity checker library.

10882.0k](/packages/ballen-plexity)[suin/php-cs-fixer-rules

A Rule set for PHP-CS-Fixer mainly targeting PHP 7.1 or higher.

13123.3k3](/packages/suin-php-cs-fixer-rules)[hybridlogic/validation

A simple, extensible validation library for PHP with support for filtering and validating any input array along with generating client side validation code.

641.1k](/packages/hybridlogic-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
