John Davidson

My php form doesn't allow me to reply to the sender

0 comments
Message:


I receive emails from clients normally without issues, but I can't reply to the client email, the reply goes only to my emails. I did put reply to header and not working.


This is the code I am using:


<?php

$from = ' <[email protected]>';
$sendTo = ' <[email protected]>';
$subject = 'New Mail From';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message');

$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';

error_reporting(E_ALL & ~E_NOTICE);

try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');

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

$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $headers,
'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'];
}


My Answer:
is used to collect user input and send it to a server for processing. It typically includes fields for users to enter their information, such as name, email, and message. The form can also include validation to ensure that the data entered is in the correct format.

Once the user submits the form, the data is sent to a server-side script written in PHP, which processes the information and can store it in a database, send an email, or perform other actions based on the user input.

Overall, a PHP form is a crucial tool for collecting and processing user input on a website.

Rate this post

4 of 5 based on 8192 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us