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();
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I am trying to create a contact me form with HTML and PHP but whenever someone try to send me a message it gives an error.
here is my HTML code:
<div class="col-lg-9">
<form class="row contact_form" action="contact_process.php" method="post" id="contactForm" novalidate="novalidate">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Enter your name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email address">
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Enter Subject">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" name="message" id="message" rows="1" placeholder="Enter Message"></textarea>
</div>
</div>
<div class="col-md-12 text-right">
<button type="submit" value="submit" class="primary_btn">
<span>Send Message</span>
</button>
</div>
</form>
</div>
and it is associated with a PHP file contact_process.php and here it is:
<?php
$to = "mygmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = $_REQUEST['subject'];
$number = $_REQUEST['number'];
$cmessage = $_REQUEST['message'];
$headers = "From: $from";
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "You have a message from your Bitmap Photography.";
$logo = 'img/logo.png';
$link = '#';
$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
$body .= "<table style='width: 100%;'>";
$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
$body .= "</td></tr></thead><tbody><tr>";
$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
$body .= "</tr>";
$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
$body .= "<tr><td></td></tr>";
$body .= "<tr><td colspan='2' style='border:none;'>{$cmessage}</td></tr>";
$body .= "</tbody></table>";
$body .= "</body></html>";
$send = mail($to, $subject, $body, $headers);
?>
So can you please let me know where I am doing it wrong
Change the code ( of all variable):
$number = $_REQUEST['number'];
to
$number = $_POST['number'];
And for check mail sent change:
$send = mail($to, $subject, $body, $headers);
to
if(mail($to, $subject, $body, $headers)){
echo 'done';
}else{
echo 'mail not sent';
}
<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 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;
?>
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);