Phpmailer: $mail->getSMTPInstance()->getLastTransactionID(); returning false where there is an actual Queue ID

Created on 9 Oct 2017  路  11Comments  路  Source: PHPMailer/PHPMailer

Hello,

I use sendgrid.com to send my emails using PHPMailer 6.0.0
I can send emails no problem the only thing is when trying to get the queue ID it is not working.

I have read here https://github.com/PHPMailer/PHPMailer/issues/1123
That it was solved but it is not working for me

require ('../vendor/autoload.php');
$mail = new PHPMailer(true);

// Server settings
$mail->SMTPDebug = 2; // Uncomment for debugging
$mail->isSMTP();
$mail->Host = "smtp.sendgrid.net";
$mail->SMTPAuth = true;
$mail->Username = "xxx";
$mail->Password = "xxx";
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->Port = 25;

Below is my SMTP debug

code before authentication removed and other replaced by xxxx for privacy
2017-10-09 02:04:20 SERVER -> CLIENT: 235 Authentication successful
2017-10-09 02:04:20 CLIENT -> SERVER: MAIL FROM:
2017-10-09 02:04:20 SERVER -> CLIENT: 250 Sender address accepted
2017-10-09 02:04:20 CLIENT -> SERVER: RCPT TO:
2017-10-09 02:04:20 SERVER -> CLIENT: 250 Recipient address accepted
2017-10-09 02:04:20 CLIENT -> SERVER: DATA
2017-10-09 02:04:20 SERVER -> CLIENT: 354 Continue
2017-10-09 02:04:20 CLIENT -> SERVER: Date: Sun, 8 Oct 2017 22:04:19 -0400
2017-10-09 02:04:20 CLIENT -> SERVER: To: xxxxx
2017-10-09 02:04:20 CLIENT -> SERVER: From: xx
2017-10-09 02:04:20 CLIENT -> SERVER: Reply-To: xx
2017-10-09 02:04:20 CLIENT -> SERVER: Subject: test
2017-10-09 02:04:20 CLIENT -> SERVER: Message-ID:
2017-10-09 02:04:20 CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.0 (https://github.com/PHPMailer/PHPMailer)
2017-10-09 02:04:20 CLIENT -> SERVER: MIME-Version: 1.0
2017-10-09 02:04:20 CLIENT -> SERVER: Content-Type: multipart/alternative;
2017-10-09 02:04:20 CLIENT -> SERVER: boundary="xxxx"
2017-10-09 02:04:20 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2017-10-09 02:04:20 CLIENT -> SERVER:
2017-10-09 02:04:20 CLIENT -> SERVER: This is a multi-part message in MIME format.
2017-10-09 02:04:20 CLIENT -> SERVER: --xxxx
2017-10-09 02:04:20 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2017-10-09 02:04:20 CLIENT -> SERVER:
2017-10-09 02:04:20 CLIENT -> SERVER: To properly view this message please switch or use an HTML compatible e-mail client.
2017-10-09 02:04:20 CLIENT -> SERVER:
2017-10-09 02:04:20 CLIENT -> SERVER: --xxxx
2017-10-09 02:04:20 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2017-10-09 02:04:20 CLIENT -> SERVER:
2017-10-09 02:04:20 CLIENT -> SERVER: some body text
2017-10-09 02:04:20 CLIENT -> SERVER:
2017-10-09 02:04:20 CLIENT -> SERVER: --xxxx--
2017-10-09 02:04:20 CLIENT -> SERVER:
2017-10-09 02:04:20 CLIENT -> SERVER: .
2017-10-09 02:04:20 SERVER -> CLIENT: 250 Ok: queued as r0k-aGYnQtWFOzqR_w
2017-10-09 02:04:20 CLIENT -> SERVER: QUIT
2017-10-09 02:04:20 SERVER -> CLIENT: 221 See you later

the output of $mail->getSMTPInstance()->getLastTransactionID() is
bool(false)

the output of $mail->getSMTPInstance()->getLastReply() is
string(19) "221 See you later "

What I really want is the 3rd reply from the bottom
2017-10-09 02:04:20 SERVER -> CLIENT: 250 Ok: queued as r0k-aGYnQtWFOzqR_w

And would like to have the queue ID "r0k-aGYnQtWFOzqR_w" returned after sending the message

One last thing not directly related to this, do I need to send tags in the message?

Your help is much appreciated
Thank you

All 11 comments

Have you tried using 6.0.1, which contains a fix for this?

Hello Synchro,
I just downloaded the latest PHPmailer and tried again, same result

2017-10-09 12:23:54 CLIENT -> SERVER: .
2017-10-09 12:23:54 SERVER -> CLIENT: 250 Ok: queued as 68qeqHy5BaP4Rw
2017-10-09 12:23:54 CLIENT -> SERVER: QUIT
2017-10-09 12:23:54 SERVER -> CLIENT: 221 See you later

$tid = $mail->getSMTPInstance()->getLastTransactionID();
var_dump($tid);
bool(false)

$reply = $mail->getSMTPInstance()->getLastReply();
var_dump($reply);
string(19) "221 See you later "

Any ideas?

Thank you

The function docs say:

If no reply has been received yet, it will return null.
If no pattern was matched, it will return false.

So because you're seeing false, it means none of the patterns matched when recordLastTransactionID was called. That is slightly odd as the text you show looks like it should match postfix response pattern: [0-9]{3} 2.0.0 Ok: queued as (.*). I'd suggest setting a breakpoint in the data() method and seeing what's happening at the time. getLastReply isn't any use for this at the PHPMailer level because (as you're seeing) it gets overwritten by the subsequent QUIT command.

Hello,

I am not sure what you mean by setting a breakpoint in the data() method?
Can you please provide an example

Thank you

In a development environment like NetBeans or PHPStorm, you can tell the code to stop at any point and inspect the values of everything while the code is running. It's much easier to tell what's going on than using var_dump, though doing a var_dump($reply) in recordLastTransactionID (after line 1287 of SMTP.php) would be interesting.

Hello,

The results are interesting please see below, I can see the queue ID from the var_dump($reply) on line 1287 as you suggested

2017-10-09 13:17:00 CLIENT -> SERVER: .
2017-10-09 13:17:00 SERVER -> CLIENT: 250 Ok: queued as 0byqG5-MT2iy1UE1IjAtjA
below is the result of vardump reply
:string(42) "250 Ok: queued as 0byqG5-MT2iy1UE1IjAtjA "
End of vardump
2017-10-09 13:17:00 CLIENT -> SERVER: QUIT
2017-10-09 13:17:00 SERVER -> CLIENT: 221 See you later

Any ideas?

Thank you

Ah, I wasn't looking carefully enough - it doesn't match the postfix pattern - it's missing the 2.0.0 result code part. You would need to add this pattern to $smtp_transaction_id_patterns:

    'myserver' => '/[0-9]{3} Ok: queued as (.*)/',

Can you tell me what kind of server you are connecting to? It should say at the very beginning of the SMTP debug output.

Hello,

You are amazing! It is working now like a charm thank you, any ideas why it is adding a whitespace at the end of the string though?

2017-10-09 13:27:21 SERVER -> CLIENT: 250 Ok: queued as hVpD7-26SJSdDOloTZnslg
2017-10-09 13:27:21 CLIENT -> SERVER: QUIT
2017-10-09 13:27:21 SERVER -> CLIENT: 221 See you later
string(23) "hVpD7-26SJSdDOloTZnslg "

Is this what you are looking for?
2017-10-09 13:17:00 SERVER -> CLIENT: 220 SG ESMTP service ready at ismtpd0001p1iad1.sendgrid.net

Thank you

I just pushed support for this format of the transaction ID - it will be in the next maintenance release, or you can just download the current master version from here.

It also trims all IDs now.

Perfect

Thank you Marcus

Great effort, all the best

Was this page helpful?
0 / 5 - 0 ratings