-1

I have a problem connecting to SMTP-Server via PHPMailer.

<?php
$mail = new PHPMailer();
$mail->isSMTP();

$mail->SMTPDebug = true;

$mail->Host = "ip.ip.ip.ip";
$mail->Port = "25";
$mail->Helo = "";
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = "";
$mail->SMTPAuth = false;
$mail->Timeout = 10;

$mail->From = "sender@example.com";
$mail->FromName = "Sender Name";

$mail->addAddress("receiver@example.com");

$mail->isHTML(true);
$mail->Subject = "Test";
$mail->Body = "<h1>Test</h1>";


if(!$mail->Send()) {
    echo $mail->ErrorInfo;
} else {
    echo "Mail sent...";
}

I figured out the main problem: After successfully connect the server is NOT sending a "220 announcement" directly. It is sending nothing but waiting for a EHLO. After any client input the server sends a "220 announcement"! I tested it with telnet.

I newer saw it before and i don't know if it respects the rfc.

The 220 AFTER the EHLO makes PHPMailer completely crashing. Any following communication is "misunderstood". I saw that PHPMailer is waiting for a 220 announcement after connecting. But this fails because the server is not sending anything. When the timeout is reached the client sends a EHLO. After that it receives a 220 but the following communication fails.

Here is the full communication output:

2017-03-30 13:38:48     Connection: opening to ip.ip.ip.ip:587, timeout=10, options=array (
                                          )
2017-03-30 13:38:48     Connection: opened
2017-03-30 13:38:58     SMTP -> get_lines(): $data is ""
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  ""
2017-03-30 13:38:58     SMTP -> get_lines(): timed-out (10 sec)
2017-03-30 13:38:58     SERVER -> CLIENT:
2017-03-30 13:38:58     CLIENT -> SERVER: EHLO conweb1.example.de
2017-03-30 13:38:58     SMTP -> get_lines(): $data is ""
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "220 EX-02.example.loc Microsoft ESMTP MAIL Service ready at Thu, 30 Mar 2017 15:38:58 +0200
                                          "
2017-03-30 13:38:58     SERVER -> CLIENT: 220 EX-02.example.loc Microsoft ESMTP MAIL Service ready at Thu, 30 Mar 2017 15:38:58 +0200
2017-03-30 13:38:58     SMTP ERROR: EHLO command failed: 220 EX-02.example.loc Microsoft ESMTP MAIL Service ready at Thu, 30 Mar 2017 15:38:58 +0200
2017-03-30 13:38:58     CLIENT -> SERVER: HELO conweb1.example.de
2017-03-30 13:38:58     SMTP -> get_lines(): $data is ""
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-SIZE 36700160
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-PIPELINING
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-DSN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-ENHANCEDSTATUSCODES
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-STARTTLS
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-AUTH LOGIN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-8BITMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          250-8BITMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250-BINARYMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $data is "250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          250-8BITMIME
                                          250-BINARYMIME
                                          "
2017-03-30 13:38:58     SMTP -> get_lines(): $str is  "250 CHUNKING
                                          "
2017-03-30 13:38:58     SERVER -> CLIENT: 250-EX-02.example.loc Hello [ip.ip.ip.ip]
                                          250-SIZE 36700160
                                          250-PIPELINING
                                          250-DSN
                                          250-ENHANCEDSTATUSCODES
                                          250-STARTTLS
                                          250-AUTH LOGIN
                                          250-8BITMIME
                                          250-BINARYMIME
                                          250 CHUNKING
2017-03-30 13:38:58     CLIENT -> SERVER: MAIL FROM:<personal@example.com>
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "250 EX-02.example.loc Hello [ip.ip.ip.ip]
                                          "
2017-03-30 13:39:03     SERVER -> CLIENT: 250 EX-02.example.loc Hello [ip.ip.ip.ip]
2017-03-30 13:39:03     CLIENT -> SERVER: RCPT TO:<tm@example.com>
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "250 2.1.0 Sender OK
                                          "
2017-03-30 13:39:03     SERVER -> CLIENT: 250 2.1.0 Sender OK
2017-03-30 13:39:03     CLIENT -> SERVER: DATA
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "250 2.1.5 Recipient OK
                                          "
2017-03-30 13:39:03     SERVER -> CLIENT: 250 2.1.5 Recipient OK
2017-03-30 13:39:03     SMTP ERROR: DATA command failed: 250 2.1.5 Recipient OK
2017-03-30 13:39:03     SMTP Error: data not accepted.
SMTP Error: data not accepted.SMTP server error: DATA command failed Detail: Recipient OK
 SMTP code: 250 Additional SMTP info: 2.1.52017-03-30 13:39:03  CLIENT -> SERVER: QUIT
2017-03-30 13:39:03     SMTP -> get_lines(): $data is ""
2017-03-30 13:39:03     SMTP -> get_lines(): $str is  "354 Start mail input; end with <CRLF>.<CRLF>
2017-03-30 13:39:03     SMTP ERROR: QUIT command failed: 354 Start mail input; end with <CRLF>.<CRLF>
2017-03-30 13:39:03     Connection: closed

The main question is: Is the error caused by a wrong protocol implementation of PHP-Mailer? Or is the SMTP Server misconfigured?

steven
  • 4,868
  • 2
  • 28
  • 58
  • You asked whether it is possible to execute commands on the smtp server, you could test with a telnet to verify that if commands can be executed, Another observation as it appears it seems that the data is empty – Guillermo Andres Fuentes Moral Mar 30 '17 at 14:49
  • Try add $mail->AltBody ="Test" after $mail->Body – Guillermo Andres Fuentes Moral Mar 30 '17 at 14:53
  • @GuillermoAndresFuentesMoral Data isnt empty! The code works with other smtp servers! I tested telnet successfully. The reason is that the responses are delayed caused by the late 220 at the beginning. Any following responses are missunderstood by the client. As you can see there is a timeout first caused by a missing 220 greeting. – steven Mar 30 '17 at 15:03
  • They could have configured the server to delay for over 10 seconds before sending the greeting (to reduce spam). It is a violation of rfc to send a helo/ehlo before receiving the greeting, but you may need to wait longer for it. Via telnet, if you connect and let it sit, does it eventually send the greeting? – drew010 Mar 30 '17 at 15:07
  • @drew010 no! Not before i send anything. But if i send something it answers immediately with 220... – steven Mar 30 '17 at 15:44
  • In this case it doesn't necessarily sound like phpmailer is the issue, but I'd agree with Synchro and don't set a low timeout in your code. Consider also you may be rate limited due to so many attempts and the server is behaving oddly because you're blocked. – drew010 Mar 30 '17 at 17:27

1 Answers1

0

Don't do this:

$mail->Timeout = 10;

RFC2821 specifies a timeout of 300sec (5 minutes) on the initial greeting, and some servers impose a long delay here (known as a "greet delay") specifically to trap non-conforming clients, usually spambots. PHPMailer respects this exactly with its default Timeout setting of 300 sec. If you shorten it, you're preventing it from waiting for long enough. Does it work if you set it to 300 or leave at its default setting?

If you say "but I don't want to wait that long during page processing!" - welcome to the world of SMTP - it's not designed for real-time use; use a local mail server relay to remove this delay and let the mail server worry about it.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Ok. I will try to wait 300sec. But i suspect this is not the issue. My ip is whitelisted on the remote smtp and nobody else is able to connect. So it would be very surprising that they choose a greet delay for spam protection. But maybe it is a default Feature of Microsoft and they didnt changed it. I will try and let you know whats going on. – steven Mar 30 '17 at 18:01