Reputation: 13
I am trying out PHP after years of inactivity, and I thought I had it but looks like I lost the touch. Can anyone see why I might not be getting any data sent to my email address?
HTML code:
<form name="Call Back Request" id="request-call-form" action="callbackrequest.php" method="POST" novalidate="novalidate">
<div class="col-md-6">
<input id="name" type="text" placeholder="Name" name="name">
<input id="email" type="text" placeholder="Email" name="email">
</div>
<div class="col-md-6">
<input id="phone" type="text" placeholder="Phone" name="phone">
<input id="subject" type="text" placeholder="Subject" name="subject">
</div>
<div class="col-md-6">
<button type="submit" value="submit" class="thm-btn"><a href="https://rootlayertechnologies.com.au/callbackrequest.php">submit now</a></button>
</div>
<div class="col-md-12">
<div id="success"></div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST['email'])) {
$email_to = "[email protected]";
$email_subject = "Call Back Request Form - Home Page | rootlayertechnologies.com.au";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors and submit the form again.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['phone']) || !isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['subject']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if (strlen($subject) < 2) {
$error_message .= 'The Subject you entered does not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = [
"content-type",
"bcc:",
"to:",
"cc:",
"href"
];
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Phone: " . clean_string($phone) . "\n";
$email_message .= "Subject: " . clean_string($subject) . "\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n"
. 'Reply-To: ' . $email_from . "\r\n"
. 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<p>Thanks for contacting us. We have received your request for a call back and a friendly member of our Product Solutions team will be in touch with you soon.</p>
<?php}?>
Upvotes: 1
Views: 57
Reputation: 116
I would begin by removing the <a>
tag inside the <submit>
button. I think that by clicking the link, you may be just opening the page instead of submitting the form.
Then, on the PHP code, i would confirm that $email_to
is properly set. (It may seem obvious, but it's always worth confirming.)
I couldn't test the code, but i didn't find any bugs per se. (Some things could be improved, e.g. lack of filter_var
and perhaps html_escape
, addslashes
, or strip_tags
.)
This leads me to think that the problem may be at the server level. So, here are some things to consider:
From:
must be an address from the same domain as the site (e.g. [email protected]). Keep Reply-to:
as is, though.mail
. You may need to implement SMTP.Upvotes: 3