MailMan API with cpanel security tokens cpsess

simonm10

Member
Aug 4, 2013
7
0
1
cPanel Access Level
Website Owner
Hi all,

My website has been using php code to manage MailMan mailing lists, to create list and set many options. Now with the update to cpanel that makes security tokens mandatory, my scripts don't work.

So the old style URLS were like this:

/https://server.host.net:2083/frontend/x3/mail/doaddlist.html

New URLs are like this:
/https://server.host.net:2083/frontend/x3/mail/doaddlist.html/cpsessXXXXXXXXXX/frontend/x3/mail/doaddlist.html

Can anyone help me to update my php code, or post their own code that does similar things? I'll post my current code below.

Thanks a lot for any help!

Simon.

PHP:
// Example simple function to create a mailmain list and set a couple of options
// doesn't work any more as not using cpsess URLs
------------------------------------------------------------------------------
function createMailingList($mailinglist,$password,$emails){

  $http = createCPanelHttpClient("https://myhost.net:2083/");
								
  $pageContents = postHttpClient($http,"https://myhost.net:2083/frontend/x3/mail/doaddlist.html", 
			  array('email' => $mailinglist,
				  'password' => $password,
				  'domain' => 'mysite.net'));

   $pos = strpos($pageContents, 'was successfully created');
		
   if ($pos === false) {
			return false;}

  // code here to set all the mailman options
  $baseMailmanUrl= "http://myhost.net/mailman/admin/".$mailinglist."_mysite.net/";
  $http = createMailmanHttpClient();
		
  // set all the mailman General options here
  $pageContents = postHttpClient($http,$baseMailmanUrl."general", 
  array('adminpw' => $password,
  'description' => $mailinglist));
  return true}

// -------------------
function createCPanelHttpClient($baseUrl){
		require_once("http.php");
	
		$http=new http_class;
		$http->follow_redirect=1;
		$http->timeout=0;
		$http->data_timeout=0;
		$http->debug=0;
		$http->html_debug=0;
		$http->user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";

		$url=$baseUrl."login/";
		$error=$http->GetRequestArguments($url,$arguments);

		$arguments["Headers"]["Referer"]=$baseUrl;
		$arguments["RequestMethod"]="POST";
		$arguments["PostValues"]=array(
				'user' => 'adminuser',
				'pass' => 'adminpassword',
				'goto_uri' => '',
				'login_theme' => 'cpanel');

    		
    		$error=$http->Open($arguments);

		if($error=="")
		{
			$error=$http->SendRequest($arguments);
			if($error=="")
			{		
				readResponse($http);
			}
		}
		
		if(strlen($error))
			echo "<CENTER><H2>Error: ",$error,"</H2><CENTER>\n";

		echo "<p>request_headers:";
		print_r($http->request_headers);
		echo "</p>";
		
		
		$http->Close();
		return $http;
	}

//-----------------------------
function createMailmanHttpClient(){
		require_once("http.php");
		$http=new http_class;
		$http->follow_redirect=1;
		$http->timeout=0;
		$http->data_timeout=0;
		$http->debug=0;
		$http->html_debug=0;
		$http->user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";

   	return $http;
	}


By the way, I was inspired to post this question by benwiggy who posted 2 years ago that he would create a PHP class to do this very thing here: http://forums.cpanel.net/f42/create-mailman-list-api-212042.html
 

simonm10

Member
Aug 4, 2013
7
0
1
cPanel Access Level
Website Owner
re: MailMan API with cpanel security tokens cpsess [old]

I was inspired to post this question by benwiggy's post from 2 years ago:

http://forums.cpanel.net/f42/create-mailman-list-api-212042.html

On my site we have created an "API" for mailman to create lists and set all the many various options, but it no longer works since cpanel was updated to introduce mandatory security tokens.

I wonder if anyone has created php code to do this, that works with the new cpsess style URL's ?

Thanks!
 

Infopro

Well-Known Member
May 20, 2003
17,113
507
613
Pennsylvania
cPanel Access Level
Root Administrator
Twitter
Re: MailMan API with cpanel security tokens cpsess [old]

Hi mods,
where did my post go? Seems to have vansihed:confused:
thanks
I've fixed the thread for you. The thread was approved, but you went back in and edited your first post to add bbcode on example links, for some reason. When you did, it put the thread back into moderation. To fix I've merged your first and second posts into one, and killed the original.



We're looking at why this is happening in some threads posted by new users. In the meantime, don't edit the first post again, or you'll put it back into moderation.
 

simonm10

Member
Aug 4, 2013
7
0
1
cPanel Access Level
Website Owner
Thanks Infopro for fixing the thread, sorry for the hassle :)

In benwiggy's post he says people can PM him if they want to see his code - is there a way to do that? Thanks again!
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
Here are the cPanel docs about making scripts work with security tokens:

Making your script work with security tokens in cPanel & WHM | cPanel Blog

Their examples are in Perl but getting an environmental variable in PHP is quite similar. You can get the token with:

Code:
$token = getenv('cp_security_token');
This token needs to sit after the hostname like so:

https://$hostname:2083$token/frontend/x3/mail/doaddlist.html

However, it looks like you are POSTing directly to the pages in cPanel rather than using their API. This means that if they update the page name or field names, your script will no longer work. In order to ensure your script works across cPanel versions, you should connect to API1 and use the Email::addlist() API1 function to add mailing lists:

Email Module Documentation

You can connect to API1 easily with the cPanel PHP XMLAPI library and the api1_query() function, if you prefer:

https://github.com/CpanelInc/xmlapi-php
 

simonm10

Member
Aug 4, 2013
7
0
1
cPanel Access Level
Website Owner
Hi Dave,

Unfortunately the cpanel mailman API doesn't offer much functionality - just adding and deleting of mailman lists, so it does not do what I need (our scripts set many options, and control the list of subscribers).

Hence we are stuck with the not very elegant method of POSTing direct to urls (which is ineligant, but has worked well for many years).

Am hoping someone else has coded something in php that I could adapt, I have struggled to write something from scratch myself.

Thanks,

Simon.
 

simonm10

Member
Aug 4, 2013
7
0
1
cPanel Access Level
Website Owner
What are the specific actions you want to take in the Mailman admin interface?
Many things, setting bounce processing options, doing bulk subscribes, setting list privacy options, pretty much any option available from the Mailman web interface, we want to set it via our script.