DreamHost API Panel


/**
*
* Ok I suck at phpdoc.org but it might be useful to use it!
*
* Thanks boldmanuk for
* http://discussion.dreamhost.com/showthreaded.pl?Board=forum_programming&Number=59785
*
* This file needs two more variables to work : $dh_username and $dh_password, your credentials to the DreamHost Panel.
*
* It is a work in progress, please contribute !
*
* @package dh-api
*/
/**
* This is the email pattern to check for valid email address.
* To use in a regex, wrap into special characters (#) and optionnaly add "^" and/or "$".
* @var string
*/
$dhapi_emailPattern="+(\.+)*@+(\.+)*(\.[a-z]{2,3})";



/**
* This var (array) contains the fake browser's headers.
* "Standard browser header."
* @var array
*/
$dhapi_headerArray=array();
$dhapi_headerArray['Host'] = "panel.dreamhost.com";
$dhapi_headerArray['Accept'] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$dhapi_headerArray['Accept-Language'] = "en-us,en;q=0.5";
$dhapi_headerArray['Accept-Encoding'] = "gzip,deflate";
$dhapi_headerArray['Accept-Charset'] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$dhapi_headerArray['Keep-Alive'] = "300";
$dhapi_headerArray['Connection'] = "keep-alive";



/**
* User-agent identifier.
* @var string
*/
$dhapi_userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7 with dh-api (http://api.dreamhosters.com/)";



/**
* This var (string) stores the first cookie.
* "I'm using two cookie jars, one for before the login, one for after. Not sure if this is neccessary however."
* note: not sure if it's "mixed"
* @var string
* @name $dhapi_cookieJar1
* @todo Check if the second one is absolutely necessary.
*/
$dhapi_cookieJar1 = tempnam($_SERVER['DOCUMENT_ROOT']."/admin",'cookie1');



/**
* This var (string) stores the second cookie.
* note: not sure if it's "mixed"
* @name $dhapi_cookieJar2
* @todo Check if this is absolutely necessary.
*/
$dhapi_cookieJar2 = tempnam($_SERVER['DOCUMENT_ROOT']."/admin",'cookie2');



/**
* This function output eventuals cURL errors.
* @param mixed $chan This is the result of curl_init().
*/
function dhapi_showCurlErrors($dhapi_curlhan) {
if (curl_errno($dhapi_curlhan) != 0) {
print "Last cURL error : ".curl_errno($dhapi_curlhan)."\n";
print "Last cURL error : ".curl_error($dhapi_curlhan)."\n";
}
} // dhapi_showCurlErrors()



/**
* This function takes the HTML of the page and returns the security hash contained in the hidden form field "security key".
* @param string $dhapi_page The HTML of the page returned by cURL.
* @return mixed The security_key found in the HTML of the page.
* @todo Maybe check that the key was found, and if not return false to break the rest?
*/
function dhapi_getSecurityKey($dhapi_page) {
$code = preg_match("#name=\"security_key\" value=\"(.{32,32})\"#", $dhapi_page, $matches);
return $matches[1];
} // dhapi_getSecurityKey()



/**
* This function disconnects from the panel (deletes the cookie associated with the connection).
*/
function dhapi_dhapi_panelDisconnect() {
global $dhapi_cookieJar2;
unlink($dhapi_cookieJar2);
// exit;
} // dhapi_panelDisconnect()



/**
* This actually connects to the panel. Three steps:
*

    *
  1. connect to the start page and gets the cookies,
  2. *
  3. send credentials to authenticate and get the panel,
  4. *
  5. reget the panel, in case of loss (...?)
  6. *
* @return bool True or false whether connection succeeded (true) or failed (false).
*/
function dhapi_dhapi_panelConnect() {
global $dh_username, $dh_password;
global $dhapi_headerArray, $dhapi_userAgent;
global $dhapi_cookieJar1, $dhapi_cookieJar2;
# start session on index/login page
$dhapi_ref_url = "";
$dhapi_url = "https://panel.dreamhost.com/";

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEJAR, $dhapi_cookieJar1);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors($dhapi_curl);
curl_close($dhapi_curl);

$dhapi_post = "Nscmd=Nlogin&username=$dh_username&password=$dh_password";

# Submit login request
$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi";

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar1);
curl_setopt($dhapi_curl, CURLOPT_COOKIEJAR, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 1);
curl_setopt($dhapi_curl, CURLOPT_POSTFIELDS, $dhapi_post);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
unlink($dhapi_cookieJar1);

