Spam, or not received at all, from mail form (new title: Please, check out my mail form)
Best Adobe Support Community,
Messages sent from my mail form either ended up in spam or was not received at all. It turned out that "$headers = "From: $name <$email>" was the problem, because a message through a mail form is not necessarily sent from the users email provider (obviously). Hence the domain address entered is different from which it is sent. No wonder...
Before you check out my code observe that x's represent secret information and that "domain.com" is nonspecific.
So I changed to the following (I also included $email in the $message):
$to = "xxx@domain.com";
$subject = "Message from visit at xxx@domain.com";
$message = "From:"."\r\n".$email."\r\n"."Message:"."\r\n".$message;
$headers = "reply-to: $email";
Edit: A new problem is the message field... I tried writing a longer message that was not received at all... I also tried adding multiple periods and apostrophes, which also turned out blank. So, please, explain what has to be changed regarding the result of the function "test_input" concerning the message field.
I really call for for a careful examination of the whole PHP down below. Thank you so much on beforehand:
<?php
/* variable definition */
$name = $email = $message = $submit = $recaptcha = "";
$errors = $success = array();
/* end of "variable definition" */
if (isset($_POST["submit"])) {
/* when submit is pressed */
/* test_input function definition */
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/* end of "test_input function definition" */
/* process the name field */
if (empty($_POST["name"])) {
$errors['name'] = "A name must be entered";
}
else {
$name = test_input($_POST["name"]);
if (preg_match("/^[a-zA-Z ]*$/", $name) == FALSE) {
$errors['name'] = "Only letters, except åäöÅÄÖ, and blank space are allowed.";
}
}
/* end of "process the name field" */
/* process the email field */
if (empty($_POST["email"])) {
$errors['email'] = "An email address must be entered.";
}
else {
$email = test_input($_POST["email"]);
if (filter_var($email, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE) == FALSE) {
$errors['email'] = "Wrong email format.";
}
}
/* end of "process the email field" */
/* process the message field */
if (empty($_POST["message"])) {
$errors['message'] = "A message must be entered.";
}
else {
$message = test_input($_POST["message"]);
}
/* end of "process the message field" */
/* check google recaptcha box */
if (empty($_POST["g-recaptcha-response"])) {
$errors['recaptcha'] = "Confirm that you are not a robot!";
}
else {
$recaptcha = $_POST["g-recaptcha-response"];
$secret = "xxx";
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$recaptcha));
if ($response->success) {
}
else {
$errors['recaptcha'] = "Robot verification failed!\r\nPlease try again!";
}
}
/* end of "check google recaptcha box" */
/* if no errors */
if (count($errors) == 0) {
$to = "xxx@domain.com";
$subject = "Message from visit at xxx@domain.com";
$message = "From:"."\r\n".$email."\r\n"."Message:"."\r\n".$message;
$headers = "reply-to: $email";
if (mail($to, $subject, $message, $headers) == TRUE) {
$success['success'] = "Message was sent successfully!";
/* field input is removed when successful */
$name = $email = $message = "";
/* end of "field input is removed when succesful" */
}
else {
$errors['mail'] = "Something went wrong!\r\nPlease try again!";
}
}
/* end of "if no errors" */
/* end of "when submit is pressed" */
}
?>
[Edited by moderator to restore default text and code styles.]
