Building a hosted mailserver serviceLast edited on Sep 22, 2012

I guess that with the new buzzwords today we would call this "cloud service" or mailbox in the cloud. What it is really: a service similar to any big providers like GoDaddy or DomainPeople that hosts a mail server for you and let you create email addresses and mailboxes hosted on their servers. Except I wanted to do it myself and offer this type of service to other people. Well not really... I just wanted to create the service but I don't want any customers. It's just a proof of concept. So my server will host mailboxes and email forwarding services for people who already own a domain name and want to have their MX record point to my server. My server also offers a web page to let users manage their email addresses. Oh, and this is all done on an Amazon EC2 virtual machine. So guaranteed uptime.

So in another post (I am now hosting my own DNS and Mail servers on Amazon EC2) , I talked about installing postfix on an EC2 VM. Now I wanna offer my users a way to create mailboxes and forward addresses by themselves instead of having to call me every time to manage their domain. As I said in the other post, a mysql server integration with postfix is probably a good idea for this type of setup. First thing to do is to install the mysql server. I won't go into details here as this is widely covered on the internet. I did a search for "ec2 mysql" to see if there was any special considerations to take since it was my first installation on a non-slackware distribution and I found this: http://www.samstarling.co.uk/2010/10/installing-mysql-on-an-ec2-micro-instance/

After you have a functional mysql server that will automatically run after a reboot, you should create a database for the mail config and create a mysql user that has access to it.

in main.cf:
    virtual_mailbox_domains = mysql:/etc/postfix/vdomains-mysql
    virtual_mailbox_maps = mysql:/etc/postfix/vmbox-mysql
    virtual_alias_maps = mysql:/etc/postfix/valias-mysql
in mysql:
    create database dbmail;
    create user 'mysqlmailuser'@'localhost' IDENTIFIED BY 'pass123';
    grant all privileges on dbmail.* to 'mysqlmailuser'@'localhost' with grant option;
    create table owners
        owner VARCHAR(255),
        pass VARCHAR(255),
        PRIMARY KEY(id)

    create table domains
        domainkey VARCHAR(255),
        domain VARCHAR(255),
        owner INT not null,

    create table aliases
        alias VARCHAR(255),
        fwd VARCHAR(255)

    create table mailbox
        username VARCHAR(255),
        mbox VARCHAR(255)
    user = mysqlmailuser
    password = pass123
    dbname = dbmail
    query = SELECT domain FROM domains WHERE domainkey='%s'

    user = mysqlmailuser
    password = pass123
    dbname = dbmail
    query = SELECT fwd FROM aliases WHERE alias='%s'

    user = mysqlmailuser
    password = pass123
    dbname = dbmail
    query = SELECT mbox FROM mailbox WHERE username='%s'

I'm not sure why, but when adding/removing an entry in the "domain" table, changes don't get picked up instantly by postfix. You don't need to "postfix reload" but it could take a minute or two. Changes to the alias and mailbox tables are reflected right away though.

Letting users do it themselves

I wanted to provide a way of having different user accounts on the server. Each user owns a different subset of domains that are registered somewhere on some registrar. The users would have set the MX record of their domain to point towards my server. Or they could use my name server and I could setup their MX record but that is a different story. I just need an automated way for the users to add their domain in the list of domains for which postfix accepts email from. I also want users to create their aliases/forwards and mailboxes. This will be done through a web page on which each users will have to login. After they are logged on, a list of their domains will appear and they have the choice of adding/remove domains. When they will click on a domain, they will be able to manage their mailboxes and forwards.

So all this web application does is to add/remove domains, aliases and mailboxes from the mysql tables that postfix uses. Since the changes are in a mysql server, the changes will be immediately available to postfix, giving instant control to the users on their virtual mail server.

Note that this code does not produce a very nice web page layout, it just does the work. A bit more a validation could also be done to improve robustness. It is more of a proof of concept than a real design. Here is the code: ww.zip