0

I have two problems currently:

  1. In my gmail, the From: header doesn't respect the 'My contact form' name. It's defaulting to something else.

  2. When I receive the email in gmail, it is of course being sent from myemail@mydomain.com (to avoid the spam folder), but when I click reply, I want it to use the contact form user's $email, not myemail@mydomain.com

I'm sure I am missing some details so if I missed something let me know and I will add it.

Here's my code:

if (!isset($_REQUEST['name']) || !isset($_REQUEST['email']) || !isset($_REQUEST['message'])) {
  die();
}

// PHP parameters
$to = "myemail@gmail.com";
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// Subject
$email_subject = $_POST['name'] . ' ' . 'is contacting you from mywebsite.com' . '!';


// body of email
$body = ""

$body = wordwrap($body, 60, "\n");

// Headers
$headers .= "From: My contact form <myemail@mydomain.com>" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Return-Path: myemail@mydomain.com" . "\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 

// Mail it
mail( $to, $email_subject, $body, $headers, "-fmyemail@mydomain.com" );
filype
  • 8,034
  • 10
  • 40
  • 66
Jefe
  • 67
  • 6

2 Answers2

1

There's a line in there missing a concatenation symbol (.) which is causing $headers lose the values From and Reply-To as they are set before the line where $header is re-initialized.

In your code the line that re-initializes $headers is:

$headers = "MIME-Version: 1.0" . "\r\n";

It should be changed to:

$headers  = "From: My contact form <myemail@mydomain.com>" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Return-Path: myemail@mydomain.com" . "\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 
filype
  • 8,034
  • 10
  • 40
  • 66
1

For part 1, the = sets the variable, and then .= adds on to the variable. So, you are overwriting your previously declared variable. It should read:

$headers = "From: My contact form <myemail@mydomain.com>" . "\r\n";
$headers .= "Reply-To: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "Return-Path: myemail@mydomain.com" . "\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n"; 

For part 2, try this: reply-to address in php contact form

In other words, change

$headers .= "Reply-To: $email" . "\r\n";

to

$headers .= "Reply-To: " . $email . "\r\n";
Community
  • 1
  • 1
taykay08
  • 341
  • 4
  • 9
  • Genius! That fixed my 2 problems.....but...unfortunately reintroduced a previous problem I was having where I was getting a softfail in my headers saying that it wasn't a permitted sender....any ideas? – Jefe May 30 '14 at 03:40
  • I'm not as familiar with that error, but it looks like this question might be related & has a few good responses: http://stackoverflow.com/questions/6201396/spf-issue-what-causes-softfail Hope that helps! – taykay08 May 30 '14 at 04:02
  • 1
    Thought I'd post that I finally got my SPF to pass. This link pointed me in the right direction: (http://www.x-pose.org/2013/10/how-to-designate-an-ip-address-as-permitted-sender) – Jefe Jun 04 '14 at 17:05