anyone help me with my contact form? it will not work, it sends a message but no confirmation of message succes? everything works fine exceptg the Message succesfull send function :(
here are the codes hope someone can help me
Contact.php
<?php
#error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
#ini_set('display_errors', true);
#ini_set('html_errors', true);
#ini_set('error_reporting', E_ALL ^ E_NOTICE ^ E_WARNING);
ini_set("log_errors", 1);
ini_set("error_log", "php-error.log");
error_log( "Hello, errors!" );
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "info#arjen079.com";
$email_subject = "Contact Application Website!";
$email_noreply = "noreply#arjen079.com";
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required
$email_message = "Form Details Below:\n\n";
$email_messagesender = "Here is a copy of the form you filled in:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Subject: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_messagesender .= "First Name: ".clean_string($name)."\n";
$email_messagesender .= "Email: ".clean_string($email)."\n";
$email_messagesender .= "Subject: ".clean_string($subject)."\n";
$email_messagesender .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
// send a copy to sender
$headers = 'From: '.$email_noreply."\r\n".
'Reply-To: '.$email_noreply."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, "Thanks For contacting!", "Thanks for contacting me, I will reply to you shortly. \n\n----------------\n\n" . $email_messagesender, $headers);
echo 'Message Succesfully Sent!<script>$(\'response-message\').text(\'Message Succesfully Sent!\');var form = document.getElementById("contact-form");form.reset();</script>';
}
?>
Contact form:
<div class="container">
<div class="row">
<div class="span7">
<form action="http://www.arjen079.com/contact.php" method="post" class="contact-form" id="contact-form">
<h1>Contact</h1>
<hr class="fancy-hr">
<input type="text" name="name" placeholder="Name" class="required">
<input type="email" name="email" placeholder="Email" class="required">
<input type="text" name="subject" placeholder="Subject" class="required">
<textarea name="message" placeholder="Message" class="required"></textarea>
<div class="response-message"></div>
<input type="submit" value="Submit" name="submit" class="float-right">
<div class="clear"></div>
</form>
Thanks already! ;)
echo 'Message Succesfully Sent!<script>$(\'response-message\').text(\'Message Succesfully Sent!\');var form = document.getElementById("contact-form");form.reset();</script>';
The error is in this part.
$(\'response-message\') should be $(\'.response-message\').
Notice the . you're missing? You're trying to target the ELEMENT called response-message, not the div with the class of it.
Related
Recently I've been having problems with my PHP contact form. It's worked great for about two years, and I haven't changed anything, so I don't really understand what the problem is. Here's the code:
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match ( "/[\r\n]/", $str );
}
if(isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message'];
// check to see if name or email have header injections
if (has_header_injection($name) || has_header_injection($email)){
die();
}
if ( !$name || !$email || !$msg ) {
echo '<h4 class="error">All Fields Required</h4>Go back and try again';
exit;
}
// add the recipient email to a variable
$to = "example#example.net";
// Create a subject
$subject = "$name sent you an email";
// construct your message
$message .= "Name: $name sent you an email\r\n";
$message .= "Telephone: $tel\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap(message, 72);
// set the mail header
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n\r\n";
// Send the Email
mail( $to, $subject, $message, $headers );
?>
<!--- END PHP CONTACT FORM -->
<!-- Show Success message -->
<h2>Thanks for contacting Us!</h2>
<p align="center">Please allow 24 hours for a response</p>
<p>« Go to Home Page</p>
<?php } else { ?>
<form method="post" action="" id="contact-form">
<label for="name">Your Name</label>
<input type="text" id="name" name="name">
<label for="tel">Your Phone Number</label>
<input type="tel" id="tel" name="tel">
<label for="email">Your Email</label>
<input type="email" id="email" name="email">
<label for="message">the date/time you wish to sign up for</label>
<textarea id="message" name="message"></textarea>
<br>
<input type="submit" class="button next" name="contact_submit" value="Sign Up">
</form>
<?php } ?>
However, when the contact form is submitted, instead of sending the information to the body of the email, it sends it in the "From" section of the email. For example, the email might say:
To: Web Developer
From: Bob Smith 888-888-8888 mondays, wednesdays fridays
Subject: Bob Smith sent you an email!
Body:
X-Priority: 1X-MSMail-Priority: high
message
I don't really know what's going on, so any help would be appreciated!
You are adding all that info in the "from" header.
$headers .= "\r\nFrom: " . $name . " \r\n\r\n" . $tel . " \r\n\r\n " . $msg . "\r\n\r\n <" . $email . "> \r\n\r\n";
Change your headers to this:
$headers = "MIME=Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$email}>\r\n"; // Removed all extra variables
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
and it should work.
You are already sending the $message, containing all the above data in the body as well.
Why you haven't experienced this before is however a mystery.
NOTE: You only need to have one \r\n after each header.
You should also change this row:
$message = wordwrap(message, 72);
to
$message = wordwrap($message, 72); // Adding $ in front of the variable.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$contact = $_POST['num'];
$email = $_POST['email'];
$message = $_POST['message'];
$ToEmail = 'info#kesems.com';
$EmailSubject = 'School Enquiry';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
header('Location:contact-us.php');
}
?>
contact-us.php
<form role="form" action="contact-us.php" id="main-contact-form" class="contact-form" name="contact-form" method="post">
<div class="row ml0">
<div class="form-group">
<input type="text" class="form-control" name="name" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" class="form-control" name="num" required="required" placeholder="Contact number">
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" required="required" placeholder="Email address">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" name="message" class="form-control" rows="3" placeholder="Any Queries/suggestions" style="resize:none"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg"/>
</div>
</div>
</form>
Simple script that sends email headers...still not working.
is code is correct?
any particular solution for this?
any particular solution for this?
thank you in advance.
Try this it worked for me
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: yourmail#gmail.com" . "\r\n" .
"Reply-To: no-reply#gmail.com" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$to = "tomail#gmail.com";
$subject = "This is subject";
$from = "yourmail#gmail.com";//optional
$message = ' this is my message hello';
if (mail($to, $subject, $message, $headers, 'ADMIN')) {
echo "mail sent"
}
else{
echo "error cannot send mail";
}
Check whether all the arguments are actually set (using ternary), just because the submit is set does not mean that all the arguments are set:
$name = isset($_POST['name']) ? $_POST['name'] : "";
$contact = isset($_POST['num']) ? $_POST['num'] : "";
$email = isset($_POST['email']) ? $_POST['email'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";
Check these values and make a condition that you display some error if something is not set.
If you're certain the values are set then you need to change
$ToEmail = 'info#example.com';
to
$ToEmail = $email;
Otherwise nothing's going to happen.
Finally, I recommend identifying your html inputs with a proper id, instead of a name.
Unrelated to the error but yet important to you
Like already pointed out by #Fred -ii-, a proper concatenation (.=) is required instead of overwriting (=) in the second header assignment of $MESSAGE_BODY:
MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY = "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
Should be:
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["num"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
$MESSAGE_BODY .= "Message: ".nl2br($_POST["message"])."\r\n";
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I think I fixed this. This was the original:
<?php
if(isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];;
$subject = $email = $_POST['subject'];;
$email_to = $email;
$header = 'From: 1totheN <website#1tothen.com>' . "\r\n";
function clean_text($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_text($email)."\n";
$email_message .= "Email: ".clean_text($email)."\n";
$email_message .= "Message: ".clean_text($message)."\n";
#mail($email_to, $subject, $email_message, $header);
echo "Your message has been sent.";
}
?>
Here's the markup on the HTML page:
<form class="contact-form" id="subscribeForm" action="mail.php" method="post">
<input type="text" name="name" class="required" placeholder="YOUR NAME">
<input type="text" name="email" class="required email" placeholder="EMAIL ADDRESS">
<input type="text" name="subject" class="required last" placeholder="SUBJECT">
<textarea name="message" placeholder="YOUR MESSAGE"></textarea>
<input type="submit" name="submit" value="SUBMIT YOUR MESSAGE">
<h6 id="form_result">Your email will not be published.</h6>
</form>
This is the fix:
$email_to = "webmail#1tothen.com";
$header = 'From: 1totheN <webmail#1tothen.com>' . "\r\n";
$email_message .= "Name: ".clean_text($name)."\n";
I think that fixes it. I added an email to the $email_to field, and added the $name. Seems to work.
The below now works. It's just small syntactic mistakes, you had a few additional ; and this line is wrong and presumably a mistake: $subject = $email = $_POST['subject'];
I also moved the function outside of the if statement...it's not incorrect to have it there, it just looks a bit weird.
function clean_text($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
if(isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$email_to = $email;
$header = 'From: 1totheN <website#1tothen.com>' . "\r\n";
$email_message .= "Name: ".clean_text($email)."\n";
$email_message .= "Email: ".clean_text($email)."\n";
$email_message .= "Message: ".clean_text($message)."\n";
#mail($email_to, $subject, $email_message, $header);
echo "Your message has been sent.";
}
?>
I have an Contact us page on my website. what i want is when someone fills the form and click on send button. The message should be arrived to my gmail. i wrote the following code for it. its not working. is there any other way i can accomplish the same.
Html code:
<form id="ContactForm" action="contacts.php" method="post">
<div>
<div class="wrapper"> <strong>Name:</strong>
<div class="bg">
<input type="text" class="input" name="name">
</div>
</div>
<div class="wrapper"> <strong>Email:</strong>
<div class="bg">
<input type="text" class="input" name="email">
</div>
</div>
<div class="textarea_box"> <strong>Message:</strong>
<div class="bg">
<textarea cols="1" rows="1" name="message"></textarea>
</div>
</div>
<span>Send</span> <span>Clear</span> </div>
</form>
php code
<?php
session_start();
$to = "someemail#gmail.com";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>
Firstly, you should check your inputs for PHP injection.
$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);
Apart from that, there doesn't seem to be anything wrong with your mail script. The problem is most likely caused from your PHP server. Does your web hosting definitely provide PHP mail? Most free web hosts do not provide this as they are often used for spamming.
Sorry, but your code is crappy (especially, those concatenations). Use Swift mailer which provides OOP-style and does all the header job for you. And make sure you've got any mail server installed (did you check if you have any?).
PHP form:
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Your Email
$receiver = 'max.mustermann#domain.tld';
if (isset($_POST['send']))
{
$name = $_POST['name']
$email = $_POST['email'];
if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
{
die( 'Please fill in all fields!' );
}
else
{
$subject = $_POST['subject'];
$message = $_POST['message'];
}
$mailheader = "From: Your Site <noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
$mailheader .= "Return-Path: noreply#" .$_SERVER['SERVER_NAME']. "\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
$mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
$mailheader .= "Message-ID: <" .time(). " noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";
if (#mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
{
echo 'Email send!';
}
}
?>
HTML form:
<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>
I am trying to make a form that will be sent to my email. But it display errors on submission. Javascript to validate the form is not shown but it works fine.
I am not sure what's wrong with my code below, been trying to figure out all day and reading various threads but to no avail.
Below is my php code to handle the form.
<?php
if(isset($_POST['name'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "me#gmail.com";
$email_subject = "Nexwave Form";
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.<br /><br />";
die();
}
$name = $_POST['name']; // required
$designation = $_POST['designation'];
$company = $_POST['company'];
$contact = $_POST['contact'];
$email = $_POST['email']; // not required
$users = $_POST['users']; // required
$error_message = "";
$string_exp = "/^[A-Za-z .'-]+$/";
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Designation: ".clean_string($designation)."\n";
$email_message .= "company: ".clean_string($company)."\n";
$email_message .= "Contact ".clean_string($contact)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "Number of Users: ".clean_string($users)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
echo (int) mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
Below is my html code:
<form name="form" action="send_mail.php" onSubmit="return validateForm();" method="POST">
Name<br/><input name="name" type="text" style="width:90%;"/><br/>
Designation<br/><input name="designation" type="text" style="width:90%;"/><br/>
Company<br/><input name="company" type="text" style="width:90%;"/><br/>
Contact Number<br/><input name="contact" type="text" style="width:90%;"/><br/>
Email<br/><input name="email" type="text" style="width:90%;"/><br/>
Number of Users<br/><input name="users" type="text" style="width:90%;"/>
<span style="text-align:right;display:block;width:174px;"><input style="height:25px;margin-top:20px;margin-bottom:10px;background-color:#ffffff;border:0;color:#009110;" type="submit" class="submit" value="Submit" /></span>
</form>
below is the error:
This is what i get even after changing the email_address and email_from.
"; echo $error."
"; echo "Please go back and fix these errors.
"; die(); } $name = $_POST['name']; // required $designation = $_POST['designation']; $company = $_POST['company']; $contact = $_POST['contact']; $email = $_POST['email']; // not required $users = $_POST['users']; // required $error_message = ""; $string_exp = "/^[A-Za-z .'-]+$/"; $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Name: ".clean_string($name)."\n"; $email_message .= "Designation: ".clean_string($designation)."\n"; $email_message .= "company: ".clean_string($company)."\n"; $email_message .= "Contact ".clean_string($contact)."\n"; $email_message .= "email: ".clean_string($email)."\n"; $email_message .= "Number of Users: ".clean_string($users)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email."\r\n" . 'X-Mailer: PHP/' . phpversion(); echo (int) mail($email_to, $email_subject, $email_message, $headers); ?> Thank you for contacting us. We will be in touch with you very soon.
your help is very much appreciated
You have many errors present in this script as mentioned by question comments above.
email form name email_address is not the same as $_POST['email']
you are not defining $email_from before you call it.
I would do some simple testing before posting a question like this and try to echo out all the variables one by one and see what you get. This would show you the two mentioned errors. And if you cannot solve it and still have errors then please post your error message so others can be of more assistance than just it doesn't work.