php form bug. Doesn't send my email [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have tried to resolve the issue myself (and some websites and questions here in Stockoverflow), but without success.
My problem is I'm trying to make my form actually send an email to the specified address down there, but it is absolutely not working. Any idea where my code is bugging?
here is a piece of it, can you see any mistake?
Thanks!
<?php
if(isset($_POST['submit'])){
$to = "email#gmail.com"; // this is my Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
}
?>
<body id="getademo">
<div class="titlepage">
<h1 id="introdemo">GET IN TOUCH</h1>
<p id="mikshahf">We will get back to you soon!</p>
</div>
<div id="form1">
<div id="formform">
<form>
<p>
<input name="name" type="text" class="feedback-input" placeholder="Name" id="name" />
</p>
<p>
<input name="email" type="text" class="feedback-input" id="email" placeholder="Email" />
</p>
<p>
<textarea name="message" class="feedback-input" id="comment" placeholder="Message"></textarea>
</p>
<div class="submit">
<input name="submit" type="submit" value="SEND" id="button-blue"/>
</div>
</form>
</div>
</div>
</body>

As I stated in comments; <form> alone defaults to a GET method when POST isn't specifically implied.
Therefore, you need to add it.
<form method="post">
Error reporting would not have signaled an error. It actually failed on you silently.

Related

php email from html form

So I've had some issues trying to get my php code to work. I found a form php handler which I want to send the email on the back end for the users.
<?php
if(isset($_POST['submit'])){
ob_start();
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];//Sender first name//
$last_name = $_POST['last_name'];//sender last name//
$subject = "Contact Request.";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$to = "-myemail#outlook.com-"; // this is your Email address
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
header('Location: http://-mysite-/thankyou.html');
exit();
}
?>
Here is the HTML form info:
<form action="php/email.php" method="post" name = "email form" enctype="multipart/form-data" class="container" align="center">
<label for="firstname"></label>
<strong>*</strong><input type="text" placeholder="Enter First Name" name="first_name" required>
<br>
<label for="lastname"></label>
<strong>*</strong><input type="text" placeholder="Enter Last Name" name="last_name" required>
<br>
<label for="email"></label>
<strong>*</strong><input type="text" placeholder="Enter Email" name="email" required>
<br>
<label for="phoneno"></label>
<input type="phoneno" placeholder="Enter Phone Number" name="phone_number">
<br>
<label for="interested"></label>
<t> Brief description of project:</t><br>
<textarea style="width:100% height:60%" align="center" name="message">
</textarea><br>
<button type="submit" class="btn" value="Send Form"><strong>Submit</strong></button>
</form>
I can't figure out why the page on submit does redirect to the email.php but doesn't do anything from there. Do I need to contact the site support team to restart my php services? I've been smacking my head against the keyboard for the last 2 days as to why this isn't working.
Because you haven't field in your form with name="submit" and trying to get its value in PHP: if(isset($_POST['submit'])).
Bonus tip: don't use name submit for any field of the form, cause some browsers getting fooled with this. Instead use formsubmit, submitted or anything else.
just add this field in your form
<input type="hidden" name="submitted" value="1">
andchange condition in PHP to
if(isset($_POST['submitted']) && intval($_POST['submitted']) == 1)...

PHP contact form continues to return error even though information is provided

I am working on a contact form but every time I try actually running it on my website I keep getting the error message "error with sending email". I'm not trying to do anything complex. I just want it to send the forms to my email. Thank you for helping. Also sorry about any bad formatting as this is my first stack overflow post.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['Name'];
$email = $_POST['Email'];
$subject = $_POST['Subject'];
$query = $_POST['Message'];
$email_from = $name.'<'.$email.'>';
$to="email#website.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email:
$email
<br>
Subject:
$subject
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:https://website.com/messagesuccess");
exit();
}
else {
echo "error with sending email";
exit();
}
?>
HTML:
<div style="margin-top:48px">
<form action="action.php" target="_blank" method="POST">
<p><input class="w3-input w3-border" type="text" placeholder="Name" required name="Name" id="Name"></p>
<p><input class="w3-input w3-border" type="email" placeholder="Email" required name="Email" id="Email"></p>
<p><input class="w3-input w3-border" type="text" placeholder="Subject" required name="Subject" id="Subject"></p>
<p><input class="w3-input w3-border" type="text" placeholder="Message" required name="Message" id="Message"></p>
<p>
<button class="w3-button w3-black" type="submit">
<i class="fa fa-paper-plane"></i> Send Message
</button>
</p>
</form>
</div>
I can't figure out what I am doing wrong. I keep getting the php error message yet all the fields seem to be in place? What am I missing?? Also huge apology since this is my first ever stackoverlfow post and I am a total PHP and stack overflow question asking noob.

