PHPMailer / PHPMailer

The classic email sending library for PHP
GNU Lesser General Public License v2.1
20.98k stars 9.73k forks source link

Get Last Message ID always Empty #2936

Open iamved777 opened 1 year ago

iamved777 commented 1 year ago

I am trying to get last message id from PHPMailer when its send email. My Code is looks like

$mail = new PHPMailer(true);

    try {

        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host       = $s_server;
        $mail->SMTPAuth   = true;
        $mail->Username   = $s_username;
        $mail->Password   = $s_password;
        $mail->SMTPSecure = $s_protocol;
        $mail->Port       = $s_port;
        $mail->Hostname = $s_server;
        $mail->XMailer = ' ';

        $mail->SMTPOptions = array(
       'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        ); 

        //Recipients
        $mail->setFrom($s_from, $s_from_name);
        $mail->addAddress("mygmail@gmail.com");

        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $message;

        $mail->preSend();

        $mail->send();

        echo $mail->getLastMessageID();
        echo "<br>";
        echo "<pre>";
        print_r($mail);
        echo "</pre>";
        echo "<br>";
        echo $mail['FromName'];

    } 
    catch (Exception $e) {

    }

When I echo $mail->getLastMessageID() its always empty but when I var_dump($mail) I have array like below.

PHPMailer\PHPMailer\PHPMailer Object
(
    [Priority] => 
    [CharSet] => iso-8859-1
    [ContentType] => text/html
    [Encoding] => 7bit
    [ErrorInfo] => 
    [From] => user@example.com
    [FromName] => Claire Smith
    [Sender] => user@example.com
    [Subject] => I am testing the SMTP
    [Body] => This is a test email!
    [AltBody] => 
    [Ical] => 
    [MIMEBody:protected] => This is a test email!

    [MIMEHeader:protected] => Date: Mon, 24 Jul 2023 23:51:46 -0400
To: mygmail@gmail.com
From: Claire Smith 
Subject: I am testing the SMTP
Message-ID: 
MIME-Version: 1.0
Content-Type: text/html; charset=iso-8859-1

    [mailHeader:protected] => 
    [WordWrap] => 0
    [Mailer] => smtp
    [Sendmail] => /usr/sbin/sendmail
    [UseSendmailOptions] => 1
    [ConfirmReadingTo] => 
    [Hostname] => s1.example.com
    [MessageID] => 
    [MessageDate] => 
    [Host] => s1.example.com
    [Port] => 587
    [Helo] => 
    [SMTPSecure] => tls
    [SMTPAutoTLS] => 1
    [SMTPAuth] => 1
    [SMTPOptions] => Array
        (
            [ssl] => Array
                (
                    [verify_peer] => 
                    [verify_peer_name] => 
                    [allow_self_signed] => 1
                )

        )

    [Username] => user@example.com
    [Password] => ashgujkfhsjfgsfsfs
    [AuthType] => 
    [oauth:protected] => 
    [Timeout] => 300
    [dsn] => 
    [SMTPDebug] => 0
    [Debugoutput] => html
    [SMTPKeepAlive] => 
    [SingleTo] => 
    [SingleToArray:protected] => Array
        (
        )

    [do_verp] => 
    [AllowEmpty] => 
    [DKIM_selector] => 
    [DKIM_identity] => 
    [DKIM_passphrase] => 
    [DKIM_domain] => 
    [DKIM_copyHeaderFields] => 1
    [DKIM_extraHeaders] => Array
        (
        )

    [DKIM_private] => 
    [DKIM_private_string] => 
    [action_function] => 
    [XMailer] =>  
    [smtp:protected] => PHPMailer\PHPMailer\SMTP Object
        (
            [do_debug] => 0
            [Debugoutput] => html
            [do_verp] => 
            [Timeout] => 300
            [Timelimit] => 300
            [smtp_transaction_id_patterns:protected] => Array
                (
                    [exim] => /[\d]{3} OK id=(.*)/
                    [sendmail] => /[\d]{3} 2.0.0 (.*) Message/
                    [postfix] => /[\d]{3} 2.0.0 Ok: queued as (.*)/
                    [Microsoft_ESMTP] => /[0-9]{3} 2.[\d].0 (.*)@(?:.*) Queued mail for delivery/
                    [Amazon_SES] => /[\d]{3} Ok (.*)/
                    [SendGrid] => /[\d]{3} Ok: queued as (.*)/
                    [CampaignMonitor] => /[\d]{3} 2.0.0 OK:([a-zA-Z\d]{48})/
                    [Haraka] => /[\d]{3} Message Queued \((.*)\)/
                    [ZoneMTA] => /[\d]{3} Message queued as (.*)/
                    [Mailjet] => /[\d]{3} OK queued as (.*)/
                )

            [last_smtp_transaction_id:protected] => E17B83EE1B
            [smtp_conn:protected] => 
            [error:protected] => Array
                (
                    [error] => 
                    [detail] => 
                    [smtp_code] => 
                    [smtp_code_ex] => 
                )

            [helo_rply:protected] => 
            [server_caps:protected] => 
            [last_reply:protected] => 221 2.0.0 Bye

        )

    [to:protected] => Array
        (
            [0] => Array
                (
                    [0] => mygmail@gmail.com
                    [1] => 
                )

        )

    [cc:protected] => Array
        (
        )

    [bcc:protected] => Array
        (
        )

    [ReplyTo:protected] => Array
        (
        )

    [all_recipients:protected] => Array
        (
            [mygmail@gmail.com] => 1
        )

    [RecipientsQueue:protected] => Array
        (
        )

    [ReplyToQueue:protected] => Array
        (
        )

    [attachment:protected] => Array
        (
        )

    [CustomHeader:protected] => Array
        (
        )

    [lastMessageID:protected] => 
    [message_type:protected] => plain
    [boundary:protected] => Array
        (
            [1] => b1=_f8dsNdQvDQtBrLj5FRoswBhLHhU4CK838ZV8W1VqI
            [2] => b2=_f8dsNdQvDQtBrLj5FRoswBhLHhU4CK838ZV8W1VqI
            [3] => b3=_f8dsNdQvDQtBrLj5FRoswBhLHhU4CK838ZV8W1VqI
        )

    [language:protected] => Array
        (
        )

    [error_count:protected] => 0
    [sign_cert_file:protected] => 
    [sign_key_file:protected] => 
    [sign_extracerts_file:protected] => 
    [sign_key_pass:protected] => 
    [exceptions:protected] => 1
    [uniqueid:protected] => f8dsNdQvDQtBrLj5FRoswBhLHhU4CK838ZV8W1VqI
)

Here uniqueid at the last is same like message id which I want. But since its protected object, I am not able to retrieve it. Let me know if anyone here can help me for solve the issue. Thanks!

Synchro commented 1 year ago

(ignore earlier response - I answered a different question!)

XL-2000 commented 1 year ago

First of all; the uniqueid is for the internal MIME boundary purposes, it had nothing to do with the last message id you want to get. So, please disregard that route! Next; prerequisite for a message id is that the mail was sent successfully, so you need to be sure it actually does, by checking the boolean result of $mail->send(); Now; setting the message id is the responsibility of the actual SMTP server sending the email. PHPMailer can only retrieve it if it was set properly. If not, PHPMailer can not do anything about that and can not do anything else than returning an empty string.

That said; a second route is to use the transaction id! By calling / echoing $mail->getSMTPInstance()->getLastTransactionID() That might suit your needs and goals and should work in your example data.

iamved777 commented 1 year ago

When I enable $mail->SMTPDebug = 4, Its always showing me like

2023-07-26 15:32:15 CLIENT -> SERVER: Message-ID: <fDFVyMqcOINam0sYNVGdDtEXPhvrxBk2sL0qlmqsj4g@myserver.com>

But when I var_dump($mail)

Message ID getting empty. Even I check in Gmail Show Original, Its there but I am not getting it with any method. I have tried $mail->getSMTPInstance()->getLastTransactionID() but its also empty.

Thanks!

iamved777 commented 1 year ago

I have also marked that

uniqueid =fDFVyMqcOINam0sYNVGdDtEXPhvrxBk2sL0qlmqsj4g
Message-ID (I can seen in Gmail Show Original) = <fDFVyMqcOINam0sYNVGdDtEXPhvrxBk2sL0qlmqsj4g@myserver.com>

so if I can retrieve uniqueid , I can join and use it as Message ID ? If Yes, Please give me the way to retrieve uniqueid From $mail.

Thanks!

ivangomes commented 1 month ago

@iamved777 did you figure it out?