Search code examples
phpsmtpgmailphpmailer

Change Message-ID PHPMailer


Hello I am making a php based mailing application that will connect with a external smtp server and send emails. Now I have managed to match everything but the Message-ID's @domain-name and Sender domain name are not matching...

This is the result I am getting : Wrong Message ID Header

and this is the result I should be getting (this email is sent from Mailwizz connected with the same SMTP server I am trying to connect with my application)

Expected Message ID Header

send.php file which I am using to connect with SMTP using PHPMailer


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp*****************';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]***********';
$mail->Password = '**********';
$mail->SMTPSecure = 'tls';
$mail->Port = 80;

$mail->SetFrom('jon@al***********','John Adams');
$mail->Sender = '[email protected]*********';
$mail->addAddress('*********@gmail.com');
$mail->Subject = 'Hello This is a TEST FROM SMTP';
$mail->isHTML(false);
$mail->Body = 'Hello let me know when its received';
$mail->addCustomHeader('X-Sender', '[email protected]**********');
$mail->XMailer=null;
$mail->MessageID = md5('HELLO'.(idate("U")-1000000000).uniqid()).'-'.$type.'-'.$id.'@send.al*********';
if(!$mail->send()){
    echo 'Error is '.$mail->ErrorInfo;
}
else{
    echo 'Message has been sent!';
}
?>

Solution

  • In my experience, you do not need to use addCustomHeader if you want to set the MessageID.

    Assuming that you want to set the Message ID to be [random]@send.alok, then please use the following:

    $mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'@send.alok>';
    

    Hence, please the following will be ok:

    <?php
    use PHPMailer\PHPMailer\PHPMailer; 
    use PHPMailer\PHPMailer\Exception; 
    
    require './Exception.php'; 
    require './PHPMailer.php'; 
    require './SMTP.php'; 
    
    
    
    $user='smtp_xxxxxx_user';
    $pass='smtp_password';
    $mail = new PHPMailer(true);                              
    
    try { 
    
        $mail->CharSet ="UTF-8";       
        $mail->SMTPDebug = 0;                      
        $mail->isSMTP();                             
        $mail->Host = 'smtp.xxxxx.com';        
    
        $mail->Username = $user;               
        $mail->Password = $pass;             
    
    
    $mail->MessageID = "<" . md5('HELLO'.(idate("U")-1000000000).uniqid()).'@send.alok>';
    
    
      $mail->setFrom('[email protected]', 'Jon Al');  
      $mail->addAddress('[email protected]');  
    
    $subject="test";
    $message="test123";
    
        $mail->Port = 25;                             
        $mail->isHTML(true);                                  
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $mail->send(); 
    //    echo 'Success'; 
    } catch (Exception $e) { 
    //echo 'Failed'; 
    }
    
    ?>
    
    

    You may refer to the screen dump below for the result

    Email received