Dan Ward Follow me on Twitter View my LinkedIn profile Subscribe to my news feed   

A self-confessed geek and web developer

 

Blog: Automatically append username domain based on current host with RoundCube Webmail

From time-to-time, I'll have a brainwave while trying to figure out something tech-oriented; well, either that or I just want to get something off my chest. Anyhow, if I get round to typing it up, it'll wind up here in the blog.

Created by dan on Saturday 13th March, 2010 at 3:52 PM
Tags: PHP, Web development

For a year now, I've been using the server drop down with RoundCube Webmail to append the domain to the username, meaning instead of having to enter 'person@domain.net' username field, I can just enter 'person' and the password. Having to choose the server each time from below the password box has become a bit of a pain, so a little looking around bought me to come up with a little script to take care of the server selection for me.

First of all, (all in <roundcube_root>/config/main.inc.php) I removed the server selection box entirely by setting the '$rcmail_config['default_host']' setting to a string containing my web server address. There were a couple of other settings, which relied on this option, one of which will be replaced and the other I left as it was.

So comment out or remove your '$rcmail_config['username_domain']' setting and plonk the below code in its place to handle appending the domain automatically:

$webmail_subdomains = array('webmail', 'w', 'mail');
if (preg_match('/^(' . implode('|', $webmail_subdomains) . ')\.(.*)$/', $_SERVER['HTTP_HOST'], $domainmatches)) {
	$rcmail_config['username_domain'] = $domainmatches[2];
} else {
	$rcmail_config['username_domain'] = '';
}

Simply put, any domains, which begin with the subdomains in '$webmail_subdomains' have the remaining hostname components set as the 'username_domain'. If the subdomains in '$webmail_subdomains' are not matched, the 'username_domain' will be kept empty, requiring the user to provide their full email address if they wish to sign in.

Looking at ticket #1484507 on Trac for RoundCube bought me to this solution, so credits to the author of the content at the below link:
http://trac.roundcube.net/ticket/1484507