Skip to content
/

Migrating a catch-all maildir to Office 365

I’ve 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 once in a while. And of course it always breaks when I’m 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 very 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 couldn’t find a 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 by Joerg Reinhardt which I 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-dump.sh <Maildir directory> <Email domain>

So for me:

$ ./maildir-dump.sh Maildir hompus.nl

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 article.

First we will read the CSV file we have generated and authored.

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

Then you need to get a reference to the mailbox you want to add the aliases to.

$temp = Get-Mailbox -Identity michael

You need to add all email addresses to the EmailAddress property, but I discovered the last email address to be added 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 you only need to set the EmailAddress property on the actual mailbox

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

And done!

3 Comments

  1. /

    Hi Michaël,

    I think if you use smtp: instead of SMTP: for all of the addresses except for one, then the one specified with SMTP: will be taken as the primary address.

    Not sure if this works with Office 365 as I haven't tested it, but that is the way it works with Exchange on-premise.

    Regards,

    Sam

    • /

      PS - Your method is also correct, I only meant to provide additional information.

  2. /

    There is another way you could achieve a catch-all, without too much difficulty. The only downside is that you have to remember what you've setup if you ever add any more Office365 users.

    Check out this entry on our website:
    http://www.lansalot.co.uk/index.php?option=com_content&view=article&id=101

    --Jon

Leave a comment