Here is some sample code that demonstrates how to send email through an Exchange 2000 server that requires user authentication.
<?php
echo "<b><center>PHP Email Test</b></center><br>";
// Place the name of you SMTP Server here
$smtp_server = "localhost";
$port = 25;
// The domain name of your server.
$mydomain = "yourdomainname.com";
// Some MS Exchange servers might require a
// valid domain name as part of the username.
// E.g., "ntdomainname\username"
$username = "username";
$password = "password";
// Senders Email Address
$sender = "username@yourdomainname.com";
// the email address you are sending to.
$recipient = "name@sendingtodomainname.com";
$subject = "PHP Email Test";
$content = "This email was sent through an Exchange Server that requires authentication.";
echo "Sending Mail to $recipient";
echo "Opening socket...<br>";
// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server,$port);
echo "Open socket result $handle<br>";
if($handle)
{
echo "EHLO<br>";
fputs($handle, "EHLO $mydomain\r\n");
echo fgets($handle,100); echo "<br>";
// SMTP authorization
echo "AUTH_LOGIN<br>";
fputs($handle, "AUTH LOGIN\r\n");
fputs($handle, base64_encode($username)."\r\n");
echo "$username<br>";
echo fgets($handle,100); echo "<br>";
fputs($handle, base64_encode($password)."\r\n");
echo "$password<br>";
echo fgets($handle,100); echo "<br>";
// Send out the e-mail
fputs($handle, "MAIL FROM:<$sender>\r\n");
echo "MAIL FROM:<BR>";
echo fgets($handle,100); echo "<br>";
fputs($handle, "RCPT TO:<$recipient>\r\n");
echo "RCPT TO:<br>";
echo fgets($handle,100); echo "<br>";
fputs($handle, "DATA\r\n");
echo "DATA:<br>";
echo fgets($handle,100); echo "<br>";
fputs($handle, "To: $recipient\r\n");
fputs($handle, "Subject: $subject\r\n");
fputs($handle, "$content");
fputs($handle, "\r\n.\r\n");
echo "DATA:<br>";
echo fgets($handle,100); echo "<br>";
// Close connection to SMTP server
fputs($handle, "QUIT\n");
echo "QUIT<br>";
echo fgets($handle,100); echo "<br>";
sleep(5);
fclose($handle);
echo "DONE!<br>";
}
else
echo "Failed to open socket!";
?>
Any comments and or improvements are welcomed!
Regards,
Kevin