Reputation: 1931
I have been working on a form in a php file. As you'll see from the code below I have written everything in a PHP file including the form tag. This form.php file is being called from another source in my folder called contact.php. I'll admit this is the neatest written code. But for some reason when I submit my information on this form, it tells me that the form has been submitted. Yet I do not receive an email ($email_to) to my billy.farroll@hotmail email address with any of the submitted information. It also works with the google recaptcha and if there is anything invalid entered. Just for some reason it will not send through the data to me i.e. Name, Email, Contact Number or the Enquiry.
Help would be GREATLY appreciated.
My Inline CSS style:
<style>
form span{
color: red;
}
form input[type="submit"]{
background: url('images/logo2.png') no-repeat;
background-size: contain;
overflow: visible;
padding-left: 45px;
height: 20px;
border: 0px;
margin: 0 auto;
margin-top: 5px;
font-family: 'Orbitron', sans-serif;
text-transform: uppercase;
}
.g-recaptcha {
margin: 5px;
}
</style>
My PHP
<?php
if(strpos($_SERVER['REQUEST_URI'], 'form' )!== false){
header('Location:index.php');
}
if($_POST){
$email;
$name;
$captcha;
$telephone;
$question;
if(isset($_POST['email']))
$email=$_POST['email'];
if(isset($_POST['name']))
$name=$_POST['name'];
if(isset($_POST['telephone']))
$telephone=$_POST['telephone'];
if(isset($_POST['message']))
$question=$_POST['message'];
if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email) || ($name == "") ||
($question == "") ||(!$captcha))
{
echo "<p>Invalid Input.</p>";
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>
method="post" style="text-align:left;">
<label>Name: <span>*</span></label><input name="name" type="text" />
<label>Email: <span>*</span></label><input name="email" type="text" />
<label>Contact Number: </label><input name="telephone" type="text" />
<label>Enquiry: <span>*</span></label>
<textarea name="message" rows="10" cols="30"></textarea>
<div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
<input type="submit" value="submit" />
</form>
<?php
} else {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=<!-- SECRET KEY -->=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false)
{
echo '<h2>Spam detected</h2>';
}
else {
$email_from = $_POST["email"];
$email_to = "[email protected]";
$question = $_POST["message"];
$email_subject = "Enquiry";
$headers =
"From: $email_from \n";
"Reply-To: $email_from \n";
$message =
"Name: ". $name .
"\r\nMobile Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\n\r\n\r\n" .
"\r\n\r\nMessage: \r\n" . $question;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);
if ($sent)
{
echo 'Thank you for your enquiry, one of our team will be in contact with you shortly.';
}
}
}
} else {
?>
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">
<label>Name: <span>*</span></label><input name="name" type="text" />
<label>Email: <span>*</span></label><input name="email" type="text" />
<label>Contact Number: </label><input name="telephone" type="text" />
<label>Enquiry: <span>*</span></label>
<textarea name="message" rows="10" cols="30"></textarea>
<div class="g-recaptcha" data-sitekey="<!-- SITEKEY -->"></div>
<input type="submit" value="submit" /><span> * required fields</span>
</form>
<?php } ?>
Upvotes: 0
Views: 75
Reputation: 1931
I found what the problem was, after talking to the the ISP they told me there were slight changes to the authorization of the server. In that, the email address that sends the information has to be verifed on the server i.e. $email_from ="[email protected]" -- has to be a verifed email address so that it has the authorization to send the data through to wherever you'd like it to be sent to i.e. $email_to = "[email protected]" Also, in the $sent variable the $email had to change to $email_from (due to the authorization situation). See code below, is working successfully now:
OLD CODE:
$email_from = $_POST["email"];
$email_to = "[email protected]";
$question = $_POST["message"];
$email_subject = "Enquiry";
$headers =
"From: $email_from \n";
"Reply-To: $email_from \n";
$message =
"Name: ". $name .
"\r\nMobile Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\n\r\n\r\n" .
"\r\n\r\nMessage: \r\n" . $question;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email);
if ($sent)
NEW CODE:
$email_from = "[email protected]"; // This email address has to be the same email on the server if using Fasthots server i.e. strawberry server - [email protected] - SENDS THE EMAIL
$email_to = "[email protected]"; // Where the email is being sent to
$question = $_POST["message"];
$email_subject = "Enquiry";
$headers =
"From: $email \n"; // This is what's shown in the receiving message mail the email variable is essential here because it's whats entered by user
"Reply-To: $email \n"; // This is what's shown in the receiving message mail the email variable is essential here because it's whats entered by user
$message =
"Name: ". $name .
"\r\nMobile Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\n\r\n\r\n" .
"\r\n\r\nMessage: \r\n" . $question;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
Upvotes: 1
Reputation: 1466
Your problem is most probably in the <form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post" style="text-align:left;">
you can:
completely remove the action
at least surround the <?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>
with double quotes ""
use action=""
Upvotes: 3