email - How to display the domain name in the subject line using an exiting PHP form
Get the solution ↓↓↓Current form works great, and is implemented across numerous domains, however the subject line is the same. I would like to deploy a form across multiple domains w/out having to manually enter “SPECIFIC_DOMAIN.com” to each form subject line. Is there a way to have the Subject line denote which URL the contact form is coming from dynamically? Current subject line is static: “Contact Mail”. Thanks in advance.
<?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);
$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 = "[email protected]";
$mailHeaders = "From: " . $user_name . "<" . $user_email . ">\r\n";
$mailBody = "User Name: " . $user_name . "\n";
$mailBody .= "User Email: " . $user_email . "\n";
$mailBody .= "Phone: " . $user_phone . "\n";
$mailBody .= "Content: " . $content . "\n";
if (mail($toEmail, "Contact Mail", $mailBody, $mailHeaders)) {
$output = json_encode(array('type'=>'message', 'text' => 'Hey there '.$user_name .'! Your message is on the way. Cheers.'));
die($output);
} else {
$output = json_encode(array('type'=>'error', 'text' => 'Unable to send email, please contact'.SENDER_EMAIL));
die($output);
}
}
?>
Answer
Solution:
Instead of using:
mail($toEmail, "Contact Mail", $mailBody, $mailHeaders)
use:
mail($toEmail, "Contact Mail from " . $_SERVER["SERVER_NAME"], $mailBody, $mailHeaders)
See: $_SERVER
Note that$_SERVER["HTTP_HOST"]
is set by the client, which means it is unreliable, whereas$_SERVER["SERVER_NAME"]
is set by the server.
Answer
Solution:
I think you can do this simply by changing:
if (mail($toEmail, "Contact Mail", $mailBody, $mailHeaders)) {
To
$sub = $_SERVER['HTTP_HOST'];
if (mail($toEmail, $sub, $mailBody, $mailHeaders)) {
You might want to consider using something like:
$sub = 'Mail from ' . $_SERVER['HTTP_HOST'];
instead though.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot access offset of type string on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.