WebMail Pro documentation

Create mail server records automatically


In the standard use scenario, when new user logs into WebMail Pro, the product will check if there's a matching domain found among mail servers currently added to Mail Servers tab of admin interface. If such a server was found, its information will be used to log user in.

Now, let's imagine you have one WebMail Pro installation but there are several servers (for example, running cPanel) and there are hundreds of customers, each of those have their domain and email accounts hosted by one of those servers, and the domain itself points at the server too.

In such case you'd need a new mail server to be created in the database every time a user logs into email account on the domain which isn't listed among existing mail servers yet. The solution offered below allows for solving such a task.

Installation and configuration

You can find the module here and follow the instructions for adding modules; however, there's a simpler method available:

  • download the module from here;
  • create CreateMailServerPlugin subdirectory under modules directory, and place files from the directory found in the archive there;
  • copy config.json to data/settings/modules/CreateMailServerPlugin.config.json file.

Once you have the configuration file in place, you can adjust it for your particular case. Entries of the configuration file are used as a template for creating a new mail server entry. The module assumes that IMAP and SMTP host names equal the domain name. But if in your case it's, for example, mail.domainname.com, supply "mail." for both "IncomingServer" and "OutgoingServer" values.

Source code explained

The primary method of the module is onBeforeLogin. The sequence of steps performed there is:

  1. When user logs in, obtain domain part of the email address and search for a mail server entry with that domain on file.

  2. If the domain wasn't found, create an instance of MailSo ImapClient and perform IMAP authentication for the account.

  3. If IMAP authentication is successful, a new mail server is added, using configuration file of the module as a template.

Advanced use

You might wish to add more complex logic to the module - for example, to check that particular domain is actually hosted by one of your servers, to avoid the situation of outside users accessing your WebMail Pro installation.