I have a simple PHP mailer script and it is sending me an email everyday as if someone has clicked the submit button without any inputs (blank email). Is there a way to suppress this behavior? What should I change in my script? Thanks
$message = $_POST[message];
$name = $_POST[name];
$email = $_POST[email];
$email_message = $name ." has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;
echo "Hi " .$name ."<br> Your Email is: " .$email ."<br><br>And we received the following message: <br>" . $message."<br><a href='../index.html'>Back Home</a>";
mail($to, $subject, $email_message);
?>
Try this code:
$message = $_POST[message];
$name = $_POST[name];
$email = $_POST[email];
$email_message = $name ." has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;
echo "Hi " .$name ." < break tag > Your Email is: " .$email ." < break tag > And we received the following message: < break tag > " . $message." < break tag > <a href='../index.html'>Back Home</a>";
if(isset($_POST[message]) && isset($_POST[name]) && isset($_POST[email]))
{
mail($to, $subject, $email_message);
} else{
echo "Error";
}
Check if the values are empty or not before sending the mail:
// Check if values exist before continuing
if (
isset($_POST['message']) && strlen($_POST['message']) > 0
&&
isset($_POST['name']) && strlen($_POST['name']) > 0
&&
isset($_POST['email']) && strlen($_POST['email']) > 0
)
{
$message = $_POST['message']; // Remember to use single or double quotes!!!!
$name = $_POST['name'];
$email = $_POST['email'];
$email_message = $name ." has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;
echo "Hi " .$name ."<br> Your Email is: " .$email ."<br><br>And we received the following message: <br>" . $message."<br><a href='../index.html'>Back Home</a>";
mail($to, $subject, $email_message);
}
You have to check whether the values that you are about to send are set or not and submit button is clicked.
Sample Form (form.php)
<form method="POST">
<input type="text" name="name" />
<input type="text" name="email" />
<textarea name="message"></textarea>
<input type="submit" name="submit" />
</form>
Callback Page (submit.php)
<?php
if(isset($_POST['submit'])) {
// Your code comes here
// you can use conditions to validate the form
// ex: if(!empty($_POST['message'])) or if(trim($_POST['message']) != "") to avoid empty message submissions
// Regular expressions to validate email addresses / name
}
?>
Alright, so - the problem is that you're not checking wether or not the fields has been filled, so we're doing that and if they haven't filled out all fields, redirect them back to the contact page with something telling why it made an error. Additionally, we're storing the fields temporarily to avoid issues with users have to fill out all fields again, in case they did fill some out.
<?php
session_start(); // For remembering input (please remember to put this before any output)
$message = $_POST[message];
$name = $_POST[name];
$email = $_POST[email];
if( empty($message) || empty($name) || empty($email) )
{
// Redirect back to contact page, but use sessions to remember already filled out fields
$_SESSION['contact_form']['message'] = $message;
$_SESSION['contact_form']['name'] = $name;
$_SESSION['contact_form']['email'] = $email;
header("Location: http://example.com/contact_page");
exit; // Make sure the rest of the script isn't completed
}
if(isset($_SESSION['contact_form'])) // Check if cleaning up is needed
{
unset($_SESSION['contact_form']); //No need to store this after we send the mail
}
$email_message = $name ." has sent you a message: \n\n";
$email_message .= "Please contact " . $name . " with \n Email: " . $email . "\n";
$email_message .= "Phone: " . $phone;
mail($to, $subject, $email_message, "From: youremail#example.com\r\n"); // added a sender header, just for best practices
?>
Hi <?php echo $name; ?><br>
Your Email is: <?php echo $email; ?><br><br>
And we received the following message: <br>
<?php echo $message; ?><br>
<a href='../index.html'>Back Home</a>
Now, that being done, we need to consider the contact page again, because now we need to tell them that an error occurred and fill out the contact fields again, for those fields thet did fill.
<?php
session_start(); // For fetching remembered output input (please remember to put this before any output)
$error = ;
$message = $name = $email = "";
if(isset($_SESSION['contact_form']))
{
$error = true;
$message = $_SESSION['contact_form']['message'];
$name = $_SESSION['contact_form']['name'];
$email = $_SESSION['contact_form']['email'];
unset($_SESSION['contact_form']); // In case they just leave
}
?>
<?php if($error): ?>
<p class="error">You missed some fields, could you please fill them out?</p>
<?php endif; ?>
<form method="post" action="">
<input type="text" name="name" value="<?php echo $name; ?>">
<input type="text" name="email" value="<?php echo $email; ?>">
<textarea name="message"><?php echo $message; ?></textarea>
<input type="submit" name="submit">
</form>
I hope this can give you an idea of some workflow and combining with a little user experience.
$message = $_POST['message'];
$name = $_POST['name'];
$email = $_POST['email'];
$email_message = $name ." has sent you a message: \n" . $message . "\n Please contact " . $name . " with \n Email: " . $email . "\n Phone: " . $phone;
echo "Hi " .$name ."<br> Your Email is: " .$email ."<br><br>And we received the following message: <br>" . $message."<br><a href='../index.html'>Back Home</a>";
if(!empty($message) && !empty($name) && !empty($email)) {
mail($to, $subject, $email_message);
}
I did not include $email_message because it won't be empty from the start since you're assembling it above.
$subject = 'Subject line for emails';
$headers = 'From: Email Tital ' . "\r\n".'X-Mailer: PHP/' . phpversion();
$message = "Your Message"\n\r";
$sentMail = #mail($to_Email, $subject, $message, $headers);
if(!$sentMail) {
echo "Server error, could not send email. Sorry for the inconvenience." ;
} else {
echo "Thank you for enquiry. We will contact you shortly !";
}
You need to check first message field is empty or not,its client site check if you press submit without message it never submit your form,
function empty() {
var x;
x = document.getElementById("message").value;
if (x == "") {
alert("Enter a message first");
return false;
};
}
and
<input type="submit" value="submit" onClick="return empty()" />
Related
I have a form that takes in data i am using php to send it to my email once a user has filled in all the required fields. If a field is empty I get a message eg. "Email is required" but the email still sends. I dont know what the problem is any ideas? Idont want to send a email if any field is empty i also dont want refresh the page everytime submit is clicked, I would like to instead just show the "Required message".
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = "";
$first_name = $last_name = $email = $ironing = $description = $Rooms ="";
if(isset($_POST['submit'])){
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$ironing = $_POST['ironing'];
$Rooms = $_POST['Rooms'];
$Description = $_POST['description'];
if (empty($_POST["first_name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["description"])) {
$descriptionErr = "Description is required";
} else {
$description = test_input($_POST["description"]);
}
if (empty($_POST["Rooms"])) {
$RoomErr = "Room number is Required";
} else {
$Rooms = test_input($_POST["Rooms"]);
}
if (empty($_POST["ironing"])) {
$ironingErr = "Ironing is Required";
} else {
$ironing = test_input($_POST["ironing"]);
}
$to = "someemail#gmail.com"; // this is your Email address
$subject = "Order Sumbittion";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n" . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
// sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header("Location: index.php");
}
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<p><span class="error">* required field.</span></p>
<div class="col-md-9">
<form action="" method="post">
First Name: <input type="text" name="first_name">
<span class="error">* <?php echo $nameErr;?></span><br>
<br>
Last Name: <input type="text" name="last_name">
<span class="error">* <?php echo $lastNameErr;?></span><br>
Email:
<br>
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br>
Ironing?<br>
<input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="Yes") echo "checked";?> value="Yes">Yes
<input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="No") echo "checked";?> value="No">No
<span class="error">* <?php echo $ironingErr;?></span>
<br>
Number Of Rooms:
<br>
<input type="text" name="Rooms">
<span class="error">* <?php echo $RoomErr;?></span>
<br>
Description of the House:
<br>
<textarea name="description" rows="10" cols="70"></textarea>
<span class="error">* <?php echo $descriptionErr;?></span>
<br>
<input type="submit" name="submit" value="Submit">
</form>
Quite simply after checking for errors and loading error message variables, you send the email without checking if any errors have been spotted.
So try adding some code before the email is sent to check for any found errors like this for example
First change this line to set the error variables to NULL
$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = NULL;
And then wrap the email sending in a test like this
if (isset( $nameErr) || isset($lastNameErr) || isset($emailErr) ||
isset($ironingErr) || isset($descriptionErr) || isset($RoomErr) ) {
// You have an error
} else {
$to = "someemail#gmail.com"; // this is your Email address
$subject = "Order Sumbittion";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n" . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
// sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header("Location: index.php");
}
This code works on my own website, the block of code used to email yourself and the user did not actually have an validation to check if any errors came up in your checks.
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = "";
$first_name = $last_name = $email = $ironing = $description = $Rooms ="";
$error = false;
if(isset($_POST['submit']))
{
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$ironing = $_POST['ironing'];
$Rooms = $_POST['Rooms'];
$Description = $_POST['description'];
if (empty($_POST["first_name"])) {
$nameErr = "Name is required";
$error = true;
} else {
$name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
$error = true;
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$error = true;
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$error = true;
}
}
if (empty($_POST["description"])) {
$descriptionErr = "Description is required";
$error = true;
} else {
$description = test_input($_POST["description"]);
}
if (empty($_POST["Rooms"])) {
$RoomErr = "Room number is Required";
$error = true;
} else {
$Rooms = test_input($_POST["Rooms"]);
}
if (empty($_POST["ironing"])) {
$ironingErr = "Ironing is Required";
$error = true;
} else {
$ironing = test_input($_POST["ironing"]);
}
if ($error === false)
{
$to = "youremail#gmail.com"; // this is your Email address
$subject = "Order Sumbittion";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n" . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
// sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header("Location: index.php");
}
}
// You can also use header('Location: thank_you.php'); to redirect to another page.
?>
<p><span class="error">* required field.</span></p>
<div class="col-md-9">
<form action="" method="post">
First Name: <input type="text" name="first_name">
<span class="error">* <?php echo $nameErr;?></span><br>
<br>
Last Name: <input type="text" name="last_name">
<span class="error">* <?php echo $lastNameErr;?></span><br>
Email:
<br>
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br>
Ironing?<br>
<input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="Yes") echo "checked";?> value="Yes">Yes
<input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="No") echo "checked";?> value="No">No
<span class="error">* <?php echo $ironingErr;?></span>
<br>
Number Of Rooms:
<br>
<input type="text" name="Rooms">
<span class="error">* <?php echo $RoomErr;?></span>
<br>
Description of the House:
<br>
<textarea name="description" rows="10" cols="70"></textarea>
<span class="error">* <?php echo $descriptionErr;?></span>
<br>
<input type="submit" name="submit" value="Submit">
</form>
If you don't want to refresh page, then you can use ajax call to send data on server to validate. Otherwise form will submit and page will refresh every time you slick submit.
And email is being sent every time weather data is valid or not, because there is no condition to check if data is valid. So use a variable and assign it 'false' and before sending check if its still true then send email.
}
First things first , the solution to your issue is that even you caught the error
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
you did not applied any check to make sure that script execution does not continue , for this you can add die(); also you can take a status variable as $status = 0; if you find any error just assign $status = 1 and before sending email check if($status == 0).
Now if you want to show error message without refreshing the page I would suggest to use jquery or any plugin such as https://validatejs.org/
So I've made myself a little contact form with php, css, and html. But when I try to add a email validation it still sends the email and doesn't change the style of the input to red (Like I would like it to). Another issue I'm having is the button redirecting to the top of the page (which I do not want it to do). Last I can I make the input keep the text rather than remove it once submitted
HTML:
<div id="contact">
<div class="container">
<form id="contact-form" method="post">
<h1>Contact Form</h1>
<fieldset>
<input placeholder="Your Name" type="text" name="name" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" type="email" name="email" id="email-input" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number (optional)" type="tel" name="phone" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site (optional)" type="url" name="site" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your message here...." name="message" required></textarea>
</fieldset>
<fieldset>
<button type="submit" id="contact-submit" name="submit">Submit</button>
</fieldset>
</form>
</div>
</div>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style";
}
?>
Try this for email validation in php
<?php
if (isset($_POST) && !empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$visitors_site = $_POST['site'];
$message = $_POST['message'];
$email_from = 'mattmowen1#gmail.com';
$email_subject = 'New Contact Submission';
$to = 'mattmowen1#gmail.com';
$headers = "From:" . $email;
$headers = "Contact Submission From: " . $email;
$message1 = "Name: " . $name;
$message2 = "\n\nEmail: " . $email;
$message3 = "\n\nPhone: " . $phone;
$message4 = "\n\nTheir Site: " . $visitors_site;
$message5 = "\n\nMessage: " . $message;
$email_body = $message1 . $message2 . $message3 . $message4 . $message5;
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
} else {
echo "<style>#email-input {color:red}</style>";
}
}
?>
As per our chat conversation. I am adding jquery ajax function according to your form requirement.
You need to create new file email.php and put your php code into this separate php file
<script>
var url = 'email.php';
$.ajax({
url : url,
type : "POST",
dataType : "JSON",
data : $('#contact-form').serialize(),
success : function(response) {
if (response.error == 0) { // success
$('#contact-form')[0].reset();
alert('Form submitted successfully. We will contact you asap.');
} else { // error
$('#email-input').css('color', 'red');//in case of email error
alert('ERROR MESSAGE');//form is invalid
}
}
})
</script>
To handle JSON request you need to send JSON object in response. So change you php code snippet like this:
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
mail($to, $email_subject, $email_body,$headers);
exit(json_encode(array('error' => 0)));
} else {
exit(json_encode(array('error' => 1)));
}
Using a small contact form, however when the form is submitted/sent, I'm getting a "No data" message.
<form method="post" action="mail.php">
<input type="text" placeholder="Name*" name="name" required>
<input type="email" placeholder="Email*" name="email" required>
<input type="text" placeholder="Subject" name="subject">
<textarea placeholder="Message" name="message" required></textarea>
<input type="submit" value="Send" name="submit">
</form>
The PHP:
<?php
// variable
$fromemail = 'any_site#my_site_com'; // from mail
$to = "marygsheehan#yahoo.ie"; // to mail
//
// check data
if (!isset($_POST["fields"])) {
die("No data");
}
$fields = $_POST["fields"];
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
$subject = "Site mail: " . $fields['subject'];
// subject massege
$subject = '=?utf-8?Q?'."\"".urlencode($subject)."\"".'?=';
$subject= str_replace("%","=",$subject);
$subject = str_replace("+","_",$subject);
// content massage
$name = $name ? $name : 'unknown';
$from = 'Mail from'."<".$fromemail.">";
$mess = $mess ? $mess : 'unknown';
$message = "<b>Client name: </b> " . $fields['name'] . "<br>";
$message .= "<b>Client email: </b> " . $fields['email'] . "<br>";
/*$message .= "<b>Client phone: </b> ".$site."<br>";*/
$message .= "<b>Subject: </b> " . $fields['subject'] . "<br>";
$message .= "<b>Text:</b>\n" . $fields['text'] . "<br>";
$message .= "Sent: ".strftime("%a, %d %b %Y %H:%M:%S");
// end content massage
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Site Mail <" . $fromemail . ">\r\n";
if(mail($to, $subject, $message, $headers)){
print 'ok';
} else {
print 'email not senta';
}
?>
I've been staring at it so long it's a blur, so it's probably a silly mistake. Any help appreciated?
There is no input field in the form with name fields
You need to access them directly like this
$_POST['your_input_name_declared_in_form']
Do it as follow
if(isset($_POST['submit']))
{
$fields = $_POST;
if( empty($fields['name']) ) {
die("No name");
}
if( empty($fields['email']) ) {
die("No email");
}
if (!empty( $fields['code'] ) ) {
die("ok");
}
}
I've set up my form and I'm unsure why the email is being sent and received, but does not include the 'message' field.
I have tried changing the ID's and testing different options but it doesn't seem to send the message
I had the issue a while back with another website but I was able to fix it and I don't remember how.
The reference used is $comment and I've used id=comment so I'm unsure why it's not sending it! Any help much appreciated. I've read other posts on here and no one has a similar issue from what I know.
Here is my website code:
<!-- Form -->` `
<form method="post" action="/contact.php" name="contactform" id="contactform" class="form form-stacked c-form">
<fieldset>
<input name="name" type="text" id="name" placeholder="Your Name" />
<input name="email" type="text" id="email" placeholder="Your E-mail" />
<textarea name="comment" id="comment" placeholder="Message"></textarea>
<input name="verify" type="text" id="verify" size="4" value="" placeholder="3 + 1 =" />
<input type="submit" class="submit btn outline" id="submit" value="Send message" />
</fieldset>
</form>
</div>
Here is my contact form .php
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
//$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if(trim($comment) == '') {
echo '<div class="error_message">Attention! Please enter a message.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}
/*
if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
exit();
} else if(trim($comment) == '') {
echo '<div class="error_message">Attention! Please enter your message.</div>';
exit();
} else if(!isset($verify) || trim($verify) == '') {
echo '<div class="error_message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($verify) != '4') {
echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
*/
if(get_magic_quotes_gpc()) {
$comment = stripslashes($comment);
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
$address = "support#idomain.sx";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
$e_subject = 'Email from: ' . $name . '.';
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
$e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comment\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email or via phone $phone";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<strong class=\"success\">Email Sent Successfully.</strong>";
echo "<p>Thank you <strong>$name</strong>, please allow up to 5 business days for a response.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
Your textarea is named comments
<textarea name="comments" id="comments" placeholder="Message"></textarea>
But here it's looking for comment
$comment = $_POST['comment'];
Make sure the name is the same as the $_POST variable
Remember form data is appended to the name of the input, not the id
EDIT
$e_content = "\"$comment\"" . PHP_EOL . PHP_EOL;
You aren't concatenating your string and variable. Change it to:
$e_content = $comment . PHP_EOL . PHP_EOL;
I don't know what the backslashes are for. If they are required, change it to:
$e_content = "\" . $comment . "\" . PHP_EOL . PHP_EOL;
Change the $comment = $_POST['comment'];
into
$comment = $_POST['comments'];
Your textarea is named comments
<textarea name="comments" id="comments" placeholder="Message"></textarea>
But here it is looking for comment
$comment = $_POST['comment'];
Make sure the name is the same as the $_POST variable. Remember that form data is appended to the name of the input, not the id.
Edit:
$e_content = "\"$comment\"" . PHP_EOL . PHP_EOL;
You aren't concatenating your string and variable. Change it to:
$e_content = $comment . PHP_EOL . PHP_EOL;
I don't know what the backslashes are for. If they are required, change it to:
$e_content = "\" . $comment . "\" . PHP_EOL . PHP_EOL;
I m having a problem with my email php form, when I click in the submit button an other page shows up saying There was a problem with your e-mail ()
I don't no what I am doing wrong?
here is my code:
html code
<!-- Subscription Form -->
<form class="email" action="form.php" method="post">
<input class="get_notified" type="text" placeholder="Enter your email address ..."/>
<button type="submit" class="go" /></form>
<!-- End Subscription Form -->
</div>
</div>
</body>
php code
<?php
$to = "email#mydomain.com";
$from = "email#mydomain.com";
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
You'll need to add a name="email" field to your HTML form in order for PHP to be able to fetch it using $_POST['email']
<input class="get_notified" name="email" type="text" placeholder="Enter your email address ..."/>