Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpmailer: Reply using only "Reply To" address

Tags:

php

phpmailer

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('[email protected]', 'Mailbox name'); $mail->AddReplyTo('[email protected]', 'Reply to name'); $mail->AddAddress('[email protected]', '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?

Many thanks in advance for any help offered!


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 <[email protected]> From: Mailbox name <[email protected]> Reply-to: Mailbox name <[email protected]>, Reply to name <[email protected] 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 
like image 921
chapmanio Avatar asked May 01 '12 10:05

chapmanio


1 Answers

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('[email protected]', 'Reply to name'); $mail->SetFrom('[email protected]', '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!

like image 97
chapmanio Avatar answered Oct 03 '22 22:10

chapmanio