1

On my site when my customers fill out the form I receive the e-mail but the From section instead of showing what the customer input in the form I receive it as "my.hosting.user.name@inmotionhosting.com". I would like to fix this so that I can directly reply to the e-mail the customer input into the form. Here is what my contact.php looks like below.

<?php
// variables start
$name = "";
$email = "";
$message = "";

$name =  trim($_POST['contactNameField']);
$email =  trim($_POST['contactEmailField']);
$message =  trim($_POST['contactMessageTextarea']);
// variables end

// email address starts
$emailAddress = 'info@mydomain.com';
// email address ends

$subject = "mydomain.com | Mobile - Message From: $name";   
$message = "<strong>From:</strong> $name <br/> <strong>E-Mail:</strong> $email </br><br/> <strong>Message:</strong> $message";

$headers = 'From: '. $name . '<' . $email . '>' . "\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";

//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
törzsmókus
  • 1,799
  • 2
  • 21
  • 28
  • info@mydomain has been replaced with my actual e-mail just did not want to post my actual email on a public forum. – user6505472 Jun 23 '16 at 17:18
  • 1
    You definitely should **sanitize your inputs**, i.e. do something to filter out malicious / incorrect input. For example: with this form, anyone could inject any additional mail headers just by inserting a line break into their name or email address! See also for fun: https://xkcd.com/327/ – törzsmókus Jun 23 '16 at 17:31
  • (also: no need to say 'Hello' or 'Thanks', they are just distracting. I edited those sentences out of your question, it will be visible after review.) – törzsmókus Jun 23 '16 at 17:46

1 Answers1

0

An assignment to a variable (e.g. $headers = …) changes its value rather than appending to it. You should use the operator .= after initializing the variable first:

$headers = "";
$headers .= 'From' . $name . …

etc.

törzsmókus
  • 1,799
  • 2
  • 21
  • 28
  • im fairly new to php could you give me an exact code of what im suppose to change? just adding the .= instead of = should fix it? – user6505472 Jun 23 '16 at 17:35
  • you should only change `=` to `.=` where you are _adding to_ the string (as far as I can see, only for `$headers`). – törzsmókus Jun 23 '16 at 17:40
  • in fact, `$headers .= "something"` is just a shorter way to say `$headers = $headers . "something"`. – törzsmókus Jun 23 '16 at 17:42
  • for some reason when I add .= to the = signs next to headers the e-mail does not send out at all – user6505472 Jun 23 '16 at 18:04
  • No, no error message I just dont recieve the e-mail in my inbox. There has too be something I could change to make this all work. – user6505472 Jun 23 '16 at 18:23
  • I think the problem is that your server does not let you send an email with a custom `From` header. this is sad but normal (see e.g. [this question](http://stackoverflow.com/questions/2014081/problem-with-php-mail-from-header)). – törzsmókus Jun 23 '16 at 18:32
  • there is one more thing to try: use double quotes around the name like this: `$headers = 'From: "'. $name . '" <' . $email . '>' . "\r\n";` – the `From` header might be invalid without them – törzsmókus Jun 23 '16 at 18:34