Search code examples
phpemailphpmaileremail-bounces

PHPMailer bounced mails not getting bounced right


I have written a PHP script, which sends mails. I'm sending them from "[email protected]" and have also set the "Return-Path" to "[email protected]", but I'm still getting bounced mails to senders mail ("[email protected]").

Here is stripped down code:

$this->mail = new PHPMailer();
$this->mail->isSMTP();
$this->mail->Host = 'host';
$this->mail->SMTPAuth = true;
$this->mail->Username = '[email protected]';
$this->mail->Password = 'pass';
$this->mail->SMTPSecure = 'tls';
$this->mail->Port = 25;
$this->mail->ReturnPath = '[email protected]';
$this->mail->From = '[email protected]';
$this->mail->send();

How could I force the bounced mails to go to the bounce mail account? Thanks for any help!


Solution

  • Don't use ReturnPath - set Sender instead. Support for the ReturnPath property was recently disabled in PHPMailer (in version 5.2.8) because it's invalid to set it at the point of sending. The return path is added by the receiver when it receives the message, and is set by putting your desired return path into the Sender property, which gets passed as the envelope sender during the SMTP conversation. Sender is set automatically when you call setFrom, but you can override it and just set it directly, like this:

    $this->mail = new PHPMailer();
    $this->mail->isSMTP();
    $this->mail->Host = 'host';
    $this->mail->SMTPAuth = true;
    $this->mail->Username = '[email protected]';
    $this->mail->Password = 'pass';
    $this->mail->SMTPSecure = 'tls';
    $this->mail->Port = 25;
    $this->mail->setFrom('[email protected]');
    $this->mail->Sender = '[email protected]';
    $this->mail->send();