Wednesday, November 08, 2006

microsoft exchange: add email (part 2)

ok, i have the solution for both creating email adress and reset the password at once.
and still using that csvde and net user command line interface. in this page, you will also find dsrm command line to delete an email address. and for the script, i'm using PHP, and some pear packages. so, it will be a script to create an email address and reset the password on microsoft exchange 2003 using PHP.

the file you should find on this page is:
  • email.add.php -> php script to create email and reset the password on microsoft exchange server
  • email.del.php -> php script to delete email from microsoft exchange server
  • do.email.add.php -> interface to add an email address using email.add.php
  • do.email.del.php -> interface to delete an email address using email.del.php
first, you must have PHP installed on your microsoft exchange server, it is up to you, using MS IIS, or Apache as the web server, but i recomend you to use Apache as it is easy to maintain. and if you using Apache, i recomend you to install the web service on port 8080. and then make sure that there is nobody can access that web service on port 8080 is only your server that will integrate the microsoft exchange email provisioning application.

ok, this is the php script to create and reset the password
<?
//--[create email and reset password]-----------------------------------------------
$dc1 = $_POST["dc1"];
$dc2 = $_POST["dc2"];
$un = $_POST["un"];
$gn = $_POST["gn"];
$fn = $_POST["fn"];
$ln = $_POST["ln"];

$csv_user = <<<CSV
DN,objectClass,name,homeMDB,cn,displayName,mail,givenName,proxyAddresses,sAMAccountName,sn,userAccountControl,userPrincipalName,homeMTA,msExchHomeServerName,mailNickname,countryCode,c,l,homePhone,mobile,telephoneNumber,co,wWWHomePage
"CN=$gn,CN=Users,DC=$dc1,DC=$dc2",user,$gn,"CN=Mailbox Store (SERVER100),CN=First Storage Group,CN=InformationStore,CN=SERVER100,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=$dc1,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=$dc1,DC=$dc2",$gn,$gn,$un@$dc1.$dc2,$fn,SMTP:$un@$dc1.$dc2;X400:c=us\;a= \;p=$dc1\;o=Exchange\;s=$ln\;g=$fn\;,$un,$ln,512,$un@$dc1.$dc2,"CN=Microsoft MTA,CN=SERVER100,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=Imofis,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=$dc1,DC=$dc2",/o=$dc1/ou=First Administrative Group/cn=Configuration/cn=Servers/cn=SERVER100,$un,376,ID,Jakarta,,,,Indonesia,
CSV;
$csv_file = "tmpdir\\".date("Y.m.d.h.m.i.")."$un.csv";
$file_handler = fopen($csv_file, "w");
$ok = true;
if (fwrite($file_handler, $csv_user) === false) {
$ok = false;
$err = 1;
$msg = "can not create user file";
$ret = "<?xml version='1.0'?>".
"<response>".
"<code>$err</code>".
"<description>$msg</description>".
"</response>";
fclose($file_handler);
echo $ret;
}
if ($ok) {
$dump = `csvde -i -f $csv_file`;
if (eregi("The specified user already exists.", $dump)) {
$err = 1;
$msg = "The specified user already exists.";
$ret = "<?xml version='1.0'?>".
"<response>".
"<code>$err</code>".
"<description>$msg</description>".
"</response>";
echo $ret;
} else {
$ret1 = "<pre>$dump</pre>";
$dump = `net user $un Pa$$w0rd`;
$ret2 = "<pre>$dump</pre>";
$dump = `del $csv_file`;
$err = 0;
$msg = $ret1.$ret2;
$ret = "<?xml version='1.0'?>".
"<response>".
"<code>$err</code>".
"<description>$msg</description>".
"</response>";
echo $ret;
}
unlink($csv_file);
}

//-----------------------------------------------[create email and reset password]--
?>

save the file in the webroot of Apache as email.add.php
now, you only need an interface to create an email address using that script from other server, or from that server it self. the script must post these parameters:
  • first name (fn)
  • last name (ln)
  • given name (gn)
  • user name (un)
  • domain (dc1)
  • domain prefix (dc2)
here is a sample of an interface that create an email address using email.add.php
<--[interface create email]----------------------------------------------->
<html>
<body>
<form method="POST" action="">
email: <input type="text" name="email" value="<?= $_POST["email"] ?>"><br>
name: <input type="text" name="name" value="<?= $_POST["name"] ?>"><br>
<input type="submit" name="action" value="Add">
</form>
</body></html>
<?
require_once 'HTTP/Client.php';
$client =& new HTTP_Client();
//--[fetch and submit email creation]-----------------------------------------------
$email = $_POST["email"];
$name = $_POST["name"];
$x = split("@", $email);
$un = $x[0];
$d = split("\.", $x[1]);
$dc1 = $d[0];
$dc2 = $d[1];
$n = split(" ", $name);
$fn = $n[0];
$ln = $n[count($n) - 1];
$x = $client->post("http://the.server.name:8080/email.add.php",
array(
"dc1" => $dc1,
"dc2" => $dc2,
"un" => $un,
"gn" => $name,
"fn" => $fn,
"ln" => $ln
));

echo "<pre>";
print_r($client->currentResponse());
echo "</pre>";
//-----------------------------------------------[fetch and submit email creation]--
?>
<--[interface create email]----------------------------------------------->
save it to your apache webserver directory, name it do.email.add.php
yes, you only need to fill the name and email address in the form, and submit it.

and this is the script to delete an account using php
<?
//--[delete email]-----------------------------------------------
$dc1 = $_POST["dc1"];
$dc2 = $_POST["dc2"];
$un = $_POST["un"];
$gn = $_POST["gn"];
$cmd = "dsrm -subtree -noprompt -c \"cn=$gn,cn=Users,dc=$dc1,dc=$dc2\"";
$dump = `$cmd`;
$err = 0;
$msg = $dump;
$ret = "<?xml version='1.0'?>".
"<response>".
"<code>$err</code>".
"<description>$msg</description>".
"</response>";
echo $ret;
//-----------------------------------------------[delete email]--
?>
and save it to email.del.php
and this is the sample of the interface
<--[interface drop email]----------------------------------------------->
<html>
<body>
<form method="POST" action="">
email: <input type="text" name="email" value="<?= $_POST["email"] ?>"><br>
name: <input type="text" name="name" value="<?= $_POST["name"] ?>"><br>
<input type="submit" name="action" value="Drop">
</form>
</body></html>
<?
require_once 'HTTP/Client.php';
$client =& new HTTP_Client();
//--[fetch and submit email deletion]-----------------------------------------------
$email = $_POST["email"];
$name = $_POST["name"];
$x = split("@", $email);
$un = $x[0];
$d = split("\.", $x[1]);
$dc1 = $d[0];
$dc2 = $d[1];
$n = split(" ", $name);
$fn = $n[0];
$ln = $n[count($n) - 1];
$x = $client->post("http://the.server.name:8080/email.del.php",
array(
"dc1" => $dc1,
"dc2" => $dc2,
"un" => $un,
"gn" => $name,
"fn" => $fn,
"ln" => $ln
));

echo "<pre>";
print_r($client->currentResponse());
echo "</pre>";
//-----------------------------------------------[fetch and submit email deletion]--
?>
<--[interface drop email]----------------------------------------------->
and save it to your apache web root directory, name it do.email.del.php
ok, thats all i have got to share to you.

but, if you have better solution, i would be very happy if you share your solution to me.

salaam

No comments: