Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
842 views
in Technique[技术] by (71.8m points)

php - phpmailer: Reply using only "Reply To" address

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('/[
]+/', '', $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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...