Add and remove email aliases (forwards) from CPanel with php (specifically on bluehost)

Web Programming 12 Comments »

In this post I will briefly describe how to add and remove email aliases/forwards using CPanel scripts in php (or any language really), specifically on bluehost web hosting.  I found other articles and scripts related to accessing CPanel, but nothing specific to bluehost.

In my searching I came across this forum post from 2008 in which a bluehost user asks if there is a way to access the information about existing email forwards from within a php script, and he is answered that there is no way to do this.  In fact, this is possible and I was able to do this on my own site earlier this week (whether or not this was possible in 2008 I can’t say, but it’s definitely possible now, although not documented anywhere at bluehost that I could find).

Information about existing email aliases are stored in a file located in “/etc/valiases/domain.com” where “domain.com” is your actual domain that you have hosted.  This information is easily found on google in regards to many different web hosts – i.e. it’s not special to bluehost.  I have the standard hosting with bluehost (nothing special) but I do have ssh terminal access.  If I log into the terminal and attempt to read the file at /etc/valiases/sg20.com it says the file does not exist.  However, I discovered that from within a php script this file is accessible, and my script was able to read and parse all existing email aliases.

At this point we could decide to write a script to simply modify the valiases file directly, or we could opt to use the existing CPanel scripts.  One site I found useful was this site: http://www.zubrag.com/scripts/cpanel-email-forwarder.php – particularly the user comments (I did not download the script from that site).

CPanel scripts are generally located in the following directory structure:

http://domain.com:2082/frontend/<theme>/mail/

CPanel uses port 2082 (possibly 2083), and your webhost probably uses a theme folder.  Some people report themes called “x”, “x2”, or “x3”.  On bluehost the theme name is simply “bluehost”.  If you want to access the CPanel scripts from php (or any other language) you will also want to include your webhost username and password.  For example:

http://username:password@domain.com:2082/frontend/bluehost/mail/

The CPanel script for adding email aliases is doaddfwd.html.  I was not able to locate any CPanel documentation specifying exactly what parameters this script takes, but the user comments at the above linked site were helpful.  My hosting uses CPanel version 11.25 at the time of this writing.  The CPanel script expects parameters:

  • email – this is simply the name of the email alias (not the domain)
  • fwdemail – the actual email (or emails) to forward the mail to
  • domain – the domain of the alias
  • fwdopt – apparently a forward “option” parameter.  This needs to be set to ‘fwd’ for a regular alias.  I’m unsure what other possible options exist.

So to put it all together, if I wanted to add an alias ‘test’ using sg20.com which forwards to ‘realemail@sg20.com’ I would do:

http://username:password@sg20.com:2082/frontend/bluehost/mail/doaddfwd.html?email=test&fwdemail=realemail@sg20.com&domain=sg20.com&fwdopt=fwd

That’s it.  CPanel will do the alias creation for you.  As for possible error conditions, I really can’t say.  As I said before, I could not find any documentation for this CPanel script.

For deleting email aliases you can use the script dodelfwd.html.  For this script the parameters need to be:

  • email – this must be your current alias address combined with the real email address using an ‘=’ – e.g. ’email=aliasemail=realemail’
  • emaildest – this is the real email address (for a second time)

Odd parameters, I know, but this is what it needs.  So to put it all together, if I wanted to delete the above alias that I previously created, I would do:

http://username:password@sg20.com:2082/frontend/bluehost/mail/dodelfwd.html?email=test@sg20.com=realemail@sg20.com&emaildest=realemail@sg20.com

Again, possible error conditions are unknown.

Using these CPanel scripts we can write our own interface for adding and removing email aliases.  CPanel also provides scripts to add and remove email accounts, which you can hopefully find information about if you are interested.  If you create your own script to conveniently manage email aliases without needing to go through your host’s CPanel interface, be sure to add your own password/validation field or password protect the directory or something.  We wouldn’t want anyone on the internet to be able to view and modify our email aliases, right?  =)

I have not posted the full script that I created to manage my email aliases, as the above information should be enough for you to write your own.  But if there is interest I will consider posting the bulk of my php script.

© Phillip Hansen | Original theme by N.Design Studio
Entries RSS Comments RSS Log in