Email address not valid when posting with php contact form - php

im having issues with my script not sending an email, the return error message is: Email address not valid! This happens when ever I enter my email address into the text field. I have a feeling that it is the (preg_match) method that is creating the issue, but after looking online I dont really understand the content of the method. Hope you guys can help, thanks.
SOURCE CODE:
<?php
/*Select email recipient*/
$myemail = "info#shadowempires.url.ph";
/*Check all form inputs using check input function*/
$name = check_input($_POST['name'], "Please enter your name");
$email = check_input($_POST['email'], "Please enter your email address.");
$comment = check_input($_POST['comment'], "Please write a message.");
/*If email is not valid show error message*/
if (!preg_match("/(\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)){
show_error("Email address not valid!");
}
/*Lets prepare the message for the email*/
$message = "Customer Question!
Contact form has been submitted by:
Name: $name
Email: $email
Comments: $comment
End of message";
/*Send the message using mail() function*/
mail($myemail, $message);
/*Redirect visitor to the thank you page*/
header('Location: thankyou.htm');
exit();
/*Functions we used*/
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){
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php exit();
}
?>

What about filter_var:
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// valid email address
}
This is an easy way to validate an email address.
UPDATE 1
Take a look to this answer. Here there is more information about using regex for validate email address: How to validate an email address in PHP
UPDATE 2
There is a tool for test regex email patterns, look here

You need to fix your regexp, here is example of working one:
/((\w|\-))+\#((\w|\-))+\.((\w|\-))+/

Related

Post contact form to html document

I currently have a contact page that emails me when it gets submitted. I want to send the fields to another htm page on the server instead so staff can access the information by viewing the page in their browser. So even if they don't have access to email, they can view the page from their browser.
The information sent must accumulate on the form, ie new submissions must submit to the ame form without deleting previous submissions.
Do I amend the code? Do I make a few changes like changing the 'mymail' to 'myform = "form.htm" ' I made a few changes and tried submitting but have no idea how to make the form work.
Below is the code that sends the email.
<?php
/* Set e-mail recipient */
$myemail = "mail#abc.co.za";
$subject = "Contact Form";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name']);
$contactnr = check_input($_POST['contactnr']);
$email = check_input($_POST['email']);
$phoneoremail = check_input($_POST['phoneoremail']);
$message = check_input($_POST['message']);
$ipaddress = $_SERVER['REMOTE_ADDR'];
$iphost = $_SERVER['HTTP_HOST'];
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Prepare the message for the e-mail */
$message = "Contact form has been submitted
Details:
Name: $name
Phone Number: $contactnr
E-mail: $email
Message: $message
Sender IP Address: $ipaddress
Form submitted via server: $iphost
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thankyou.htm');
exit();
/* Functions we used */
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)
{
?>
<html>
<body>
<b>Please provide missing information so we can assist you:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
If you want to post this to an HTML page aswell as sending it per E-Mail, why don't you just write to an HTML file?
$file = 'formdata.html';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "Details:<br>
Name: $name<br>
Phone Number: $contactnr<br>
E-mail: $email<br>
Message: $message<br>
Sender IP Address: $ipaddress<br>
Form submitted via server: $iphost<br><hr>";
// Write the contents to the file
file_put_contents($file, $current);
Maybe include formdata.html then as an iFrame on a page only the staff can access?

Blank form keeps submitting (HTML, PHP)

So I have a contact form where customers can fill in their details. When submitted, the form posts the information across to a PHP script that then formats it into html and emails it to my business email.
The only problem I have is that I keep receiving blank emails at random. Obviously the PHP script is being triggered and an email is being sent but I'm not sure why.
The form has required fields so even if someone tried to submit it blank, it wouldnt let them. When I recieve these emails, there should at least be something in them.
I've thought about adding some extra validation to the PHP script to check if any of the required values are empty/missing, but the form deals with this anyway.
Does anyone know what is happening? It's probably something simple i've overlooked.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$budget = $_POST['budget'];
$timeframe = $_POST['timeframe'];
$desc = $_POST['desc'];
//Send HTML formatted email
$send = mail("nathan#nathanthompson.co.uk",
"You have received an enquiry",
"<html>
<body>
<h3>Here is the information for the enquiry: </h3>
<br>
<p>Name: $name</p>
<p>Email: $email</p>
<p>Budget: $budget</p>
<p>Timeframe: $timeframe</p>
<br>
<p>Description of enquiry:</p>
<p>$desc</p>
</body>
</html>
", "Content-type: text/html; charset=iso-8859-1");
if ($send)
{
header("Location:index.html");
}
else
{
echo "An error occurred. Please return to the contact form and try again.";
}
?>
Here you go, try this! :)
<?php
//Check the request method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//Start by importing and sanitizing the fields
$name = sanitize($_POST["name"]);
$email = sanitize($_POST["email"]);
$budget = sanitize($_POST["budget"]);
$timeframe = sanitize($_POST["timeframe"]);
$desc = sanitize($_POST["desc"]);
//Make sure the fields are populated
if (empty($name)) {die("The 'Name' field was not populated. Please return to the contact form and try again.");}
if (empty($email)) {die("The 'Email' field was not populated. Please return to the contact form and try again.");}
if (empty($budget)) {die("The 'Budget' field was not populated. Please return to the contact form and try again.");}
if (empty($timeframe)) {die("The 'Time Frame' field was not populated. Please return to the contact form and try again.");}
if (empty($desc)) {die("The 'Description' field was not populated. Please return to the contact form and try again.");}
//Everthing is fine, so send the email!
$send = mail("nathan#nathanthompson.co.uk", "You have received an enquiry",
"<html>
<body>
<h3>Here is the information for the enquiry: </h3>
<br>
<p>Name: $name</p>
<p>Email: $email</p>
<p>Budget: $budget</p>
<p>Timeframe: $timeframe</p>
<br>
<p>Description of enquiry:</p>
<p>$desc</p>
</body>
</html>
", "Content-type: text/html; charset=iso-8859-1");
if ($send) {
header("Location: index.html");
} else {
die("An error occurred. Please return to the contact form and try again.");
}
} else {
die("An error occurred. Please return to the contact form and try again.");
}
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES);
return $data;
}
?>

