MailMan API with cpanel security tokens cpsess
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 "
Error: ",$error,"
\n"; echo "request_headers:"; print_r($http->request_headers); echo "
"; $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; } [/PHP] 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:-
re: MailMan API with cpanel security tokens cpsess [old] Hi mods, where did my post go? Seems to have vansihed:confused: thanks 0 -
Re: MailMan API with cpanel security tokens cpsess [old] [quote="simonm10, post: 1461961">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.0 -
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! 0 -
Happy to help. :) He hasn't been active since 2011, no telling if he'll reply to you or not. Hang in there, someone will hopefully be by this thread and have a proper answer for you. :) 0 -
Here are the cPanel docs about making scripts work with security tokens: [url=http://blog.cpanel.net/making-your-script-work-with-security-tokens-in-cpanel-whm/]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: $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: [url=http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api1/ApiEmail#Email::addlist]Email Module Documentation You can connect to API1 easily with the cPanel PHP XMLAPI library and the api1_query() function, if you prefer:0 -
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. 0 -
Surprised that no-one else has done a script like this...:( 0 -
What are the specific actions you want to take in the Mailman admin interface? 0 -
[quote="cPanelKenneth, post: 1468171">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.0
Please sign in to leave a comment.
Comments
10 comments