0

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"?

Vladimir
  • 1,425
  • 16
  • 31

1 Answers1

2

It looks like that your MS Exchange Server is not configured correctly as seen in your reply above:

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)]

So you might wish to adjust the configuration and remove the check mark for "Offer Basic authentication only after starting TLS" which will then offer plain "Auth Login" without the need to do a startTLS here.

The reason by the way why the C# code is working but not the PHP one might be that the ssl certificate running on your MS Exchange server isn´t trusted. I think when you run the C# code its done on a PC joined to active directory domain and the SSL root certificate used by the SMTP port from the Microsoft Exchange server is there, but on the server (Linux?) running the PHP code the root cert isn´t there. Here is a link which you can use to troubleshoot that.

You also might wish to try something like:

$smtp = Mail::factory('smtp', array (
    'host' => "mail.mydomain.com",
    'port' => 587,
    'auth' => plain,
    'socket_options' => array('ssl' => array('verify_peer_name' => false)),
    'username' => "activeDirectoryDomain\\sender",
    'password' => "password"
));

As mentioned here.

BastianW
  • 2,628
  • 7
  • 29
  • 38
  • Thanks for the hint. I succeeded in enabling "Auth Login" and now I am a step further (see updated question). The SSL certificate is a trusted one, by an official CA (not the windows domain one). – Vladimir Aug 03 '17 at 14:11