76

I'm using phpmailer on my website and to help with spam issues I have created a mailbox to send these emails from (using SMTP).

I have set the emails to come from the mailbox address and then I have added a reply to address for where I want the replies to go to:

$mail = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth   = true;
$mail->SMTPSecure = 'tsl';
$mail->SMTPDebug  = 1;
$mail->Host       = EMAIL_HOST;
$mail->Port       = EMAIL_PORT;
$mail->Username   = EMAIL_USER;
$mail->Password   = EMAIL_PASS;

$mail->SetFrom('mailbox@email.com', 'Mailbox name');
$mail->AddReplyTo('replyto@email.com', 'Reply to name');
$mail->AddAddress('user@email.com', 'User name);

The emails send successfully and seem to get through the spam filters ok, but when I press reply it includes both the mailbox account and the reply to account.

Is this what is meant to happen? I only want the reply to address to appear when you press reply. Is this even possible?


Edit:

Looking at the email headers it seems like the from address is getting included in the reply to field. I have no idea why!

Date: Tue, 1 May 2012 11:16:25 +0100
To: User name <user@email.com>
From: Mailbox name <mailbox@email.com>
Reply-to: Mailbox name <mailbox@email.com>, Reply to name <replyto@email.com
Subject: Email subject
Message-ID: <54c530c0d1f3ff33fc87c4c41c2c9ffd@localhost>
X-Priority: 3
X-Mailer: PHPMailer 5.1 (phpmailer.sourceforge.net)
MIME-Version: 1.0
Content-Type: multipart/alternative;
     boundary="b1_54c530c0d1f3ff33fc87c4c41c2c9ffd"

--b1_54c530c0d1f3ff33fc87c4c41c2c9ffd
Content-Type: text/plain; charset = "iso-8859-1"
Content-Transfer-Encoding: 8bit
starball
  • 20,030
  • 7
  • 43
  • 238
chapmanio
  • 2,901
  • 3
  • 20
  • 22
  • not what i would expect to happen, sounds like an issue with your mail client, try another one to see if this is the case –  May 01 '12 at 10:14
  • Can you examine the raw headers of one of the email the script is generating? That would help explain things. – Stuart May 01 '12 at 10:14
  • 2
    It seems to be happening with all mail clients, looking at the headers it is defining the reply-to with both addresses: `Reply-to: Mailbox name , Reply to name ` I wonder why that is happening. – chapmanio May 01 '12 at 10:20
  • hunt down the AddReplyTo() function –  May 01 '12 at 10:33
  • 3
    I can't answer my own question yet but looking at the phpmailer code it is because the AddReplyTo code was appearing **after** the SetFrom code, and phpmailer adds the SetFrom address to the replyto list if it hasn't already been defined. – chapmanio May 01 '12 at 10:45

2 Answers2

171

I have found the answer to this, and it is annoyingly/frustratingly simple! Basically the reply to addresses needed to be added before the from address as such:

$mail->addReplyTo('replyto@email.com', 'Reply to name');
$mail->SetFrom('mailbox@email.com', 'Mailbox name');

Looking at the phpmailer code in more detail this is the offending line:

public function SetFrom($address, $name = '',$auto=1) {
   $address = trim($address);
   $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
   if (!self::ValidateAddress($address)) {
     $this->SetError($this->Lang('invalid_address').': '. $address);
     if ($this->exceptions) {
       throw new phpmailerException($this->Lang('invalid_address').': '.$address);
     }
     echo $this->Lang('invalid_address').': '.$address;
     return false;
   }
   $this->From = $address;
   $this->FromName = $name;
   if ($auto) {
      if (empty($this->ReplyTo)) {
         $this->AddAnAddress('ReplyTo', $address, $name);
      }
      if (empty($this->Sender)) {
         $this->Sender = $address;
      }
   }
   return true;
}

Specifically this line:

if (empty($this->ReplyTo)) {
   $this->AddAnAddress('ReplyTo', $address, $name);
}

Thanks for your help everyone!

Ivan
  • 1,274
  • 16
  • 22
chapmanio
  • 2,901
  • 3
  • 20
  • 22
  • 4
    You probably use an early version. This is not the good answer any more. I tried different ways, phpmailer seems to be more robust than my cunny tricks... Your input seems wrong "$this->AddReplyTo" should be "$smth->addReplyTo". That is, do not use common words (like $this), and always check spelling, in this case it's "addReplyTo" starting lowercase "add" – Otvazhnii Jun 04 '14 at 10:19
  • 1
    Indeed, this buggy behaviour was removed from PHPMailer years ago, so this answer no longer applies. – Synchro Mar 02 '16 at 13:46
45

At least in the current versions of PHPMailers, there's a function clearReplyTos() to empty the reply-to array.

    $mail->ClearReplyTos();
    $mail->addReplyTo(example@example.com, 'EXAMPLE');
Fabian Peter
  • 466
  • 5
  • 2