I need to send an SMTP email over Exchange Server 2016 in PHP using PEAR mail and an authenticated AD user. I found an example online:
<html><body>
<?php
include('Mail.php');
$body = "Hi,\n\nHow are you?";
$headers = array (
'From' => "sender@mydomain.com",
'To' => "recipient@gmail.com",
'Subject' => "Hi from MailTest3.php!"
);
$smtp = Mail::factory('smtp', array (
'host' => "mail.mydomain.com",
'port' => 587,
'auth' => true,
'username' => "activeDirectoryDomain\\sender",
'password' => "password"
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
</body></html>
When I run this I get the following error:
authentication failure [SMTP: No supported authentication methods (code: 250, response: mail.mydomain.com Hello [192.168.30.254] SIZE 37748736 PIPELINING DSN ENHANCEDSTATUSCODES STARTTLS AUTH GSSAPI NTLM 8BITMIME BINARYMIME CHUNKING)]
At the same time this similar C# application works fine without any problems:
static void Main(string[] args)
{
var body = "Hi,\n\nHow are you?";
var msg = new MailMessage(
from: "sender@mydomain.com",
to: "recipient@gmail.com",
subject: "Hi from MailTest3.cs!",
body: body);
var smtp = new SmtpClient(
host: "mail.mydomain.com",
port: 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(
userName: "activeDirectoryDomain\\sender",
password: "password");
try
{
smtp.Send(msg);
Console.WriteLine("Message successfully sent!");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
As the C# code works fine, the Exchange Server is correctly configured but PEAR mail cannot authenticate.
How shall I configure PEAR mail and/or Exchange so that this can work? Anonymous SMTP email sending is not an option in this environment.
Update: After BastianW's comment I succeeded in enabling AUTH LOGIN and now there is a different error:
DEBUG: Recv: 220 mail.mydomain.com Microsoft ESMTP MAIL Service ready at Thu, 3 Aug 2017 15:33:14 +0200
DEBUG: Send: EHLO localhost
DEBUG: Recv: 250-mail.mydomain.com Hello
DEBUG: Recv: 250-SIZE 37748736
DEBUG: Recv: 250-PIPELINING
DEBUG: Recv: 250-DSN
DEBUG: Recv: 250-ENHANCEDSTATUSCODES
DEBUG: Recv: 250-STARTTLS
DEBUG: Recv: 250-AUTH LOGIN
DEBUG: Recv: 250-8BITMIME
DEBUG: Recv: 250-BINARYMIME
DEBUG: Recv: 250 CHUNKING
DEBUG: Send: AUTH LOGIN
DEBUG: Recv: 334 VXNlcm5hbWU6
DEBUG: Send: cGxcUHJlcGF5LlBsYXRmb3Jt
DEBUG: Recv: 334 UGFzc3dvcmQ6
DEBUG: Send: OTAjMDNiUjFaaGM2SjRU
DEBUG: Recv: 235 2.7.0 Authentication successful
DEBUG: Send: MAIL FROM:
DEBUG: Recv: 250 2.1.0 Sender OK
DEBUG: Send: RSET
DEBUG: Recv: 250 2.0.0 Resetting
DEBUG: Send: QUIT
Why does PEAR mail send RSET which closes the connection when it gets the "Sender OK"?