# Resubmit the index page request - often get a blank response from the login page. This makes sure the session is legit
$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi";

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);

$success = strpos($dhapi_page, "Welcome to your DreamHost Control Panel!");
if ($success>0) { return true; } else { return false; }
} // dhapi_panelConnect()



/**
* This function creates a new email account. Steps:
*
    *
  1. Get the "Create New Email Address" page to extract the security_key.
  2. *
  3. Post the variables to the "?" page.
  4. *
  5. Extract from the result page "error" or "success"
  6. *
* @param string $email (input "text") The email address, like user@example.com. The hashing is done inside the function.
* @param string $redirect (input "textarea") The email address(es) to redirect the new one to, or empty (""). One or many line-separated addresses (NOTE: is this \\n, \\r, mix?).
* @param mixed $redirect_only (input "hidden") Boolean "false" creates a normal inbox (also with forward capabilities) ; boolean "true" creates a forward-only email-address (no actual inbox is created) ; string "null" creates a "garbage email address. Warning : Last two value dump gecos and other values...
* @param mixed $name (input "text") The "name" (ex: John Doe), facultative.
* @param string $pass (input "text") The password for this account. It is used twice, so have it confirmed/checked before. Min 6 chars. TODO: Max?
* @param mixed $limMB (input "text") Limit value of disk usage (in MB or empty), or "false".
* @param bool $warning (input "checkbox") The "email me if my usage is within 10% of my limit (or 100MB, whichever is smaller)".
* @param int $rem_qty (input "text") Remove read messages when inbox reaches this message quantity (max 2000). Set to false for 0. NOTE: MIN:0, MAX:2000.
* @param int $rem_age (input "text") Remove read messages older than this numbers of days (max 9999). Set to false for 0. NOTE: MIN:0, MAX:9999.
* @param bool $rem_new (input "checkbox") Remove even unread messages.
* @param mixed $rem_fol (input "text") Save removed messages in this folder. Leave empty or set to false to really deleted them. NOTE: This will be formatted with imap_utf7_encode() so warn the user if input does not match formatted.
* @param bool $rem_mai (input "checkbox") Email me when messages are removed.
* @return bool True If the email was created, false otherwise.
*/
function dhapi_panelEmailCreate($email, $redirect, $redirect_only, $name, $pass, $limMB, $warning, $rem_qty, $rem_age, $rem_new, $rem_fol, $rem_mai) {
global $dhapi_headerArray, $dhapi_userAgent, $dhapi_url;
global $dhapi_cookieJar2;

# ask for the "Create New Email" page and get the security_key
$dhapi_ref_url=$dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?tree=mail.addresses¤t_step=Index&next_step=New";
$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
$dhapi_skey = dhapi_getSecurityKey($dhapi_page);

# post vars after they are created
$mail=split("@", $email);
$alias=$mail[0];
$domain=$mail[1];
if ($limMB) { $enable_quota="on"; $hard_quota=intval($limMB); }
if (!$warning) { $notify_disk=""; } else { $notify_disk="on"; }
if ($rem_qty) { $max_messages=intval($rem_qty); } else { $max_messages="2000"; }
if ($rem_age) { $days=intval($rem_age); } else { $days=9999; }
if ($rem_new) { $rotate_new="on"; } else { $rotate_new=""; }
if ($rem_fol) { $archive="on"; $archive_folder=imap_utf7_encode($rem_fol); }
if ($rem_mai) { $notify="on"; } else { $notify=""; }

$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?";

if ($redirect_only==="null") {
$dhapi_post = "tree=mail.addresses¤t_step=New&next_step=NewFinal&security_key=$dhapi_skey&dest=null";
$dhapi_post.= "alias=$alias&domain=$domain";
}
elseif ($redirect_only==true) {
$dhapi_post = "tree=mail.addresses¤t_step=New&next_step=NewFinal&security_key=$dhapi_skey&dest=forward";
$dhapi_post.= "&alias=$alias&domain=$domain&addresses=$redirect";
}
elseif ($redirect_only==false) {
$dhapi_post = "tree=mail.addresses¤t_step=New&next_step=NewFinal&security_key=$dhapi_skey&dest=forward&mailbox=1"; // basic from hidden
$dhapi_post.= "&alias=$alias&domain=$domain&gecos=$name&random=&password1=$pass&password2=$pass"; // basic login
$dhapi_post.= "&enable_quota=$enable_quota&hard_quota=$hard_quota¬ify_disk=$notify_disk&addresses=$redirect"; // advanced login, archive and forward
$dhapi_post.= "&max_messages=$max_messages&days=$days&rotate_new=$rotate_new&archive=$archive&archive_folder=$archive_folder¬ify=$notify"; // tidy inboxes...
}

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 1);
curl_setopt($dhapi_curl, CURLOPT_POSTFIELDS, $dhapi_post);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
// echo "
$dhapi_page
";
// echo "\n
dhapi_post=$dhapi_post";

