The form only works if the user submitting the form is using a .com email address. If the user is using a .com address I receive an email from them containing their form information (content). But, if anyone with another email address for example .co.uk submits the form, it does not send an email to myself (billy#hotmail.co.uk), please help. The enquiry.php code is below:
<?php
$email_to = "billy#hotmail.com";
$firstname = $_POST["firstname"];
$surname = $_POST["surname"];
$telephone = $_POST["telephone"];
$email = $_POST["email"];
$time = $_POST["time"];
if (empty($firstname)) {
show_error("Please fill in your Name - hit back in the browser to correct");
}
if (empty($surname)) {
show_error("Please fill in your Surname - hit back in the browser to correct");
}
if (empty($email)) {
show_error("Please fill in your Email Address - hit back in the browser to correct");
}
if (empty($telephone)) {
show_error("Please fill in your Telephone Number - hit back in the browser to correct");
}
if (empty($time)) {
show_error("Please select a collection time slot - hit back in the browser to correct");
}
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
show_error("E-mail address not valid");
}
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Easter 2018 Order Form";
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message =
"First Name: ". $firstname .
"\r\nSurname: " . $surname .
"\r\nTelephone Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\nTime Slot: " . $time .
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f"
.$email_from);
if ($sent) {
header("location:
http://www.billyfarroll.co.uk/thank-you.html");
} else {
echo "There has been an error sending your comments. Please try later.";
}
function check_input($data, $problem='') {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0) {
show_error($problem);
}
return $data;
}
function show_error($myError) {
?>
OK the problem is with your regex.
The thing is that when you use this regex
([\w\-]+\#[\w\-]+\.[\w\-]+)
it will match a result on these kind of address mail
billy#hotmail.co.uk
but the matched result will be
billy#hotmail.co
because you're askin for a_word#a_word.a_word
So the email address is valid according to your code, but isn't existing
You have to recheck your regex.
Maybe try
("/.*#.*\..*/")
Related
I have a working email script. Now I want to add the senders email in CC, so they get a copy of the email as a result. So I added the $headers part to it and in the email on the receiving end it shows as being CC'd, but the CC'd email does not actually get the email. Any thoughts?
<?php
if($_POST) {
$yourEmail = "myemail#business.com";
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$headers = 'From:' . $email . "\r\n" . 'Cc:' . $email . "\r\n";
$responseRefresh = "Redirecting in 5 seconds.<br>";
$responseFillData = "Please fill all the data.<br>";
$responseValidEmail = "Please enter valid email.<br>";
$responseCompleted = "Form successfully submitted.<br>";
$responseFailed = "Form failed to be submitted.<br>";
$response = "" . $responseRefresh;
$hasError = false;
if($fname == "" || $lname == "" || $email == "" || $phone == "") {
$hasError = true;
$response = $responseFillData . $response;
}
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9+-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i", $email)) {
$hasError = true;
$response = $responseValidEmail . $response;
}
if($hasError) {
header("refresh:5;url=index.html#contact");
echo $response;
return false;
}
$text = "Voornaam: $fname \n
Familienaam: $lname \n
E-mailadres: $email \n
Telefoonnummer: $phone \n
Bericht: \n
$message";
if(mail($yourEmail, $fname . " " . $lname . " sent a message via de DDW website", $text, $headers)) {
$response = $responseCompleted . $response;
}
else {
$response = $responseFailed . $response;
}
header("refresh:5;url=index.html#contact");
echo $response;
}
?>
Next to that, I also want to format the response message. I tried adding some html elements to the message, but that does not seem to work. How can I add my image in the 'success' response?
Example:
$responseCompleted = "Form successfully submitted. <img src="http://www.ddw-sanitair.be/img/vink.png"><br>";
I just started learning php not long ago, and I am currently building a "contact us" form which will send the user input to my email. I have been on this for days thinking I will figure it out, but I am not getting it. I wanna also be able to receive the user's input in my email and also be able to detect the user's IP address. When I submitted the form, I received every other input but the IP address though I used "localhost".
I tried with the <input type="hidden" name="message" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"> but I read online that it's better to do it without the <input type="hidden"> and just process everything in the form processor script. Please kindly help me with this.
<?php
$emailError = "";
$messageError = "";
function getUserIp(){
$client = $_SERVER['HTTP_CLIENT_IP'];
$forward = $_SERVER['HTTP_X_FORWARD_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $client;
}else{
$ip = $remote;
}
return $ip;
}
if(isset($_POST['submit'])){
//declares variable
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
if(empty($_POST['email'])){
$emailError = "Please enter your email";
}
if(empty($_POST['subject'])){
$subjectError = "Please enter a subject?";
}
}
if(!empty($_POST['email']) && !empty($_POST['subject'])){
// Send the email
$to = "you#yourdomain.com";
$email = "From: $email";
$subject = "Subject: $subject";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";;
$headers = "From: $email,";
$send_contact=mail($to,$email,$subject,$message,$headers);
header("Location: domain");
}
?>
change below section --
if(!empty($_POST['email']) && !empty($_POST['subject'])){
// Send the email
$to = "you#yourdomain.com";
$ip =getUserIp();
$email = "From: $email";
$subject = "Subject: $subject";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";;
$headers = "From: $email,";
$send_contact=mail($to,$email,$subject,$message,$headers);
header("Location: domain");
}
I have VERY little experience with PHP.
I am testing out a contact form for this website. I am only getting the "subject, message, and header" fields in the email.
I need the telephone and name fields to show in my email message as well.
What am I doing wrong?
Any help is appreciated -- thanks!
<?php
$name;$email;$message;$captcha;$telephone;
if(isset($_POST['name'])){
$name=$_POST['name'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['telephone'])){
$telephone=$_POST['telephone'];
}if(isset($_POST['message'])){
$message=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form. <a href=http://www.website.com#contact/>Back to Form</a></h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6LcYjgcT****q9vhur7iH_O4dZPl4xUAVwW8=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}else
{
// Checking For Blank Fields..
if ($_POST["name"] == "" || $_POST["email"] == "" || $_POST["telephone"] == "" || $_POST["message"] == "") {
echo "Fill All Fields..";
} else {
// Check if the "Sender's Email" input field is filled out
$email = $_POST['email'];
// Sanitize E-mail Address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email) {
echo "Invalid Sender's Email";
} else {
$to = 'email#gmail.com';
$subject = 'Contact Form';
$message = $_POST['message'];
$name = $_POST['name'];
$headers = 'From:' . $email . "\r\n";
// Sender's Email
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message .= "\r\n Name: " . $name;
$message .= "\r\n Email: " . $email;
$message .= "\r\n Telephone: " . $telephone;
$message .= "\r\n Message: " . $message;
// Send Mail By PHP Mail Function
if (mail($to, $subject, $message, $headers)) {
echo "Your mail has been sent successfully!<a href=http://wwwwebsite.com>Back</a>";
} else {
echo "Failed to send email, try again.";
exit ;
}
}
}
}
?>
The $message variable is the content of the mail that will be sent to your adress, add the values you want to get in your mail to that variable.
like so : (since you're not using HTML mail I think)
$message .= "\r\n Name : " . $name;
$message .= "\r\n Email : " . $email;
etc..
Before you call the mail function.
ALSO :
You're adding Phone number, Email, and message in your $email variable. you should give these their own variable.
I have just now completed building this form and it is running quite well, thanks to the user community of stackoverflow; but I have now noticed an issue with my form while testing it in different scenarios.
The major problem is that it is sending information regardless of any information being present and regardless of whether the user has entered correct data. The error is showing up on the page, but the data is being sent, even when error is being displayed on the page.
I want to find out how to stop the form from sending an E-mail, when the fields are empty or have wrong content entered into them?
Here is the PHP code, I am using:
<?php
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";
if(isset($_POST['email'])) {
$email_to = "myself#mydomain.com";
$email_subject = "Contact us - My company's name";
{
if (empty($_POST["first_name"]))
{$first_nameErr = "(First Name is required)";}
else
{$first_name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name))
{
$first_name = "(Only letters and white space allowed)";
}
}
if (empty($_POST["last_name"]))
{$last_nameErr = "(Last Name is required)";}
else
{$last_name = test_input($_POST["last_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name))
{
$last_name = "(Only letters and white space allowed)";
}
}
if (empty($_POST["email"]))
{$emailErr = "(Email ID is required)";}
else
{$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "(Invalid email format)";
}
}
if (empty($_POST["overview"]))
{$overviewErr = "(Overview is required)";}
else
{$overview = test_input($_POST["overview"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$overview))
{
$overview = "(Only letters and white space allowed)";
}
}
}
//Email & SEND INFO
$email_message = "Form details below.\n\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Services: ".clean_string(implode(', ', $service))."\n";
$email_message .= "Overview: ".clean_string($overview)."\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);
?>
<!-- Success HTML -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
I believe you are wanting to stop the form from sending any data if there is nothing to send in the forms, right? If so then you need to do that via javascript. The easiest way would be with jquery. I suggest This tutorial. That will help you get off the ground
I think your code blocks might be incorrect try this:
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// define variables and set to empty values
$first_nameErr = $last_nameErr = $emailErr = $overviewErr = "";
$first_name = $last_name = $email = $overview = "";
if(isset($_POST['email'])) {
$email_to = "myself#mydomain.com";
$email_subject = "Contact us - My company's name";
}
if (empty($_POST["first_name"])) {
$first_nameErr = "(First Name is required)";
} else {
$first_name = test_input($_POST["first_name"]);
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$first_name = "(Only letters and white space allowed)";
}
if (empty($_POST["last_name"])) {
$last_nameErr = "(Last Name is required)";
} else {
$last_name = test_input($_POST["last_name"]);
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$last_name = "(Only letters and white space allowed)";
}
if (empty($_POST["email"])) {
$emailErr = "(Email ID is required)";
} else {
$email = test_input($_POST["email"]);
}
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "(Invalid email format)";
}
if (empty($_POST["overview"])) {
$overviewErr = "(Overview is required)";
} else {
$overview = test_input($_POST["overview"]);
}
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$overview)) {
$overview = "(Only letters and white space allowed)";
}
//Email & SEND INFO
$email_message = "Form details below.\n\n";
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Services: ".clean_string(implode(', ', $service))."\n";
$email_message .= "Overview: ".clean_string($overview)."\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);
if ($first_nameErr || $last_nameErr || $emailErr || $overviewErr) {
echo "Thank you for contacting us. We will be in touch with you very soon.";
} else {
echo "There are some errors in your form: " . $first_nameErr . ', ' . $last_nameErr . ', ' . $emailErr . ', ' . $overviewErr;
}
Not sure if that'll fix everything but it should be easier to see what's going on.
Okay my contact form (http://jshjohnson.com/new/contact.php) works as it should however the ReCaptcha error (that usually comes up when you enter the words incorrectly) shows up on page load.
I have set an empty variable called message which should stop this but it unfortunately doesn't.
<?php
include("functions.php");
require_once('recaptchalib.php');
$publickey = "6LdLX9oSAAAAALZawj-uldWrjsI0zYcR-w_r_sNh";
$privatekey = "<removed>";
$resp = recaptcha_check_answer($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
$recaptcha_form = recaptcha_get_html($publickey);
$message = "";
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
$message = "The reCAPTCHA wasn't entered correctly. Go back and try it again.
(reCAPTCHA said: " . $resp->error . ")";
} else {
// ADD YOUR CODE HERE to handle a successful ReCAPTCHA submission
// e.g. Validate the data
$myemail = "contact#jshjohnson.com";
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$fullname = $firstname . " " . $surname;
$email = $_POST['email'];
$contact = $_POST['message'];
$website = $_POST['website'];
$subject = "Website Contact";
if($firstname == '') {
$message = 'Please type your first name' ;
} else if ($surname == '') {
$message = 'Please type your surname';
} else if ($email == '') {
$message = 'Please type your email';
if (preg_match("/^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/", $email)) {
$message = "Email address is valid";
} else {
$message = "Email address is invalid";
}
} else if ($contact == '') {
$message = 'Please type your message';
} else {
/* Let's prepare the message for the e-mail */
$contact = "Hello!
Josh Johnson Web Design contact form has been submitted by:
Name:* $fullname
E-mail:* $email
Website: $website
Budget: £
Message:*
$contact
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $contact);
/* Redirect visitor to the thank you page */
header('Location: index.php');
exit();
}
}
?>
Any ideas??
You have to delete or ignore that errors. In Rails, it is
flash[:recaptcha_error] = nil
For your PHP, you always check if it's valid with
if (!$resp->is_valid) {
and then print the error with $resp->error. So even if nothing was typed in yet on first page load, it might think it is invalid and print the error. Instead, check to see if the recaptcha_response_field is empty or not before verifying the CAPTCHA.