SOLVED Delete Email Forwarding Account with xmlapi.php

BillSm

Member
Jun 1, 2018
6
2
3
US
cPanel Access Level
Website Owner
Hi,

I am using this code posted by baldarab, Mar 19, 2014 to create a forwarding account. It works great.

But now I am trying to use the same code to delete a forwarding account, but can't find the correct syntax on the forums.

Tried: delforward, dodelfwd (in place of addforward) but neither works.

$result_forward = $xmlapi->api2_query($account, 'Email', 'addforward', array($email_domain, $email_user, 'fwd', $dest_email) );
?>

Any ideas?



<?php
include 'xmlapi.php' ;

$ip = 'nnn.nnn.nnn.nnn';
$root_pass = 'pppppppp';
$account = 'aaaaaa';
$email_user = $_POST['Username'];
$email_password = $_POST['EmailPassword'];
$dest_email = $_POST['ParentEmail'];
$email_domain = 'domain.com';
$email_quota = '10';
$xmlapi = new xmlapi($ip);
$xmlapi->set_port(2083);
$xmlapi->password_auth($account,$root_pass);
$xmlapi->set_output('xml');
$result = $xmlapi->api1_query($account, 'Email', 'addpop', array($email_user, $email_password, $email_quota, $email_domain) );
$result_forward = $xmlapi->api2_query($account, 'Email', 'addforward', array($email_domain, $email_user, 'fwd', $dest_email) );
?>​
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463

BillSm

Member
Jun 1, 2018
6
2
3
US
cPanel Access Level
Website Owner
Hello @BillSm,

The corresponding cPanel API 2 function to delete a forward is documented at:

cPanel API 2 Functions - Email::delforward - Developer Documentation - cPanel Documentation

You'll need to update the code to use the email and emaildest parameters for this function.

Thank you.
Hi Michael, thanks for the reply.

I looked at the link and modified the code to:

$result_forward = $xmlapi->api2_query($account, 'Email', 'delforward', array(address => $address_to_delete, forwarder => $dest_email) );

But the address is still not deleted. Error logged was:
Failed to delete forwarder. Unable to locate the forwarder “&quot;&quot;” for account “” on domain “mydomain.com”.

Also tried with expilicity entering the address and forwarder emails instead of using variables. Same error.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
Also tried with expilicity entering the address and forwarder emails instead of using variables. Same error.
Are you sure the forwarder you attempted to delete exists under the associated cPanel account?

Thank you.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
Hello,

Try deleting the forwarder via the command line to test that it works well. EX:

Code:
cpapi2 --user=username Email delforward email=user%40example.com emaildest=fwdtome%40example.com
This will help determine if the issue is with your custom script, or with the API function itself.

Thank you.
 

BillSm

Member
Jun 1, 2018
6
2
3
US
cPanel Access Level
Website Owner
Looking at your command line code, I changed it from:

$result_forward = $xmlapi->api2_query($account, 'Email', 'delforward', array(address => $address_to_delete, forwarder => $dest_email) );

To:

$result_forward = $xmlapi->api2_query($account, 'Email', 'delforward', array(email => $address_to_delete, emaildest => $dest_email) );

And it deleted correctly.

Thanks so much for your help!
 
  • Like
Reactions: cPanelMichael

BillSm

Member
Jun 1, 2018
6
2
3
US
cPanel Access Level
Website Owner
Is there a way to show the status of a process? Like if the deletion or creation was successful or failed? Other than in an error log so I can see the status in the browser and display it?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,880
2,258
463
If I want to delete a pipe forwarder, is emaildest parameter "|script/path" ?
Hello @veronicabend,

The emaildest parameter should be the path of the script if the destination is a pipe to a program. It needs to be in quotes and encoded. For example, you'd use a command like this if the path was /home/username/public_html/test.php :

Code:
uapi --user=username Email delete_forwarder address=test5%40domain.com forwarder="%7c%2fhome%2fusername%2fpublic_html%2ftest.php"
The list_forwarders UAPI function is useful if you want to see the specific uri_forward destination to use with the emaildest parameter when deleting a forwarder:

Code:
uapi --user=username Email list_forwarders domain=domain.com
Thank you.