This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have a form in html file and I'm trying to get email on my gmail account when user fill the form and submit it. My website is running on Server not on local host.My code has no error but still not getting the email .I dont know why? Can any one give me sample code or tell me what am I doing wrong? .. Thanks in advance ...
Here is my Code
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send Now</button>
</div>
</form>
And the sendemail.php is
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'khurram.ansar#gmail.com';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
According to the documentation :
Multiple extra headers should be separated with a CRLF (\r\n).
So your php file should be like :
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'khurram.ansar#gmail.com';
$headers = "MIME-Version: 1.0" . "\r\n" .
"Content-type: text/plain; charset=iso-8859-1" . "\r\n" .
"From: {$name} <{$from}>" . "\r\n" .
"Reply-To: <{$from}>" . "\r\n" .
"Subject: {$subject}" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
die;
?>
Related
<div class="col-lg-12 col-md-12 offset-lg-1">
<div class="impl_con_form">
<div class="col-lg-12 col-md-12">
<h1>get in touch</h1>
</div>
<form action="sendmail.php" method="POST" class="contact-form" novalidate="novalidate">
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="first_name" class="form-control require" placeholder="YOUR NAME">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="email" class="form-control require" placeholder="YOUR EMAIL" data-valid="email" data-error="Email should be valid.">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="SUBJECT">
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group">
<textarea name="message" class="form-control" placeholder="YOUR MESSAGE"></textarea>
</div>
</div>
<div class="response"></div>
<div class="col-lg-12 col-md-12">
<input type="hidden" name="form_type" value="contact">
<button type="submit" data-type="submit" name="submit" value="submit">Submit</button>
</div>
</form>
</div>
</div>
The Code above is a contact form for one of my practice projects im working on. Im trying to get it to link with my sendmail.php file and send it to my current email address but im not having any luck.
<?php
if( isset($_POST['submit']))
$to = "jamie#sharpsdigital.co.uk";
$sub = "Contact Form Enquiry";
$headers = "From: Star Tyres <jamie#sharpsdigital.co.uk>"."\r\n";
$headers .= "Reply-To: ".$email." \r\n";
$headers .= "X-Mailer: PHP/". phpversion()."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$first_name.'</p>
<p>Email: '.$email.'</p>
<p>Subject: '.$subject.'</p>
<p>Message: '.$message.'</p><br>
';
$mail = mail($to, $sub, $message, $headers, 'jamie#sharpsdigital.co.uk');
if ( $mail)
{
header('Location: contact.html?var=sent');
}
else
{
/* error_reporting(-1);
ini_set('display_errors', 'On');
echo 'error';
print_r(error_get_last()); */
header('Location: contact.html?var=error');
}
This one is my php, I have linked up both with using the action="sendmail.php" and using the method post. However when I got to click the button and send it just seems to go to *.com/sendmail.php and I dont get an email. What am I doing wrong?
UPDATE ==============================
After a few comments I have tried to tweak a few things. im now getting the echo command back once I hit the submit button but nothing else.
<?php
extract($_POST);
echo "Made it to sendmail.php";
if( isset($_POST['submit']))
{
$mailTo = "jamie#sharpsdigital.co.uk";
$sub = "Contact Form Enquiry";
$headers = "From: Star Tyres <jamie#sharpsdigital.co.uk>"."\r\n";
$headers .= "Reply-To: ".$email." \r\n";
$headers .= "X-Mailer: PHP/". phpversion()."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$first_name.'</p>
<p>Email: '.$email.'</p>
<p>Subject: '.$sub.'</p>
<p>Message: '.$message.'</p><br>
';
$mail = mail($to, $sub, $message, $headers, 'jamie#sharpsdigital.co.uk');
}
?>
Change your button to submit the form to this:
<input type="button" name="submit" value="Submit">
input is an element of a form, a button no. The name is what you will look for in the php ($_POST['submit'])
The value attribute is what will be displayed inside the button
HTH
<?php
extract($_POST);
if( isset($_POST['submit']))
{
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$to = "jamie#sharpsdigital.co.uk";
$sub = "Contact Form Enquiry";
$headers = "From: Star Tyres <jamie#sharpsdigital.co.uk>"."\r\n";
$headers .= "Reply-To: ".$email." \r\n";
$headers .= "X-Mailer: PHP/". phpversion()."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$first_name.'</p>
<p>Email: '.$email.'</p>
<p>Subject: '.$sub.'</p>
<p>Message: '.$message.'</p><br>
';
$mail = mail($to, $sub, $message, $headers, 'jamie#sharpsdigital.co.uk');
}
?>
or, more simply:
<?php
$to = 'jamie#sharpsdigital.co.uk';
$subject = 'Contact Form Enquiry';
$message = '<h4>New Contact Form Enquiry : </h4>
<p>Name: '.$_POST['first_name'].'</p>
<p>Email: '.$_POST['email'].'</p>
<p>Subject: '.$subject .'</p>
<p>Message: '.$_POST['message'].'</p><br>
';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: ".$_POST['email']' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
TESTING PURPOSE:
create new file testemail.php and input this code:
<?php
$to = 'jamie#sharpsdigital.co.uk';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: no-reply#domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
then browse to page and see if you received an e-mail after...
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
When I trying send emails with form in html 5 - this doesn't work and I don't know why.
Look: This is form in html:
<div class="col-md-4 col-sm-12">
<div class="contact-form bottom">
<h2>send message</h2>
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Your name">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Your e-mail">
</div>
<div class="form-group">
<textarea name="message" id="message" required="required" class="form-control" rows="8" placeholder="message..."></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
</div>
and this is sendemail.php:
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'MYMAIL#gmail.com';//replace with your email
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
I hope you help me. Thanks a lot!
mail() functions expects headers parameter as String and you have passed an Array. Try to use as below :
$headers = '';
$headers .= "MIME-Version: 1.0";
$headers .= "Content-type: text/plain; charset=iso-8859-1";
$headers .= "From: {$name} <{$from}>";
$headers .= "Reply-To: <{$from}>";
$headers .= "Subject: {$subject}";
$headers .= "X-Mailer: PHP/".phpversion();
I have a problem with receiving mail. I have tried different mail providers (microsoft, yahoo, gmail) and still not receiving mail. I am working on web site for my friend and I bought a bootstrap template with php contact.
I am using xampp and I have tried sending the mail that way and I have upload it on some other friends web site in sub folder and still not receiving it.
Here is the code:
HTML index.php
<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
<div id="msgInfo"></div>
<input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name">
<input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email">
<textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message"></textarea>
<button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>
Here is the contact.php code:
<?php
// PHP script for sending email
//
// Configuration
//
$toEmail = ""; // replace with your email where you would like to send email
$subject = 'Contact form from my website'; // replace with subject you want to receive
$body = 'You have received email from website:'; // replace with text that you want to receive in email
$from = ''; // replace with email that will look like sender
//
// ----- do not edit after this line if you don't understand what you are doing -----
//
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "Invalid input";
return false;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body .= "\n";
$body .= "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message\n";
$headers = "From: $email\n";
$headers .= "Reply-To: $toEmail";
$res = mail($toEmail, $subject, $body, $headers);
echo "OK";
return true;
?>
This isn't my first time that I am working with php, in the past I had a working php(2-3 years ago) and I have tested that also but it doesn't work -.-
Can someone help me please.
You have to add name attribute for all input/textarea tags in your form so you can find them in your $_POST array
<form class="js-contact-form" role="form" action="mail/contact.php" method="post" data-parsley-validate>
<div id="msgInfo"></div>
<input type="text" class="form-control wow fadeInLeft name" data-wow-delay="0.2s" placeholder="Name" required data-parsley-error-message="Enter name" name="name">
<input type="email" class="form-control wow fadeInLeft email" data-wow-delay="0.4s" placeholder="Email" required data-parsley-error-message="Enter email" name="email">
<textarea class="form-control wow fadeInLeft message" data-wow-delay="0.6s" rows="6" placeholder="Message" required data-parsley-error-message="Enter message" name="message"></textarea>
<button type="submit" class="btn btn-lg wow fadeInUp" data-wow-delay="1s">Send message</button>
</form>
and change this line in your php code ($ToEmail variable is always empty)
$res = mail($toEmail, $subject, $body, $headers);
with this
$res = mail($email, $subject, $body, $headers);
Try to replace with this
$body .= "\n";
$body .= "Name: ".$name."\n";
$body .= "Email: ".$email."\n";
$body .= "Messagee: ".$message."\n";
and change the headers:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers.= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = 'From: '.$name."\r\n".'Reply-To: '.$email."\r\n".'X-Mailer: PHP/' . phpversion();
Update
if($res){
echo "OK";
}
else{
echo "failed."
}
By this you can know whether your mail function in working or not.
Enable errors in php if not enabled using:
ini_set('display_errors',1);
I have a simple contact form that is failing to send, I'm new to PHP so it's most likely I have forgotten something or the code is wrong. I understand that there are many questions on here around the same subject but I'm unsure about my code.
I'm getting the error message: Undefined index: send in \mail.php on line 31
HTML
<html>
<head>
<title>form</title>
</head>
<body>
<h2>Contact Form</h2>
<form id="form_id" name="form_name" action="mail.php" method="post">
<div>
<input type="text" name="name" id="name" placeholder="Name" required/>
</div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
</div>
<div>
<input type="number" name="tel" id="tel" placeholder="Phone" required/>
</div>
<textarea name="message" type="text" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</form>
</body>
</html>
PHP
<?php
$to = 'test#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];
$body = <<<EMAIL
This is a message for your website.
Name: $name
Email: $email
Tel: $tel
Message: $message
EMAIL;
// 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";
// Additional headers
$headers .= 'To: Bob <$to>, Bob <$to>' . "\r\n";
$headers .= 'From: Website <noreply#example.com>' . "\r\n";
$headers .= 'Cc: noreplt#example.com' . "\r\n";
$headers .= 'Bcc: noreply#example.com' . "\r\n";
if ($_POST['send']){
mail($to, $subject, $body, $header);
echo 'Message Sent.';
} else {
die('Failed to Send');
}
?>
use mail($to, $subject, $body, $headers);
here you used $header
I'm relatively new to PHP and I'm having trouble getting my PHP to take my form data and submit it in an email. The email sends to me with the plain text I have in it but all the form data isn't there. Does anybody have any ideas of what's wrong?
Here's the HTML:
<div id="box3" class="clearfix">
<form action="contact.php" method="post" enctype="text/plain">
<div id="greyContainer" class="clearfix">
<p id="text5">
Contact Us
</p>
<p id="text6">
Feel free to contact us and leave a message if you have any general questions!
</p>
<label id="formgroup">
<p id="text7">
Name:
</p>
<input id="name" type="text" value="Full Name" name="name"></input>
</label>
<label id="formgroup1">
<p id="text8">
Number:
</p>
<input id="phone_number" type="text" value="Phone Number" name="phone_number"></input>
</label>
<label id="formgroup2">
<p id="text9">
Email:
</p>
<input id="email" type="text" value="Email Address" name="email"></input>
</label>
<label id="formgroup3">
<p id="text10">
Message:
</p>
<textarea id="message_block" name="message_block" >Message or question...</textarea>
</label>
<p id="text11">
<span id="textspan">AVA Roofing & Siding<br />12 Arcade Ave.<br />Amherst, NY 14226<br />716.602.3947</span><br />
</p>
<img id="image1" src="img/envelope.png" class="image" />
<a href="thank_you.html">
<input id="input" type="submit" value="Submit" name="submit" ></input>
</a>
<div id="whiteLine" class="clearfix">
</div>
</form>
</div>
</div>
Here's the PHP:
<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$to = "example#gmail.com" ;
$from = $_POST['email'] ;
$name = $_POST['name'] ;
$headers = "From: $from";
$subject = "AVA Roofing Contact Form";
$message2 = $_POST['message_block'];
$number = $_POST['phone_number'];
$message = "A visitor has submitted the following requirements. \n\nName: $name\n\nEmail: $email\n\nNumber: $number\n\nMessage: $message";
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
$send = mail($to, $subject, $message, $headers);
if($send)
{header( "Location: http://www.example.com/folder/thank_you.html" );}
else
{print "We encountered an error sending your mail, please notify webmaster#YourCompany.com"; }
?>
You've got the wrong encoding on your form:
<form action="contact.php" method="post" enctype="text/plain">
^^^^^^^^^^^^^^^^^^^^^
Since you're telling the browser to send it as text/plain, it will not be processed by PHP. For this simple form, you don't need to specify ANY encoding. Just accept the defaults.
If you insist on specifying an encoding, it should be application/x-www-form-urlencoded for standard forms, or multipart/form-data if you're doing file uploads.
You can send to other file like file.php and in this file the action you would have something like this:
$recipiente = "your#email.com ";
$asunto = "Formulario";
$message ="name: ".$name."<br>";
$message .="email: ".$email."<br>";
$message .="coment: ".$coment."<br>";
$message = stripslashes($message);
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\r\n";
$headers .= "From: $email\r\n";
$headers .= "Reply-to: $email\r\n";
$headers .= "Cc: $email\r\n";
mail($recipiente,$asunto,$message,$headers);