PHPMailer how to set bounce email address

2.4k Views Asked by At

i know there are plenty of guides, but i tried so many of them with no result. I also tried to reshuffle the parameters so sender goes before replyTo and vice versa, etc, but still.

The idea is to appear to the recpient as it came from [email protected], but on reply (human or robot as bounce email) to always reply to [email protected]

No matter what I do, the bounced email always delives to [email protected] instead of [email protected]

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

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

try {
    $mail = new PHPMailer(true);

    $mail->CharSet = 'UTF-8';                                   // UTF8 Encoding
    $mail->Encoding = 'base64';                                 // Needs to be set with UTF8
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                        // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = "smtp.gmail.com";                 // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = "[email protected]";                     // SMTP username
    $mail->Password   = "somePassword";                // SMTP password
    $mail->SMTPSecure = "tls";
    $mail->Port       = 587;                 // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Sender
    $mail->setFrom('[email protected]');
    $mail->addReplyTo('[email protected]');
    $mail->Sender = '[email protected]';

    // Recipient
    $mail->addAddress('[email protected]');

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = "test bouncing";
    $mail->Body    = "teting";
    //$mail->AltBody = strip_tags($row_campaign['text']);
                                            
    $mail->MessageID = $messageId; // removed from example here, but is stated above
    $mail->addCustomHeader('In-Reply-To', $messageId);

    $mail->send();
}
catch (Exception $e) {
    echo "Error: {$mail->ErrorInfo}";
    die();
}
unset($mail);

Any idea where is the problem? PHPMailer 6.1.6

1

There are 1 best solutions below

0
Synchro On BEST ANSWER

The bounce email address is the SMTP MAIL FROM address, also known as the envelope sender. This is often the same as the From address (and PHPMailer uses the from address as the sender by default), but it is entirely normal for it to be different (subject to DMARC config). In PHPMailer you set it by setting the Sender property, as you are doing in this line:

$mail->Sender = '[email protected]';

When a server receives a message, it takes the envelope sender and adds it to the message in a Return-Path header; this is not something that a sending server should ever do.

So to change the bounce address, change that setting; it is mostly independent of from and reply-to addresses. Note however that gmail may ignore this if you have not configured it as an alias for your gmail username.