hello
When I set all the necessary details i an getting this error "Access denied".
please tell me what is this error
Hello,
We just provided example of code and probably you will need to update XML API client class, you can find the latest one here:
https://github.com/CpanelInc/xmlapi-php
If you need our assistance, you may open a ticket with additional information of the problem, so we can test it out.
thank you very much it's work very good but i want to Chang password email script can you help me plz?
Hello daarkm,
I'm sorry but I'm not sure I follow. You want to change an email address password for a script or by a script? You can see more about cPanel's API here.
Please clarify your question and we'll see how we can assist further.
Fast forward to 2020 and the XML-API was eliminated from cPanel years ago. If you have a dedicated server or VPS server, you have access to the cPanel object which creates a CPANEL Class allowing the use of UAPI functions. Some of us are on shared servers and can't use those without the CPANEL Class.
I played around with a command line script I found and was able to modify it so it works. The code below provides the framework to develop a custom PHP application that will add the email forwarding address to cPanel. There is also a URL to delete a forwarded email address. There isn't much error checking in this, so those who use it in their own scripts may want to add that.
<?php
################################################## #############
# cPanel Email Forwarder Creator Version 1.0
################################################## #############
#
################################################## #############
#
# Can be used in 3 ways.
#
# 1) Sample run from browser:
# cpanel_email_forwarder.php?euser=john&edomain=site 1.com&eforward=johny@gmail.com
#
# 2) When run from browser without parameters will show entry form
#
# 3) Sample run from your script (by including):
# $REQUEST['euser'] = 'john';
#
# $REQUEST['edomain'] = 'your_domain.com';
#
# $REQUEST['eforward'] = 'johny@gmail.com';
# include("cpanel_email_forwarder.php");
#
################################################## #############
////////////////////////////////////////////////////////////////
/////////////////////// SETTINGS START ////////////////////////
////////////////////////////////////////////////////////////////
// cpanel user (the one you login to cpanel)
define('CPANELUSER','cpanel-username-here');
// cpanel password (the one you login to cpanel)
define('CPANELPASS','cpanel-password-here');
// your cpanel domain (domain name)
define('CPANELDOMAIN','your_domain.com');
// Hard Coded cPanel Session Token;
define('CPANEL_SESSION','cpsessXXXXXXX');
////////////////////////////////////////////////////////////////
/////////////////////// END OF SETTINGS ////////////////////////
////////////////////////////////////////////////////////////////
function getVar($name, $def = '') {
if (isset($_REQUEST[$name]) && ($_REQUEST[$name] != ''))
return $_REQUEST[$name];
else
return $def;
}
$cpuser = CPANELUSER;
$cppass = CPANELPASS;
$cpdomain = CPANELDOMAIN;
$cpsess = CPANEL_SESSION;
$euser=getVar('euser');
$edomain=getVar('edomain');
$eforward = getVar('eforward');
if (empty($edomain)) $edomain = $_SERVER['SERVER_NAME']; // populate curent domain
if (empty($euser) || empty($edomain) || empty($eforward)) {
echo "All fields are required to create email forwarding:<br>
<form method='post'>
Username:<input name='euser' value='$euser'> (sample: john)<br>
Domain:<input name='edomain' value='$edomain'> (sample: mysite.com)<br>
Redirect to:<input name='eforward' value='$eforward'> (sample: jimm@site2.com)<br>
<input type='submit' value='Create forwarder' style='border:1px solid black'>
</form>";
die();
}
// Create email forwarder
/*******email forward below***************/
// This Works! forward an email address
// url with variables
$url_add = "https://" . $cpuser . ":" . $cppass . "@" . $cpdomain . ":2083/" . $cpsess . "/execute/Email/add_forwarder?domain=" . $edomain . "&email=" . $euser . "@" . $edomain . "&fwdopt=fwd&fwdemail=" . $eforward;
/*******************************************/
/*******delete a forwarded email below***************/
// This Works! To delete a forwarded email
// url with variables
$url_delete = "https://" . $cpuser . ":" . $cppass . "@" . $cpdomain . ":2083/" . $cpsess . "/execute/Email/delete_forwarder?address=" . $euser . "@" . $edomain . "&forwarder=" . $eforward;
/************/
$url = $url_add;
// $url = $url_delete; // debug - can be used to delete a forwarded email
/********************/
$contents = file_get_contents($url); // execute the command and get results
echo "<h3>Contents: " . $contents . "</h3>";
/***********/
echo "<h3>URL: " . $url . "</h3>"; // this displays the URL executed
echo "Email Forwarder created: $euser@$edomain => $eforward";
Last edited by Matt; 08-14-2020 at 10:08 PM.
Brilliant share WHK, thank you!
For those of us who are less PHP savvy, is there a specific PHP version you've chosen while testing your script?
Thanks Larry - I used PHP version 7.3