4

I have an SMTP question. I have created a PHP script to send out emails. The requirement is that I need to send email from 'email1@example.com' but I need replies to come to 'email2@example.com'

I have added email2@example.com in the reply-to header field. The only problem I am having is that when someone receives the email and clicks on the reply button, both email1@example.com and email2@example.com are being shown in the TO field.

Is there any way that I can have email1@example.com removed from the TO field and only show the email address that was specified in the reply-to field?

I am using PHPMailer and the code is below:

    $this->phpmailer->IsSMTP();
    $this->phpmailer->Host = $server;
    $this->phpmailer->Port = $port;
    $this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1@example.com
    $this->phpmailer->AddReplyTo($replyEmail,$fromName);  //this is email2@example.com
    $this->phpmailer->Subject = $subject;
    $this->phpmailer->AltBody = $msgTXT; // non-html text
    $this->phpmailer->MsgHTML($msgHTML); // html body-text
    $this->phpmailer->AddAddress($email);
Undefined Variable
  • 4,196
  • 10
  • 40
  • 69

2 Answers2

9

Try:

$this->phpmailer->IsSMTP();
$this->phpmailer->Host = $server;
$this->phpmailer->Port = $port;
$this->phpmailer->AddReplyTo($replyEmail,$fromName);  //this is email2@example.com
$this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1@example.com
$this->phpmailer->Subject = $subject;
$this->phpmailer->MsgHTML($msgHTML); // html body-text
$this->phpmailer->AddAddress($email);

Try setting AddReplyTo() first before SetFrom. phpmailer needs to change this behavior. It adds the from address to the reply-to field. If you set reply-to first before the from address, it will not be able to add your from address to the reply-to header.

rationalboss
  • 5,330
  • 3
  • 30
  • 50
  • That sounds like a good idea, I will check it and let you know how it goes – Undefined Variable Aug 27 '12 at 16:09
  • yeah but it's kind of a "hack" so you don't need to edit phpmailer. If you just need to send a normal email, you can also stop using phpmailer completely and do this: mail('email1@example.com','mail subject','the email body',"From: from.address@example.com\nReply-to: the.reply@address.here"); but your server must be configured to use mail(). – rationalboss Aug 27 '12 at 16:12
  • 1
    Just to note, the call to $this->phpmailer->MsgHTML($msgHTML) sets the AltBody at the same time, so will effectively wipe out the AltBody set in the previous line – bumperbox Jan 05 '16 at 20:27
-1

From a google search and first result

Adding a Reply-To Address ^

By default the Reply-To address will be the FROM address unless you specify otherwise. That is simply E-Mail client intelligence. However, you can have the E-Mail come from one E-Mail address and any replies go to a different one. Here's how:

$mailer->AddReplyTo('billing@yourdomain.com', 'Billing Department');

NOTE: You can have multiple Reply-To addresses, just duplicate the line in the previous code example and change the E-Mail address on each line.

You will need to add this line before your from address. Please check out this for the solution to the same problem.

Following these examples, your code should look like

$this->phpmailer->IsSMTP();
$this->phpmailer->Host = $server;
$this->phpmailer->Port = $port;
$this->phpmailer->AddReplyTo($replyEmail,$fromName);  //this is email2@example.com
$this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1@example.com
$this->phpmailer->Subject = $subject;
$this->phpmailer->AltBody = $msgTXT; // non-html text
$this->phpmailer->MsgHTML($msgHTML); // html body-text
$this->phpmailer->AddAddress($email);
Community
  • 1
  • 1
bretterer
  • 5,693
  • 5
  • 32
  • 53