Skip to content

Migrating a catch-all maildir to Office 365

I have been running my own mail server at home for years using Postfix, dovecot, amavisd-new, ClamAV and SpamAssassin. But it requires a reliable connection and some maintenance occasionally. And of course, it always breaks when I am on the other side of the world.

To free myself of that burden, I decided to make the move to Office 365. I got myself a P1 subscription and started to browse through the configuration screens. The migration of an account from IMAP to Exchange Online was amazingly fast and easy.

Happy with how everything looked, felt, and connected, I was ready to make the switch.

Just before I wanted to change the MX record to point to Office 365, I double checked the configuration of my account. I discovered I could not find any way to set my account as a catch-all account. After some research I found out this is not possible at all!

Catch-all Mailbox

A catch-all mailbox receives messages sent to email addresses in a domain that do not exist. Exchange Online anti-spam filters use recipient filtering to reject messages sent to mailboxes that don’t exist, so catch-all mailboxes are not supported.

That left me 2 options:

  1. Stop the migration to Office 365 and leave things as they were.
  2. Make every email address I used in the past an alias.

I started searching if anyone has done this before. It looks like this is not the case, so seeing this as a challenge, I started working on my own solution.

Extracting all used addresses

First you need to get every email address ever used by others as a recipient.

As my bash scripting is a bit rusty, I found this script to convert mdir to mbox format by Joerg Reinhardt which I have used as base for my own script: getting email recipient addresses from maildir.

The script needs 2 parameters, the maildir directory and the email domain you want to get the aliases for.

./ <Maildir directory> <Email domain>

So, for me:

./ Maildir

This results in a long file with a lot of email addresses. To aggregate this list into a CSV file you can use the following command:

sort Maildir.dump | uniq -c | sort -k1nr | awk 'BEGIN {OFS = ";";} {print $1,$2}' > Maildir.csv

This allows you to open the file using Excel and remove all entries you don’t want to be an alias.
I counted 385 unique email addresses, too many to add manually.

Adding the aliases to a mailbox

First, I configured and connected to Exchange Online using the Windows PowerShell to the Service article.

Then, read the generated and authored CSV file.

$csv = Import-Csv D:\Maildir.csv -Delimiter ';' -Header Count, Email

Get a reference to the mailbox you want to add the aliases.

$temp = Get-Mailbox -Identity michael

Add all the email addresses to the EmailAddress property, but I discovered the last email address will become the default address. So, make sure the primary SMTP address is added last.

$temp.EmailAddresses = $temp.EmailAddresses | ? { $_ -ne "SMTP:" + $temp.PrimarySmtpAddress }
$csv | % { $temp.EmailAddresses += ("SMTP:" + $_.Email) }
$temp.EmailAddresses += "SMTP:" + $temp.PrimarySmtpAddress

Now set the EmailAddress property on the actual mailbox

Set-Mailbox -Identity michael -EmailAddresses $temp.EmailAddresses

And done!