Updated: I have a simple PHP form that should submit data to an email address but it isn't sending it. it's just sending the field names.
Here's the code:
<?php
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".nl2br($_POST["phone"])."\r\n";
$MESSAGE_BODY .= "Email: ".nl2br($_POST["email"])."\r\n";
$MESSAGE_BODY .= "Opt in: ".nl2br($_POST["send"])."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<form class="form-signin" name="index" action="index.php" method="post">
<img src="img/coffee.png" width="235" height="227">
<h2 class="form-signin-heading">Enter your information</h2>
<input name="name" type="text" class="input-block-level" id="name" placeholder="Name">
<input type="text" class="input-block-level" name="surname" id="surname" placeholder="Surname">
<input type="text" class="input-block-level" name="designation" id="designation" placeholder="Designation">
<input type="text" class="input-block-level" name="phone" id="phone" placeholder="Cell Number">
<input type="text" class="input-block-level" name="email" id="email" placeholder="Email">
<label class="checkbox">
<input type="checkbox" value="Send-me-helpful-information" name="send" checked> <p>Send me helpful information</p>
<input name="submit" type="submit" value="submit" class="btn btn-large btn-primary" id="go" rel="leanModal" href="#thankyou">
<p>Terms & Conditions Apply.
Click HERE</p>
</form>
Please help
Comment: It's sending now but submits a blank email on page load. Does anyone know a fix for this?
Change your submit button to this:
<input type="submit" value="Submit" />
Your PHP should look more like
if(!empty($_POST)) { // This is to say if the form is submitted.
// All your PHP post stuff here. The email sending stuff.
$errors = array();
// Do this for all your required fields.
if(isset($_POST['email']) && $_POST['email'] != '') { // If there's a variable set and it isn't empty.
$mailheader = "From: " . $_POST['email'] . "\r\n";
} else {
$errors[] = "No email submitted";
}
print_r($_POST); // This is for debugging. Remove it when satisfied.
echo $MESSAGE_BODY; // Also for debugging. Ensure this is fine.
if(empty($errors)) {
if(mail()) { // Your mail function
// Successful mail send, either redirect or do whatever your do for successful send
} else {
// Mail failure, inform user the sending failed. Handle it as you wish. Errors will be sent out.
}
} else {
print_r($errors); // See what's going error wise.
}
}
<form name="form1" id="form1" action="" method="post"> // form like this
<input type="submit" name="submit" id="submit" value="submit"> // submit button like this
</form>
<?php if(isset($_REQUEST['submit']))
{
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
} ?>
<?php
if(isset($_POST['submit'])){
//your sending email php code
}
?>
function mail()
uses SMTP Server so make sure it is perfectly configured and installed if you are using it on your local server such as WAMP or ZAMP.
rest if it still not working put your php code in between:
<?php
if(isset($_POST['submit']))
{
// ur php code here
}
?>
Since your form is posting some values, you need to have an <input type="submit"> to post those. name is just an indentifier, if you will call it "submit", it won't work.
Try adding <input type="submit" value="Whatever you want your text to be"> instead of <button name="submit" type="submit" class="btn btn-large btn-primary"><a id="go" rel="leanModal" href="#thankyou">Submit</a></button>.
If you have another problem, please explain it more. What errors do you get?
Try adding some echo to see where your program stops working.
Try this
<?php
if($_POST['submit']!="")
{
$ToEmail = 'email#yahoo.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."\r\n";
$MESSAGE_BODY .= "Surname: ".$_POST["surname"]."\r\n";
$MESSAGE_BODY .= "Designation: ".$_POST["designation"]."\r\n";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."\r\n";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."\r\n";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
}
?>
Related
I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?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']);
$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 '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// 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);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>
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";
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 wrote this code but it isn't sending it to my email. What can be wrong?
This is the code from the contact form:
<!-- send mail configuration -->
<input type="hidden" value="send-mail.php" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
This is the code for the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['kurtfarrugia92#gmail.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
Basic PHP mail script.
file mail.php
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("someone#example.com", $subject,
$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mail.php'>
Email: <input name='email' type='text'><br>
Subject: <input name='subject' type='text'><br>
Message:<br>
<textarea name='message' rows='15' cols='40'>
</textarea><br>
<input type='submit'>
</form>";
}
?>
</body>
</html>
Do you have error reporting on? It may be handy to get more info about what is going on/wrong.
error_reporting(E_ALL);
Try:
echo "mail($mail, $subject, $msg, $headers)";
to see if what you are sending to mail() is exactly what you think it is.
Hi I've previously used this very simple php contact script with success though when I've tried implementing it on a new HTML page with the form won't submit. Can anyone see any obvious errors?
Any help would be much appreciated
Here is the html of the form:
<div id="formContainer">
<form action="form.php" method="post" id="contactForm">
<fieldset>
<legend>Your details</legend>
<label for="name">Name *</label>
<input type="text" id="name">
<label for="email">Email *</label>
<input type="email" id="email">
<label for="tel">Telephone</label>
<input type="tel" id="tel">
</fieldset>
<fieldset>
<legend>Tutoring</legend>
<label for="type">Type of lesson</label>
<select name="type" id="type">
<option>Individual</option>
<option>Group</option>
</select>
<label for="subject">Subject</label>
<input name="subject" list="subjects" id="subject">
<datalist id="subjects">
<option>English</option>
<option>Biology</option>
<option>Geography</option>
</datalist>
<label for="level">Your level</label>
<select name="level" id="level">
<option>Beginner</option>
<option>GCSE</option>
<option>A-Level</option>
<option>University</option>
</select>
<label for="hours">Hours/week</label>
<input type="number" id="hours">
<label for="info">Additional Information</label>
<textarea name="info" id="info" rows="10" cols="6"></textarea>
</fieldset>
<input type="submit" name="submit" value="Send" id="sendButton">
<input type="hidden" name="submit_check" value="1" />
</form>
</div>
And here is the simple php script:
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'onjegolders#gmail.com';
$EmailSubject = 'Site contact form ';
$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"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
$MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
$MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
$MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
$MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
$MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
<html>
<h3>Thanks for your email</h3>
<h4>I'll get back to you as soon as possible</h4>
<p>Click here to go back to previous page</p>
</html>
<?php
} else {
?>
<html>Sorry, this form didn't work</html>
<?php
};
?>
Try with
if ( !empty($_POST["email"]) )
However you can check, what is posted in that page using:
echo '<pre>';
var_dump( $_POST );
Change your form.php as the following
<?php
if ($_POST["email"]<>'') {
$ToEmail = 'onjegolders#gmail.com';
$EmailSubject = 'Site contact form ';
$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"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY = "Telephone: ".$_POST["tel"]."<br>";
$MESSAGE_BODY = "Type: ".$_POST["type"]."<br>";
$MESSAGE_BODY = "Subject: ".$_POST["subject"]."<br>";
$MESSAGE_BODY = "Level: ".$_POST["level"]."<br>";
$MESSAGE_BODY = "Hours required: ".$_POST["hours"]."<br>";
$MESSAGE_BODY .= "Additional information: ".nl2br($_POST["info"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
echo <<<EXCERPT
<html>
<h3>Thanks for your email</h3>
<h4>I'll get back to you as soon as possible</h4>
<p>Click here to go back to previous page</p>
</html>
EXCERPT;
} else {
echo "<html>Sorry, this form didn't work</html>";
}
?>
in your
if ($_POST["email"] <> '') {
change that into
if ($_POST["email"] != '') {
You should try this:
if(!empty($_POST["email"])){
//your email preparation code
}
Insted of using this:
if($_POST["email" <> ''){
//your email preparation code
}
The ! basically means not, so the !empty means not empty
You could also just use:
if ($_POST["email"]) {
This works much like the != "" or empty() check. PHP was incepted to handle forms well. And you can just let it probe incoming form fields. It uses some magic boolean conversion rules for strings, which most of the time will accomplish what you want.
Another advantage of this simpler style is that it eases debugging if you enable the E_ALL and E_NOTICE (=debug) error_reporting mode.