Click to See Complete Forum and Search --> : Example: Authenticating via Exchange Server


curtissoftware
10-01-2002, 07:25 AM
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

bcfc1982
11-30-2002, 01:39 PM
Thanks for this information. I was having problems with the mail() function then I saw this message and used the code and it worked. One thing I'm still trying to do though is to email multiple addresses in a single call and to add a CC address to mail().
I've tried passing more than one email address by concatenating commas and semi colons between addresses. That did not seem to work. I'm passing CC email address via $ccemail variable (below). This did not seem to work as well.

// Initiate connection with the SMTP server
$handle = fsockopen($smtp_server,$port);
if($handle)
{
fputs($handle, "EHLO $mydomain\r\n");
// SMTP authorization
fputs($handle, "AUTH LOGIN\r\n");
fputs($handle, base64_encode($username)."\r\n");
fputs($handle, base64_encode($password)."\r\n");

// Send out the e-mail
fputs($handle, "MAIL FROM:<$sender>\r\n");
fputs($handle, "RCPT TO:<$recipient>\r\n");
fputs($handle, "DATA\r\n");
fputs($handle, "To: $recipient\r\n");
fputs($handle, "CC: $ccemail\r\n");
fputs($handle, "Subject: $subject\r\n");
fputs($handle, "$content");
fputs($handle, "\r\n.\r\n");

Latest Update...
I got this to work now by passing mulitple recipients eg.

fputs($handle, "RCPT TO:<$recipient1>\r\n");
fputs($handle, "RCPT TO:<$recipient2>\r\n");
fputs($handle, "DATA\r\n");
fputs($handle, "To: $recipient1\r\n");
fputs($handle, "To: $recipient2\r\n");

Is there anyway to use Exchange without Authentication?