Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new email account from php script on dovecot/postfix

I'd like to make a page to allow people to create new email accounts on a dovecot/postfix server (via imap).

I've seen the php functions imap_open and imap_createmailbox, but these functions don't create accounts, it only creates a new directory :/ (and you need the login/password of an existing account to use imap_open...)

So I'd like to know if it's possible to do this :).

Edit

I'm adding this because my previous message wasn't specific enough. I need a front page to allow the users to register a new mailbox and to check if their wanted mail alias is free or not. That's why most of the web-based admin are not good for me. I don't want the end-users to see the admin panel.

like image 1000
banibanc Avatar asked Feb 14 '23 18:02

banibanc


1 Answers

By default postfix & dovecot get their users from the system. This is /etc/passwd on a UNIX/Linux box.

Managing this from a PHP script is possible, but not too pretty.

  • You can use posix_getpwnam to check if a username exists, and get information about this user.

  • Adding users is best done with useradd(8) on Linux. You will need to use exec() to launch this shell utility. Example usage might be:

    $pw = crypt($_POST['password'], '$6$1234567890123456'); # This should be 16 characters of random salt
    exec(sprintf('useradd --groups mailuser --no-user-group --shell nologin --password %s', escapeshellarg($pw));
    

I wouldn't really recommend this, though.

What I would recommend, is storing your users in a SQL database, such as PostgreSQL, MySQL, or SQLite. This is fairly easy to set up, and on the PHP side all you have to do is add/remove/update rows on the database. This is probably the best solution since it's not too complex, yet still fairly flexible.
The postfix page has a few "HOWTO's" on the subject, and so does dovecot.

Aa a final option, there's LDAP. LDAP is more complex, but arguably 'better' & more flexible. PHP has a LDAP interface.

like image 174
Martin Tournoij Avatar answered Feb 17 '23 01:02

Martin Tournoij