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'];
}