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 something@gmail.com, but on reply (human or robot as bounce email) to always reply to noreply@custom.com
No matter what I do, the bounced email always delives to something@gmail.com instead of noreply@custom.com
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 = "something@gmail.com"; // 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('something@gmail.com');
$mail->addReplyTo('noreply@custom.com');
$mail->Sender = 'noreply@custom.com';
// Recipient
$mail->addAddress('fndsgfds@fsscd.com');
// 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
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 = 'noreply@custom.com';
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.