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>
Related
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";
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Recently, I have purchased a domain and hosting. I've also created a webpage having contact form in it. I want when the user wrote something in textarea of contact form and clicks submit then that message is sended to my gmail account and so that i can reply also to those messages. I've also used this script to do so but its not working.
This is sendemail.php file
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'raunakhajela#gmail.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
This is my contact form code in index.php
<div class="contact" id="contact">
<div class="container-3x">
<div class="block-header"> <!-- Blocks Header -->
<h2 class="title">Contact Us</h2>
</div>
<div class="block-content"> <!-- Blocks Content -->
<form action="sendemailphp" method="post" class="trigger-form-animate">
<input type="text" value="" id="name" name="name" placeholder="Name *" />
<input type="text" value="" id="email" name="email" placeholder="Email *" />
<input type="text" value="" id="website" name="website" placeholder="Website *" />
<textarea type="text" value="" id="message" name="message" rows="3" placeholder="Your Message *" ></textarea>
<div class="clr"></div>
<button>Submit Message</button>
</form>
</div>
</div>
</div>
I've also tried "http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html" this tutorial but its not working. Unable to find "Send through Gmail (easier to set up)" this option in gmail on adding another accounts windoww.
How can i do that?? Plzz hlp
You don't have a named subject form element which may explain why mail is failing and may very well be sent to Spam instead.
For example: <input type="text" name="subject">. Either add one of replace.
You will however need to make sure that this is filled out using a conditional statement
(see further below for an example).
$subject = #trim(stripslashes($_POST['subject']));
with
$subject = "Form submission";
You could also add this instead to your form:
<input type="hidden" name="subject" value="Form submission">
Either this or what I've outlined just above will work.
Many Email clients will either send mail to Spam or reject it altogether if a "subject" is missing, which is clearly missing from your code.
Checking if subject has been filled example:
if(isset($_POST['subject']) && !empty($_POST['subject'])){
// do someting
}
try this,both will work
<?php
$to = "usermailid#gmail.com";
$subject = "Welcome to";
$message = " Hi $username,<br /><br />
Thank you for signing up with us.<br />
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
if($mail)
{
$to = "admin#gmail.com";
$subject = "Following Customer Signed Up";
$message = " $username,Customer is signed up with us,<br /><br />
Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
Phone:$phone<br/>Zip Code:$zip<br/>Message:$message_cust<br/><br/><br/>
Thanks <br />";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <yourmailid#gmail.com>' . "\r\n";
$mail=mail($to,$subject,$message,$headers);
}
?>
I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>
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");
}
?>
I'm trying to send a php mail but it seems that I have a error in my foreach because the mail shows html..
This is my code:
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<label for="fullname">Votre Nom :
<input id="fullname" name="fullname" type="text" value="nelson" />
</label>
<label for="emailaddress" class="margin">Votre e-mail:
<input id="email" name="email" type="text" value="" />
</label>
<label for="message">Message:<br />
<textarea id="message" name="message" cols="40" rows="8"></textarea>
</label>
<p>
<input id="submit-button" class="button gray stripe" type="submit" name="submit" value="Envoyer le message" />
</p>
</fieldset>
</form>
<?php
foreach ($_POST as $value) {
$value = strip_tags($value);
$value = htmlspecialchars($value);
}
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
if (isset($_POST['submit'])) {
mail($to, $suject, $message);
echo"mail had been sent";
}
?>
Can anyone help me please
You need to set the Content-type header in your email message:
$name = $_POST["fullname"];
$email = "email:" .$_POST["email"];
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .$_POST["message"];
$to="email#hotmail.com";
$suject="site internet";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (isset($_POST['submit'])) {
mail($to, $suject, $message, $headers);
echo"mail had been sent";
Your message body also needs to be contained in <html> tags.
Your foreach is kind of useless, just do that. More fast than a loops
$name = strip_tags(htmlspecialchars($_POST["fullname"]));
$email = "email:" .strip_tags(htmlspecialchars($_POST["email"]));
$message = "Nom: <br/>" .$name. "email:<br/> " .$email. "message: " .strip_tags(htmlspecialchars($_POST["message";));
To send email containing HTML you must set the header so that the email client knows that the email contains HTML. You also have to make the body of your email an HTML document.
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
mail($to, $suject, $message, $header);
And then in the message itself:
<html>
<head></head>
<body>
Content here
</body>
</html>