$success=strpos($dhapi_page, "Successfully added");
// echo "success=$success;\n";

if ($success>0) {return true; }
else { return false; }
} // dhapi_panelEmailCreate()


/**
* This function edits an existing email account. Steps:
*
    *
  1. Get the "edit" page to extract the security_key.
  2. *
  3. Post the variables into the "?" page.
  4. *
  5. Extract from the result page...
  6. *
* @param string $email (input "text") The email address (user@domain) to edit.
* @param string $redirect (input "text") The email address(es) to redirect this one, or empty (""). One or many line-separated addresses (NOTE: is this \\n, \\r, mix?).
* @param mixed $redirect_only (input "hidden") Boolean "false" creates a normal inbox (also with forward capabilities) ; boolean "true" creates a forward-only email-address (no actual inbox is created) ; string "null" creates a "garbage email address. Warning : Last two value dump gecos and other values...
* @param mixed $name (input "text") The "name" (ex: John Doe), facultative.
* @param string $pass (input "text") The password for the account. It is used twice, so have it confirmed/checked before. Min 6 chars. TODO: Max?
* @param mixed $limMB (input "text") Limit value of disk usage (in MB or empty), or "false".
* @param bool $warning (input "checkbox") The "email me if my usage is within 10% of my limit (or 100MB, whichever is smaller)".
* @param int $rem_qty (input "text") Remove read messages when inbox reaches this message quantity (max 2000). Set to false for 0. NOTE: MIN:0, MAX:2000.
* @param int $rem_age (input "text") Remove read messages older than this numbers of days (max 9999). Set to false for 0. NOTE: MIN:0, MAX:9999.
* @param bool $rem_new (input "checkbox") Remove even unread messages.
* @param mixed $rem_fol (input "text") Save removed messages in this folder. Leave empty or set to false to really deleted them. NOTE: This will be formatted with imap_utf7_encode() so warn the user if input does not match formatted.
* @param bool $rem_mai (input "checkbox") Email me when messages are removed.
* @return bool True If the email was edited, false otherwise.
*/
function dhapi_panelEmailEdit($email, $redirect, $redirect_only, $name, $pass, $limMB, $warning, $rem_qty, $rem_age, $rem_new, $rem_fol, $rem_mai) {
global $dhapi_headerArray, $dhapi_userAgent, $dhapi_url;
global $dhapi_cookieJar2;

$mail = split('@', $email);
$alias = $mail[0];
$domain = $mail[1];
if ($limMB) { $enable_quota = "on"; $hard_quota = intval($limMB); }
if (!$warning) { $notify_disk = ""; } else { $notify_disk = "on"; }
if ($rem_qty) { $max_messages = intval($rem_qty); } else { $max_messages = "2000"; }
if ($rem_age) { $days = intval($rem_age); } else { $days = 9999; }
if ($rem_new) { $rotate_new = "on"; } else { $rotate_new = ""; }
if ($rem_fol) { $archive = "on"; $archive_folder = imap_utf7_encode($rem_fol); }
if ($rem_mai) { $notify = "on"; } else { $notify = ""; }

# get the security_key
$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?tree=mail.addresses&odomain=$domain¤t_step=Index&next_step=Edit&oalias=$alias";
$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
$dhapi_skey = dhapi_getSecurityKey($dhapi_page);

# post vars after they are created
$dhapi_ref_url=$dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?";

if ($redirect_only==="null") {
$dhapi_post = "tree=mail.addresses¤t_step=Edit&next_step=EditFinal&security_key=$dhapi_skey&dest=null";
$dhapi_post.= "&alias=$alias&domain=$domain";
}
elseif ($redirect_only==true) {
$dhapi_post = "tree=mail.addresses¤t_step=Edit&next_step=EditFinal&security_key=$dhapi_skey&dest=forward";
$dhapi_post.= "&alias=$alias&domain=$domain&addresses=$redirect";
}
elseif ($redirect_only==false) {
$dhapi_post = "tree=mail.addresses¤t_step=Edit&next_step=EditFinal&security_key=$dhapi_skey&dest=forward&mailbox=1"; // basic from hidden
$dhapi_post.= "&alias=$alias&domain=$domain&gecos=$name&random=&password1=$pass&password2=$pass"; // basic login
$dhapi_post.= "&enable_quota=$enable_quota&hard_quota=$hard_quota¬ify_disk=$notify_disk&addresses=$redirect"; // advanced login, archive and forward
$dhapi_post.= "&max_messages=$max_messages&days=$days&rotate_new=$rotate_new&archive=$archive&archive_folder=$archive_folder¬ify=$notify"; // tidy inboxes...
}

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 1);
curl_setopt($dhapi_curl, CURLOPT_POSTFIELDS, $dhapi_post);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
// echo "
$dhapi_page
";
// echo "\n
dhapi_post:$dhapi_post;\n\n";

