0

Sorry if this may seem stupid but my PHP knowledge is somewhere near the "0" mark. I've built a website and am using a rather simple PHP contact form as per needs. However I still have some issues which I am not able to really solve.

For instance, when I receive the email I would like to be able to reply to the person who sent the email, basically be able to reply to his/her added email address. Right now the PHP is configured to have a "from" and "to" containing my server side email(s). I will paste below the code. Whenever I try to hit "Reply" I am basically replying to my server side email address.

Is there a way I can do that through the code I pasted below?

And the other question is related to Gmail. When I get an email everything is put inline so I get something like: "Name - Name, Surname - Surname, Email - Email, Message - Message". I have a redirection put on the server side email so I can get the email on a gmail address as well. However, if I am viewing the email on the server side, through webmail, is properly formatted, each line on its separate row.

Can I achieve that on Gmail?

Here is my PHP code

<?php

// configure
$from = 'contact@mydomain.com';
$sendTo = 'contact@mydomain.com';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in the email
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

// let's do the sending

try
{
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    $headers = array('Content-Type: text/html; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}
Robert
  • 9
  • 4
  • The line ending issue would probably be solved by changing `\n` to `\r\n`. You can read more about it here: http://stackoverflow.com/questions/1761051/difference-between-n-and-r – M. Eriksson Feb 25 '17 at 16:30
  • thanks a lot for the reply, it was useful ! :) – Robert Feb 25 '17 at 23:27

1 Answers1

1

Instead using hard-coded "From" you should receive user's email and set it as 'from'.

I guess that in your form you have email field with name email. So change from

'From: ' . $from,

to

'From: ' . $_POST['email'],

Now header From: should contain provided user's email. In my opinion your code is weird but this fix your problem.

Wolen
  • 874
  • 6
  • 15
  • Thanks a lot ! It worked like a charm. I added the $_POST['email'] to "Reply-To" as well since I wanted to be able to catch the sender's email address when clicking on the reply button. I accepted your answer, I would've upvoted it but I don't have enough rep. – Robert Feb 25 '17 at 23:15