php form validation required fields

I have a php form that i'm trying to make required fields for but I cannot seem to get it to work right and I know its something i'm missing. I just need to make the form require $name $bank $email and $phone.
<?php
/* Set e-mail recipient */
$myemail = "email#email.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$bank = check_input($_POST['bank'], "Enter your name");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$headphones = check_input($_POST['headphones']);
$subject = "Allied Affiliated Funding - Bose Landing Page Form Submission";
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $name
Bank: $bank
E-mail: $email
Phone: $phone
Head Phones: $headphones
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
if($_POST['headphones'] == Yes){
header("Location: landing1/thank-you-bose.html");
}
else {
header("Location: landing1/thank-you.html");
}
exit();
/* Functions we used */
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)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
I have looked at several different tutorials on how to complete this but none of the solutions seem to be working.
I think your problem is at this line
if($_POST['headphones'] == Yes){
It needs to be
if($_POST['headphones'] == "Yes"){
You dont have a variable named Yes so i think you want to check if the content of headphones is "yes".

php mail not sending "invalid email address"

im getting the "invalid email address"
all is hardcoded for testing, what is missing? thanks!
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['example#example.com'];
$subject = $HTTP_POST_VARS['subjectaaa'];
$message = $HTTP_POST_VARS['messageeeee'];
/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html>
Change
$email = $HTTP_POST_VARS['jaaanman2324#gmail.com'];
$subject = $HTTP_POST_VARS['subjectaaa'];
$message = $HTTP_POST_VARS['messageeeee'];
to
$email ='jaaanman2324#gmail.com';
$subject ='subjectaaa';
$message = 'messageeeee';
I think you want it to be hardcoded like this:
$email = 'jaaanman2324#gmail.com';
Otherwise you are trying to get the value out of HTTP_POST_VARS with the key of jaaanman2324#gmail.com
First, don't use $HTTP_POST_VARS, it's $_POST now.
Second, by writing $HTTP_POST_VARS['jaaanman2324#gmail.com'] you're looking for table element with juanman234#gmail.com key.
That's not what you wanted to do.
If you want to hardcode it, write
$email = 'jaaanman2324#gmail.com';`
if not, write
$email = $_POST['email'];
to get email field from form.

.php only sending part of a form to a email address

I'm trying to setup a simple website with a form that sends to a specific email address. The idea is to have a few different things to be sent in an email, like: first and last name, age, email, city, subject (which is a dropdown menu with a few different options) and then finally a comment box.
I've actually spent the last 14 hours reading and looking up a way of how to get the PHP script to work properly so that the form will be send. But for some reason I just can't figure it out.
I just cant seem to get the form to send with everything included that I require to be included. Could somebody point me out why the below code doesn't work?
<?php
/* EMAIL RECIPIENT */
$myemail = "name#example.com";
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
/* Check all form inputs using check_input function */
$formName = check_input ($_POST['formName'], "Please enter your First Name");
$formSurname = check_input ($_POST['formSurname'], "Please enter your First Name");
$formAge = check_input ($_POST['formAge'],);
$formEmail = check_input ($_POST['formEmail'], "Please enter your email so we can get back to you");
$formIntent = check_input ($_POST['formIntent'], "Pleas choose a reason for why contact us");
$formIntent = " (MYFORM) " . $formIntent;
$formComment = check_input ($_POST['formComment']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $formEmail))
{
show_error("E-mail address not valid");
}
/* The layout for the email and how it will be set up for reading the email. */
$message = "Hello!
Your contact form has been submitted by:
Name: $formName
Age: $formAge
City: $formCity
Email: $formEmail
Comment: $formName
The reason for contact with Mistral is: $formIntent
Comment: $formComment
End of message.
";
/* Send the message using the mail() function */
mail($myemail, $subject, $message);
/* Redirect visitors to the thank you page */
header('Location: ../thanks.html');
function show_error($myError)
{?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
Well, you validate $_POST['formSurname'] but then you never use it. Similarly, you output $formCity in your $message but you never defined it.
This is most likely the cause of your problems.

Categories