Why isn't my php form working, i don't get the email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I have this php form:
<form method="post" action="mailer.php" class="contact-form-map">
<div class="row">
<input type="text" name="name" id="name" placeholder="Your Name" required>
</div>
<div class="row">
<input type="number" name="number" id="number" placeholder="Contact Number" required>
</div>
<div class="row">
<textarea type="text" name="message" id="message" placeholder="Message" rows="6" cols="33" required></textarea>
</div>
<div class="row">
<input type="submit" class="map-button" value="Send">
</div>
</form>
and this is my mailer.php
<?php
if(isset($_POST['submit'])){
$to = "lala.lal#gmail.com"; // info#computerrepairscroydon.co.uk
$name = $_POST['name'];
$number = $_POST['number'];
$subject = "Form submission - Contact";
$message = $name . " " . " wrote the following:" . "\n\n" . $_POST['message'] . "\n\n" . " Contact number: " . $number ;
$headers = "From:" . $name;
mail($to,$subject,$message,$headers);
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
header("Location: index.html?success=1#form");
}
?>
When I enter information into de form and click send nothing happens, but the email isn't send, i don't get it on my gmail.
Why is that?
You forgot to write name="submit" to the submit input
<input type="submit" class="map-button" value="Send" name="submit">
that's why the code is "jumping" the if isset $_POST["submit"]
You need to add name="submit" to your submit button.
<input type="submit" class="map-button" value="Send" name="submit">

Not receiving email with PHP form

I'm trying to make a simple html form with php to send messages to a specific email. A friend sent me this form but since i'm very noobish in PHP i can't seem to figure out why i'm not receiving any emails...
This is my HTML CODE:
<div class="form">
<form id="contactform" action="mailer.php" method="post">
<p class="contact">NOME</p>
<input id="name" name="name" placeholder="Primeiro e Ășltimo nome" required type="text">
<p class="contact">EMAIL</p>
<input id="email" name="email" placeholder="exemplo#domĂ­nio.com" required type="email">
<p class="contact">MENSAGEM</p>
<textarea rows="6" required placeholder="Escreva aqui a sua mensagem"></textarea>
<br />
<input class="buttom" name="submit" id="submit" tabindex="5" value="ENVIAR" type="submit">
</form>
</div>
And this is my PHP:
<?php
$to = 'MYEMAIL#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: '.$_POST['email'].'' . "\r\n" .
'Reply-To: MYEMAIL#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $name, $subject, $message, "From: MYEMAIL#gmail.com\n");
?>
Any help, please?
Your form is wrong:
<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
From: <input type="text" name="from"><br>
Subject: <input type="text" name="subject"><br>
Message: <textarea rows="10" cols="40" name="message"></textarea><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["from"]))
{
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$message = $_POST["message"];
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("webmaster#example.com",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
}
}
?>
$name is not a parameter in the mail function. If you are trying to add the persons name somewhere, add it to the subject, message or define it as an additional parameter. Try taking $name out and see if it works without it.
PHP mail function

PHP mail form - only one variable not submitting

I have set up a PHP mail form set up that only correctly outputs some of the variables entered in the form. It DOES mail the $name and $email variables, but not the $message variable.
The php to send the form is here:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" ){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
//sending email
require_once("siteIncludes/class.phpmailer.php");
$mail = new PHPMailer();
$email_body = "";
$email_body = $email_body . "Name: " . $name . $message . "<br />";
$email_body = $email_body . "Email: " . $email . "<br />";
$email_body = $email_body . "Message: " . $message;
$mail->SetFrom("$email,$name");
$address = "foo#bar.com";
$mail->AddAddress($address);
$mail->Subject = "Form Submission | ".$name;
$mail->MsgHTML($email_body);
if(!$mail->Send() ){
echo 'There was a problem sending the email: '.$mail->ErrorInfo;
exit();
}
header("Location: myContact.php?status=thanks");
exit();
};
?>
And the HTML that sets up the form is here:
<div id="contactFormWrap" class="span6 offset3">
<form method="post" action="myContact.php" id="contactForm">
<div>
<label for="name">Please leave your name</label>
<input type="text" name="name" id="name" value="" class="required" />
</div>
<div>
<label for="email">and your email</label>
<input type="text" name="email" id="email" value="" class="required email" />
</div>
<div>
<label for="subject">What is your message about?</label>
<input type="text" name="subject" id="subject" value="" class="required" />
</div> -->
<div>
<label for="message">and your message</label>
<textarea name="message" id="message" value="" rows="10" class="required"></textarea>
</div>
<div id="messageButtons">
<input type="submit" value="Submit" name="contactSubmit" id="contactSubmit" class="sendEmail btn" />
</div>
</form>
</div>
I hope that was enough information. Does anyone know why the $message variable isn't being output to the submitted email?
thanks
I think this is your problem:
value=""
The <textarea> tag does not have a value attribute, however different browsers have different ways of handling invalid code, so whatever browser you are using must be using the value found in this invalid attribute instead of what you type in the actual text box.
Just do:
<textarea name="message" id="message" rows="10" class="required"></textarea>

Categories