Visualizzazione post con etichetta email. Mostra tutti i post
Visualizzazione post con etichetta email. Mostra tutti i post

domenica 4 gennaio 2015

[bash CPanel] hostmonster / bluehost email accounts and forwarders bulk management

Hostmonster and Bluehost give you the possibility to bulk add email accounts and forwarders loading a preformatted CSV. Ok, but I have hundreds and I need to bulk add, delete, migrate, etc etc.

I came to several websites stating that something like this would have worked:

http://username:password@domain.com:2082/frontend/hostmonster/mail/dodelfwd.html?bla bla bla

But you can safely replace the frontend theme "hostmonster" with "x3" (default CPanel theme) or "bluehost" or your custom theme. Perhaps it worked a long time ago, or it works in a non chrooted environment. I own a shared hosting, so I don't have access to sudo, nor to /etc/valiases, neither to other special folders, so the classic sysadmin ways are not applicable for me.

Bluehost / hostmonster have a landing page on which you need to authenticate, then it generates a CPanel session (you can see it in the url "cpsess" as soon as you authenticate) and you just can't perform this from outside without spending days on curl / phantomjs etc. Another time wasting method would be the CPanel XML API authentication, which wouldn't work in this case due to the special Bluehost double authentication mentioned above.

Life is easier... It's not possible to handle this method from outside, but it's perfectly working from localhost bash. Thus you simply need to login via SSH in order to perform manual commands or upload a script in order to handle this automatically.

SSH must be enabled on CPanel. Login via SSH and try yourself:

curl http://youruser:yourpass@127.0.0.1:2082
\/json-api/cpanel?cpanel_jsonapi_user=repeatyouruser
\&cpanel_jsonapi_version=2\&cpanel_jsonapi_module=Email
\&cpanel_jsonapi_func=delforward\&email=yourforwardercompleteemail
\&emaildest=yourrecipientcompleteemail

Please note that I needed to escape the & in order for curl to work on bash, or it will interprete the & as a process fork and puts curl in background. The new API calls can be found here:

https://documentation.cpanel.net/display/SDK/cPanel+API+2+Functions+-+Email%3A%3Aaddforward

Read it carefully, you can now perform almost any kind of operations e.g. giving your cuatomers a "subpanel" in which perform almost any task with your (hidden) credentials. Please note that if you miss something, you will always get return 1 (should be = ok), so please carefully refer to the documentation. In my example, addforward returns the submitted email address unless you mispell some parameter or miss something. It only gives error if you completely omit all the parameters.

Do you like to use their PHP APIs instead? No problem, download it from:
https://github.com/CpanelInc/xmlapi-php

Here's the above example translated into PHP API call:

$xmlapi = new xmlapi("127.0.0.1");
$xmlapi->password_auth("youruser","yourpass");

$xmlapi->set_output("json"); //it could be xml
$xmlapi->set_port(2082);
$xmlapi->set_debug(1);

$acct = array(
email => "aaa@yourdomain.com",
domain => "yourdomain.com",
fwdopt => "fwd",
fwdemail => "you@ yourdomain.com"
 );

print $xmlapi->api2_query( "repeatyouruser", "Email", "addforward", $acct );

giovedì 1 gennaio 2015

[bash CPanel] hostmonster / bluehost mail migration between customers

I have a reseller plan bound to a company which was sold. I need to migrate some domains from that plan to another reseller plan I opened for my personal stuff.

Hostmonster / Bluehost asks 100$ for a domain migration (5 domains, 20 email folders).

I have all IMAP folders, so I arranged to do it myself and save that bucks. It's just a matter of recursively merging remote directories. You can also merge mailboxes, e.g. put info@aaa.com into johndoe@aaa.com etc etc.

SSH must be enabled on both CPanels.

  1. Unassign your domain from the old plan: it states that emails will be deleted... sure but it wont delete nor the corrsponding folders, neither the web server folders, so don't panic.
  2. Assign your domain to the new plan, it only asks you the old domain password.
  3. Enter the old SSH and type
rsync -Purva --ignore-existing --progress /home2/olddomain/mail/olddomain.com/oldaddress/ newdomain@ newdomain.com:/home2/newdomain/mail/newdomain.com/newaddress

-P enables the --ignore-existing directive
-u saves the the newer file in case of name conflict (impossible, however it's healtier to enable it)
-r recursive
-v verbose, redunded with --progress however healtier than only one
-a archive, so recursive and safer, ok I admit that I redunded a lot but it actually works.

The source folder is intentionally trailed with a / in order to retain the paths.

You could also do it for a whole domain...

rsync -Purva --ignore-existing --progress /home2/olddomain/mail/olddomain.com/ newdomain@ newdomain.com:/home2/newdomain/mail/newdomain.com

Better save your money and offer me a pizza :-)