$success = strpos($dhapi_page, "Successfully edited "); // echo "success=$success;\n";
if ($success > 0) { return true; }
else { return false; }
} // dhapi_panelEmailEdit()



/**
* This function checks if an email address already exists. It takes the address as parameter and checks for USER in DOMAIN.
* @param string $email This is the email address to check.
* @return bool True if the email DOES NOT exist, false otherwise.
*/
function dhapi_panelEmailCheck($email) {
global $USER, $PASS, $dhapi_url;
global $dhapi_headerArray, $dhapi_userAgent;
global $dhapi_cookieJar1, $dhapi_cookieJar2;
$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?tree=mail.addresses&";
$param_string = "";

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);

$mail = split('@', $email);
$dhapi_skey = dhapi_getSecurityKey($dhapi_page);
$dhapi_post = "tree=mail.addresses¤t_step=Index&next_step=DoSelector&security_key=$dhapi_skey&domainselector={$mail[1]}";

$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?";

$dhapi_curl = curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 1);
curl_setopt($dhapi_curl, CURLOPT_POSTFIELDS, $dhapi_post);
$dhapi_page = curl_exec($dhapi_curl);
// echo "
$dhapi_page
";
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);

// the following line is because in the panel HTML, the alias is bolded and not the @domain
$email=str_replace('@', '@', $email);
$exist=strpos($dhapi_page, $email);

if ($exist>0) { return false; } else { return true; }
} // dhapi_panelEmailCheck()



/**
* This functions deletes PERMANENTLY an email account and all its messages
* First connects to the "Manage email" page and get the security code;
* Then requests the delete-email URI.
* @param string $email The email address to delete.
* @return bool True is email deleted, false otherwise
*/
function dhapi_panelEmailDelete($email) {
global $dhapi_headerArray, $dhapi_userAgent, $dhapi_url;
global $dhapi_cookieJar2;

# build the variables...
$dh_mailDelete = split('@', $email);
$alias = $dh_mailDelete[0];
$domain = $dh_mailDelete[1];

# get the security key...
$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?tree=mail.addresses&";
$dhapi_curl=curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
$dhapi_skey = dhapi_getSecurityKey($dhapi_page);

# delete the email address...
$dhapi_ref_url = $dhapi_url;
$dhapi_url = "https://panel.dreamhost.com/index.cgi?tree=mail.addresses&odomain=$domain¤t_step=Index&next_step=DeleteFinal&oalias=$alias&security_key=$dhapi_skey";
$dhapi_curl=curl_init($dhapi_url);
curl_setopt($dhapi_curl, CURLOPT_VERBOSE, 1);
curl_setopt($dhapi_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dhapi_curl, CURLOPT_HEADER, 1);
curl_setopt($dhapi_curl, CURLOPT_REFERER, $dhapi_ref_url);
curl_setopt($dhapi_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($dhapi_curl, CURLOPT_HTTPHEADER, $dhapi_headerArray);
curl_setopt($dhapi_curl, CURLOPT_USERAGENT, $dhapi_userAgent);
curl_setopt($dhapi_curl, CURLOPT_COOKIEFILE, $dhapi_cookieJar2);
curl_setopt($dhapi_curl, CURLOPT_POST, 0);
$dhapi_page = curl_exec($dhapi_curl);
dhapi_showCurlErrors ($dhapi_curl);
curl_close($dhapi_curl);
// echo "
$dhapi_page
";

$success = strpos($dhapi_page, "Successfully deleted $email!");
if ($success > 0) { return true; }
else { return false; }
} // dhapi_panelEmailDelete()



Source: http://api.dreamhosters.com/