I'm using this tutorial to create a contact form for my site and it doesn't seem to be working. After I fill in my form and press submit, nothing happens. I would like to know whats wrong with it and how I can fix it.
This is my code:
HTML:
<form action="mail/contact.php" method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name:</label><br />
<input type="text" name="name" id="name" class="form-control" placeholder="Name"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email:</label><br />
<input type="text" name="email" id="email" class="form-control" placeholder="Email"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message:</label>
<textarea name="message" rows="5" id="message" class="form-control" placeholder="Message"></textarea>
</div></div>
<br />
<input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg"/>
</form>
PHP:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'name#mail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
It's not a problem actually, you hace to change $to to $email in the email() method because the variable that has the email you want to send to is $email.
Your contact.php code will be:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($email, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
Another recommendation is to check this tutorial for a secure contact form.
Also you should check out this two php frameworks:
Symfony 2 Mail
Zend 2 Mail
Related
I tested to receive to my own email, for some reason I get everything but its not showing the content of what I type. It doesnt show the actual message when I receive it. Can anyone help? Thanks!
<div class="contact-heading">
</div>
<form role="form" class="contact-form" action="contact-us.php" method="post">
<div class="form-group">
<label class="" for="exampleInputPassword1">Name</label>
<input type="text" name="name" class="form-control" id="exampleInputPassword1" placeholder="Name:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail2">Email address</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail2" placeholder="E-Mail:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail3">Company</label>
<input type="text" name="company" class="form-control" id="exampleInputEmail3" placeholder="Company:" required >
</div>
<div class="form-group">
<label class="" for="exampleInputEmail4">Message</label>
<textarea class="form-control textarea" name="message" rows="8" id="exampleInputEmail4" placeholder="Message:"></textarea>
</div>
<button type="submit" class="btn btn-submit">Submit</button>
</form>
</div>
<?php
$to = 'emailsample#yahoo.com';
$subject = "Contact us";
$name = stripslashes($_REQUEST['name']);
$email = stripslashes($_REQUEST['email']);
$company = stripslashes($_REQUEST['company']);
$message = stripslashes($_REQUEST['message']);
$msg .= "Name: ".$name."\r\n";
$msg .= "E-mail: ".$email."\r\n";
$msg .= "Company: ".$company."\r\n";
$message .= "Topicjk: ".$message."\r\n";
$msg .= "Subject: ".$subject."\r\n\n";
$msg .= "---Message--- \r\n";
$msg .= "\r\n\n";
$mail = #mail($to, $subject, $msg, "From:".$email);
if($mail) {
header("Location:contact-us.html");
} else {
echo 'Message could not be sent! Please try again.';
}
?>
Because you are not passing $message (which has content) to mail in this line.
Change from
$mail = #mail($to, $subject, $msg, "From:".$email);
To
$mail = #mail($to, $subject, $message, "From:".$email);
or
Change from
$message .= "Topicjk: ".$message."\r\n";
To
$msg .= "Topicjk: ".$message."\r\n";
I have a contact form in my website and it doesn't work properly. I get the email but the email it's empty, I don't get the information from the form.
the part of the html in my index.html
<form role="form" form name="contactform" method="post" action="send_form_email.php">
<div class="form-group">
<label>Nombre</label> <input type="text" name="nombre" class="form-control" placeholder="Introduce tu nombre" >
</div>
<div class="form-group">
<label>Apellidos</label> <input type="text" name="apellidos" class="form-control" placeholder="Introduce tus apellidos" >
</div>
<div class="form-group">
<label>Email</label> <input type="emal" name="email" class="form-control" placeholder="Introduce tu email" >
</div>
<div class="form-group">
<label>Telefono</label> <input type="text" name="telefono" class="form-control" placeholder="Introduce tu telefono" >
</div>
<div class="form-group">
<label>Mensaje</label> <textarea name="mensaje" class="form-control" rows="7"></textarea>
</div>
<button type="submit" class="btn btn-default" value="Submit"> Enviar
</form>
</div>
the part of the php file called "send_form_email.php"
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent=" $nombre $apellidos $email $telefono $mensaje";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
<input type="emal" name="email" class="form-control" placeholder="Introduce tu email" > change to
<input type="email" name="email" class="form-control" placeholder="Introduce tu email" >
you have typo in type attribute.
Try this
<?php
$nombre = $_POST['nombre'];
$apellidos = $_POST['apellidos'];
$telefono = $_POST['telefono'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$formcontent = "{$nombre} {$apellidos} {$email} {$telefono} {$mensaje}";
$recipient = "deniz946#gmail.com";
$subject = "Subject from $nombre";
if(PHP_OS == "Linux" || PHP_OS == "Darwin") $new_line = "\n"; /* if Linux or Mac */
elseif(PHP_OS == "WINNT") $new_line = "\r\n"; /* if Windows */
$mailheader = "MIME-Version: 1.1" . $new_line;
$mailheader .= "Content-type: text/html; charset=utf-8" . $new_line;
$mailheader .= "From: ".$recipient.$new_line;
$mailheader .= "Return-Path: " . $recipient . $new_line;
$mailheader .= "Reply-To: " . $recipient . $new_line;
mail($recipient, $subject, $formcontent, $mailheader, "-r" . $recipient) or die("Error!");
echo "Thank You! We will get back to you as soon as possible!" . " -" . "<a href='./index.php'> Back to site</a>";
?>
Perhaps I can help you with an example I used myself:
The HTML contact Form:
<div class="row">
<!-- Content -->
<div id="content" class="8u skel-cell-mainContent">
<section class="12u">
<header>
<h2>Contact</h2>
</header>
<form method="post" action="mail.php">
<div class="row half">
<div class="6u">
<input name="subject" placeholder="Name" type="text" class="text" />
</div>
<div class="6u">
<input name='email' placeholder="Email" type="text" class="text" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row half">
<div class="12u">
<!--a href="mail.php" class="button">Submit</a-->
<input type="submit" class="button" value="Submit">
</div>
</div>
</form>
</section>
</div>
The PHP processing of the form:
<?php
//mail(to,subject,message,headers,parameters)
// Check if the "from" input field is filled out
//if (isset($_POST['from']))
//{}
$to = 'mail#xxxxxx.com'; // my email address
$subject = $_POST['subject']; //name client
$feedback = 'thank you, we will reply soon.';
$header = $_POST['email']; //Email client
$message = $_POST['message'];
$message = wordwrap($message, 70);
$message = <<<EMAIL
Dear (YOUR WEBSITE NAME HERE),
My name is: $subject
$message
Email Client: $header
EMAIL;
// send mail
if($_POST){
mail($to, $subject, $message, $header);
echo $feedback;
}
?>
Remember very carefully, the part from Dear... to EMAIL has to be against the left side of the screen, without any tabs or spaces. Else it won't work!
Hopefully you can use this to your success :)
I keep getting an error as follows when i submit my form. I'm not sure what it is. I'm running the site on WAMP (windows) and im writing it in dreamweaver CS6. Please note I'm fairly new to PHP. Thanks!
Here is the code:
Notice: Undefined index: message in S:\wamp\www\latech\contact.php on line 29
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
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 = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\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);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
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 = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\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);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
Your name attribute is msg your calling it message in PHP.
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
$msg = $_POST['message']; // no need to trim message
Also here...
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized.
You are using
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
Rather use
<textarea id="msg" name="message" spellcheck="true" ></textarea>
I am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>
Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!
I am using below simple code to send mail using php. when i echo the line $headers = "From: . $email\r\n"; //echo $headers; exit; which results like From: . test#email.com.
I found dot is printing before mail address , so mail is coming but not properly.If i remove dot , mail is not even coming.Whats wrong with this code ?
<?php
if (isset($_REQUEST['user_email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['user_email'];
$user_name = $_REQUEST['user_name'];
$user_message = $_REQUEST['user_message'];
include 'contact_mail_form.php';
$to = "testtest#gmail.com";
$message = $contactText;
$subject = "Test Mail";
$headers = "From: . $email\r\n"; //echo $headers; exit;
$headers .= "Content-type: text/html\r\n";
mail($to,$subject,$message,$headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
?>
<form id="contact_form" name="ContactForm" action="" method="post" onsubmit="return checkContact(this);">
<div class="wrapper">
<div class="fright">
<div class="name">Message:</div>
<textarea cols="1" rows="1" name="user_message" id="user_message" ></textarea>
<span class="clear"></span> </div>
<div class="fleft">
<div>
<div class="name">Your Name:</div>
<input type="text" class="input" name="user_name" id="user_name" />
<span class="clear"></span> </div>
<div>
<div class="name">E-mail:</div>
<input type="text" class="input" name="user_email" id="user_email" />
<span class="clear"></span> </div>
</div>
</div>
<div class="buttons"><a class="more" href="javaScript:void(0);" onClick="document.getElementById('contact_form').reset()">clear</a> <input class="more1" type="SUBMIT" class="button2" value="Send" name="" /> </div>
</form>
<?php } ?>
Try it like this your dot is acting as string not as concatenation
$headers = "From:" . $email."\r\n";
Maybe because \r\n should be surrounded with " not '
And Also
$headers .= 'Content-type: text/html'. "\r\n";