I have a form validation email script which is using FROM email address entered to "Email" field; unfortunately my hosting accepting only one specific email address: no-reply@myhost.com
How I can specify "custom" FROM email address in this php script:
<?php
if($_POST)
{
require('constant.php');
$user_name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$user_phone = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
$content = filter_var($_POST["content"], FILTER_SANITIZE_STRING);
if(empty($user_name)) {
$empty[] = "<b>Name</b>";
}
if(empty($user_email)) {
$empty[] = "<b>Email</b>";
}
if(empty($user_phone)) {
$empty[] = "<b>Phone Number</b>";
}
if(empty($content)) {
$empty[] = "<b>Comments</b>";
}
if(!empty($empty)) {
$output = json_encode(array('type'=>'error', 'text' => implode(", ",$empty) . ' Required!'));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => '<b>'.$user_email.'</b> is an invalid Email, please correct it.'));
die($output);
}
//reCAPTCHA validation
if (isset($_POST['g-recaptcha-response'])) {
require('component/recaptcha/src/autoload.php');
$recaptcha = new \ReCaptcha\ReCaptcha(SECRET_KEY, new \ReCaptcha\RequestMethod\SocketPost());
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$resp->isSuccess()) {
$output = json_encode(array('type'=>'error', 'text' => '<b>Captcha</b> Validation Required!'));
die($output);
}
}
$toEmail = "myemail@gmail.com";
$mailHeaders = "From: " . $user_name . "<" . $user_email . ">
";
if (mail($toEmail, "Contact Mail", $content, $mailHeaders)) {
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .', thank you for the comments. We will get back to you shortly.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
die($output);
}
}
?>
I have tried to replace :
$mailHeaders = "From: " . $user_name . "<" . $user_email . ">
";
with:
$mailHeaders = "From: no-reply@myhost.com>
";
but it did not work; Sorry, my PHP knowledge is very small :(
Thank you in advance.
$mailHeaders = "From: no-reply <no-reply@myhost.com>
";
fixed the issue, just make sure there is space between no-reply and <no-reply@myhost.com>
but now we lost "phone" instance- how I can combine
$user_phone = filter_var($_POST["phone"], FILTER_SANITIZE_STRING);
$content = filter_var($_POST["content"], FILTER_SANITIZE_STRING);
together?
found a solution by adding :
$mail_body = $content;
$mail_body .= "
Name: ".$user_name; //sender name
$mail_body .="
E-Mail: ".$user_email; //sender email
$mail_body .="
Phone: ".$user_phone; //sender phone
$mail_body .="
";
if (mail($toEmail, "Contact Mail", $mail_body, $mailHeaders)) {