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
Related
I've been trying to send mail through the contact form on my website. I've used the POST method and that has seemed to work for another one of my projects, but now I'm puzzled as to what the problem is within the code. I've literally copy-pasted the code and still seem to come across the error. Also is there a way in which the name of the sender is displayed rather than the gibberish that actually ends up showing?
//PHP CODE
<?php
function has_header_injection($str){
return preg_match("/[\r\n]", $str);
}
if(isset ($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$msg = $_POST['msg'];
if(has_header_injection($name) || has_header_injection($email)){
die();
}
if(!$fname || !$lname || !$email || $msg){
echo '<h4> All fields required!</h4>';
exit;
}
$to = 'mymail8#gmail.com'; //FOR ELUCIDATION
$subject = $fname . $lname ."sent you via your contact form";
$message = "Name: ". $fname . $lname;
$message .= "Email: $email\r\n";
$message .= "Message:\r\n$msg";
$message = wordwrap($message, 100);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: $lname <$email>\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
mail($to, $subject, $message, $headers);
}
else{
echo "failed to send mail!";
}
?>
//FORM (HTML)
<form action="mail-handler.php" class="msg" method="POST">
<div class="name">
<div class="fname">
<div>FIRST NAME</div>
<input type="text" placeholder="Enter your first name" id="fname" name="fname">
</div>
<div class="lname">
<div>LAST NAME</div>
<input type="text" placeholder="Enter your last name" id="lname" name="lname">
</div>
</div>
<div class="add-info">
<div class="mail">
<div>E-MAIL</div>
<input type="text" placeholder="Enter your email" id="mail" name="email">
</div>
<div class="phn">
<div>PHONE NUMBER</div>
<input type="text" placeholder="Enter your phone number" id="phn" name="phn">
</div>
</div>
<div class="msg-box">
<div>MESSAGE</div>
<input type="text" placeholder="Enter your Message" id="msg" name="msg" >
</div>
<button type="submit" id="sub" name="aubmit">Submit</button>
</form>
<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'm going to post my html form and my php and hopefully someone can help me make them actually work. My agenda is to make what the user inputs into the html be sent to my email, and also for them to receive an email back saying that we got their email.
I also wanted for the user to be send to a "thank you we got your email" page after hitting submit. The html is kind of long so forgive me for that.
<!-- contact --->
<div class="templatemo_caption">
<div class="clear"></div>
<p>Create any task you would like.</p>
<div class="clear">
</div>
<div align="center">
<div class="container">
<div class="row">
<div class="col-md-3">
<form action="sendmail.php" method="POST">
<form role="form">
<div class="form-group">
<input name="name" type="text" class="form-control" id="name" placeholder="Your Name" maxlength="30">
</div>
<div class="form-group">
<input name="email" type="text" class="form-control" id="email" placeholder="Your Email" maxlength="30">
</div>
<div class="form-group">
<input name="subject" type="text" class="form-control" id="subject" placeholder="Your Task" maxlength="40">
</div>
<br /> <br />
<input type="submit" value="Send Feedback" />
</div>
<div class="col-md-9">
<div class="txtarea">
<textarea name="subject" rows="10" class="form-control" id="subject"> Any specific details we need to know?</textarea>
</div>
</form>
</form>
</div>
</div>
</div>
<!--Contact End-->
AND THE PHP LOOKS LIKE THIS
<?php
/* Subject and email variables */
$emailsSubject = 'This is where you type what you subject will show up as';
$webMaster = 'chelsieoverbay#yahoo.com';
/* Gathering Data Variables - Whats in the form */
$name = $_POST ['name'];
$email = $_POST ['email'];
$subject = $_POST ['subject'];
$msg = $_POST['msg'];
/*Security*/
/* What You Want To See In The Email Place Inbetween $body = <<<EOD and EOD; */
$body = <<<EOD
<strong>Client:</strong> $name
<br />
<br />
<strong>Email:</strong> $email
<br />
<br />
<strong>Subject:</strong> $subject
<br />
<br />
______________________________________________
<br />
<br />
$msg
EOD;
/* Headers is a tag containing the users email and how you want it to display in your email */
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
/* This is what sends the email */
$success = mail($webMaster, $emailsSubject, $body, $headers);
/* Results Rendered as Html */
echo file_get_contents("studenthome.nku.edu/~overbayc1/onlinebutler/submit.html");
?>
Here's an example of how you could send two messages, one to your recipent and one to yourself.
//MESSAGE TO RECIEPENT ************************************************/
// multiple recipients
$to = $_POST["email"];
// subject
$subject = 'Your mail has been recieved';
// message
$message = '
<html>
<head>
<title>Your mail has been recieved</title>
</head>
<body style="margin: 0px;">
<p style="margin-bottom: 35px;">Dear '.$_POST["name"].',</p>
<p style="margin-bottom: 5px;">
Your email with the subject: '.$con_subject.' has been recived, you will be answered as soon as possible, possibly within the last 5 days.
</p>
<p>
- AthaxDesigns
</p>
</body>
</html>
';
// 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: '.$_POST["email"] . "\r\n";
$headers .= 'From: AthaxDesigns <noreply#athaxdesigns.com>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
//MESSAGE TO MYSELF ************************************************/
// multiple recipients
$to = "support#athaxdesigns.com";
// subject
$subject = 'You have recieved an mail from '.$_POST["name"].' Subject: '.$con_subject.' ';
// message
$message = '
<html>
<head>
<title>You have recieved an mail from '.$_POST["name"].'</title>
</head>
<body style="margin: 0px;">
<p style="margin-bottom: 35px;">Name: '.$_POST["name"].' Email: '.$_POST["email"].',</p>
<p style="margin-bottom: 5px;">
'.$_POST["subject"].'
</p>
<p style="margin-bottom: 5px;">
'.$_POST["content"].'
</p>
</body>
</html>
';
// 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: ' . $to . "\r\n";
$headers .= 'From: '.$_POST["name"].' <'.$to.'> '. "\r\n";
$headers .= 'Reply-To: ' . "\r\n" .
// Mail it
mail($to, $subject, $message, $headers);
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);