PHP email script not sending all form fields - php

IF (I Started Receiving Spam Bot Forms)
THEN (I Implemented New PHP Email script using a basic Honey Pot Method)
$ERROR (New PHP is not sending ALL the forms fields. Upon sending the form, my email is only receiving the, textarea id="message", field)
$LOG_FILE (My previous PHP script implemented a dynamic catch-all solution for form fields)
$FAILED_SOLUTION (Conversely I attempted to add the individual, $phone & $address fields manually on lines #6 7 & 14 of the PHP but am still only receiving the, textarea id="message", field)
$NOTES (I am self taught & typically only deal with PHP on a need to know basis. Please try to keep it simple and include a step-by-step explanation. Feel free to suggest any "best practices" i may have overlooked unrelated to my problem!)
$QUESTION = "Can someone show me how to call the other form fields in the PHP script to send to my email?"
$SUCCESS = "Thanks in Advance For Any Help That Maybe Given!";
PHP:
<?php
if($_POST){
$to = 'your-email-here#gmail.com';
$subject = 'Contact Form Submission';
$name = $_POST['name'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$email = $_POST['email'];
$message = $_POST['message'];
$robotest = $_POST['robotest'];
if($robotest)
$error = "Spam Protection Enabled";
else{
if($name && $phone && $address && $email && $message){
$header = "From: $name <$email>";
if(mail($to, $subject, $message,$header))
$success = "Your message was sent!";
else
$error = "Error_36 there was a problem sending the e-mail.";
}else
$error = "Error_09 All fields are required.";
}
if($error)
echo '<div class="msg error">'.$error.'</div>';
elseif($success)
echo '<div class="msg success">'.$success.'</div>';
}
?>
HTML FORM:
<form method="post" action="Form_Email.php">
<input type="text" id="name" name="name" placeholder="name" required>
<input type="text" id="phone" name="phone" placeholder="phone" required>
<input type="text" id="address" name="address" placeholder="address" required>
<input type="text" id="email" name="email" placeholder="email" required>
<textarea id="message" name="message" placeholder="message" required> </textarea>
<p class="robotic">
<input name="robotest" type="text" id="robotest" class="robotest" autocomplete="off"/>
</p>
<input type="submit" id="SEND" value="Submit">
</form>

Your message contains only $_POST['message'] for now. If you want to append other values, use concatenation on your $message variable.
$message .= ($name . $phone . $address . $etc)
Notice: A $foo .= $bar construction stands for $foo = $foo . $bar.
Do not forget about whitesigns such as spaces or new lines wherever you want. Simply concatenate ' ' or "\n".
After that, just send a mail using your $message as message.

Related

PHP contact form submits but does not send mail

I recently made a website using HTML, CSS, and JS. Since I don't know PHP, I am stuck at building the contact form where it is vital on the website. I learned a bit from YouTube tutorials and have the following HTML & PHP code:
<div class="contact_form">
<form action="/action_page.php">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$mailTo = 'example#something.in';
$headers = 'From: '.$mailFrom;
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($mailTo, $headers, $txt);
header("Location: index.html?mailsent");
}
?>
Why do I need the MIME and content-type headers at the bottom as that bit I added from another tutorial.
When I use the form and try sending the message, I get the "?mailsent" after the URL but I receive no email which is a professional plan by GoDaddy.
They are also hosting my website. I contacted them to know whether the server allows me to make my contact form with the plan I have and they said yes. So, I must be missing something important here.
refer to the documentation of mail function
there are 3 required parameter : email destination (to), subject and the message, and two additional options are: headers and parameters.
your code didnt respect that, because you missing to add the subjuct as parameter.
and you get ?mailsent because you use header("Location: index.html?mailsent") without any test if the email send successfully or not.
i suggest you to replace the last two lines of your php code with this
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
// error
}
EDIT:
you can get the error message with error_get_last() function.
thanks to https://stackoverflow.com/a/20203870/195835
$subject = "some subject"; // you can replace it with $subject = $_POST["subject"]
$result = mail($mailTo, $subject , $txt,$headers);
if ($result){
// mail send successfully
header("Location: index.html?mailsent");
} else {
print_r(error_get_last());
}
You are missing the form action, So PHP doesn't know what to do with your variable data.Try adding method="post" inside <form> tag. Like this
<div class="contact_form">
<form action="/action_page.php" method="post">
<input type="text" id="name" name="name" placeholder="Name*">
<input class="contact_even" type="text" id="email" name="email" placeholder="Email id*">
<input type="text" id="phone" name="phone" placeholder="Phone No.">
<input class="contact_even" type="text" id="city" name="city" placeholder="City">
<textarea id="subject" name="subject" placeholder="How Can We Help You?"></textarea>
<input type="submit" value="Submit">
</form>
</div>
And also. If you use your computer as localhost(using xampp , wamp, or something without a hosting service) You have to make some changes to the config files.
Also try this modified php code
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$message = $_POST['message'];
$title = "replace this";
$mailTo = 'support#udichi.in';
$txt = $name.'('.$phone.') from '.$city.' says:\n\n'.$message;
$headers = 'From: '.$mailFrom . PHP_EOL .'Reply-To:' .$mailFrom . PHP_EOL . 'X-Mailer: PHP/' . phpversion();
mail($mailTo,$title,$txt,$headers);
header("Location: index.html?mailsent");
}
?>

Undefined Index in PHP using textarea [duplicate]

This question already has answers here:
Why cant I access $_POST variable with a hyphen/dash in its key if passing key as variable?
(2 answers)
Closed 6 years ago.
I've seen dozens of posts about this issue and it basically comes down to a variable not being declared or given a value. However I'm 100% sure it's the same and declared.
I have a basic contact form in HTML and I want it to send me and e-mail when someone hits the submit button. I am debugging the code as well to see what the problem is. The only issue it can find is that there is an Undefined Index which belongs to my text area.
I know that the name of the textarea must be the same as the name on my $_POST in the PHP. Please take a look at the two sections of code and tell me if you can see why it wouldn't be fetching the information from my textarea. The name is message-area.
HTML
<form action="mail.php" method="post" name=contact-me-form >
<label name="firstname secondname">Name: * </label><br>
<input class="half-box" type="text" name="firstname" required >
<input class="half-box" type="text" name="secondname" required ><br>
<p class="first-name">First Name</p>
<p class="second-name">Last Name</p><br>
<label name="email">Email Address: * </label><br>
<input class="full-box" type="email" name="email" spellcheck="false" required><br>
<label name="subject">Subject: </label><br>
<input class="full-box" type="text" name="subject"><br>
<label name="message">Message: * </label><br>
<textarea name="message-area" form="contact-me-form" type="text" placeholder="Please enter your message"></textarea>
<button name="submit" type="submit" value="Submit">Submit</button>
</form>
PHP
<?PHP
$to = "";
$from = "";
$first_name = '';
$last_name = '';
$subject = '';
$message = null;
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
if(isset($_POST['submit'])){
$to = 'email#test.com';
$from = $_POST['email'];
$first_name = $_POST['firstname'];
$last_name = $_POST['secondname'];
$subject = $_POST['subject'];
$message = $_POST["message-area"];
if($message == null){echo "no message detected";}
$headers = "From: " . $from;
$headers = "From:" . $to;
mail($to,$subject,$message,$headers);
}
?>
As you can see the names are identical yet when I submit the data it comes up displaying the following.
int(8) string(29) "Undefined index: message-area" string(58) "/hermes/bosnaweb25a/b2294/ followed by a bit more information and my error gets displayed: ["message"]=> NULL } no message detected.
I honestly have no idea why this isn't being picked up, can anyone with more experience highlight my mistake?
EDIT 1
This is not to do with dashes/hyphens as I've edited my code as you can see below. It's also important to note that if I change this to raw text it still doesn't work, still acts as if there is no data from the textarea.
HTML
<form action="mail.php" method="post" id=contact-me-form >
<label name="firstname secondname">Name: * </label><br>
<input class="half-box" type="text" name="firstname" required >
<input class="half-box" type="text" name="secondname" required ><br>
<p class="first-name">First Name</p>
<p class="second-name">Last Name</p><br>
<label name="email">Email Address: * </label><br>
<input class="full-box" type="email" name="email" spellcheck="false" required><br>
<label name="subject">Subject: </label><br>
<input class="full-box" type="text" name="subject"><br>
<label name="message">Message: * </label><br>
<textarea name="messagearea" type="text" placeholder="Please enter your message"></textarea>
<button name="submit" type="submit" value="Submit">Submit</button>
</form>
PHP
<?PHP
$to = "";
$from = "";
$first_name = '';
$last_name = '';
$subject = '';
$message = null;
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
if(isset($_POST['submit'])){
$to = 'harry.brockley#hotmail.co.uk';
$from = $_POST['email'];
$first_name = $_POST['firstname'];
$last_name = $_POST['secondname'];
$subject = $_POST['subject'];
$message = $_POST["messagearea"];
if($message == null){echo "no message detected";}
$headers = "From: " . $from;
$headers = "From:" . $to;
mail($to,$subject,$message,$headers);
}
?>
EDIT 2
Tested it with a hard coded value works so it has to be the variable name. It's just strange that it only happens on the textarea.
Here is your solution just replace below line with your text-area code.
<textarea name="message-area" placeholder="Please enter your message"></textarea>
here it is no need form="contact-me-form" text.
Thank you for your help and I found out the problem and it seems to work. I haven't actually received the e-mail yet (Possibly terrible Vietnamese Internet).
The problem was that my variables defined as $variable = "" were not set as null as I thought. I had to go through and assign = null to all of my variables and it seemed to bypass the error.
You seem to have a random form="" attribute in the <textarea></textarea>
If you change it to the following it should work.
<textarea name="message-area" type="text" placeholder="Please enter your message"></textarea>
With respect to the check on $message not working with ($message == null), you should use empty() to check it as that will catch NULL and "".
if (empty($message)) {
echo "no message detected";
}

Incorrect PHP/Contact Form Failing to function correctly

So, I'm currently trying to code a contact form with the little knowledge of PHP that I have, and needless to say, it's failing miserably. I've searched the rest of the interwebs far and wide, and I haven't been able to get any answers solely by gleaning information off of forums.
Thus, I've come to you guys for help. Here is the form code:
<form method="post" action="contactphp.php">
<label>First Name</label>
<input name="firstname" placeholder="Type Here">
<label>Last Name</label>
<input name="lastname" placeholder="Type Here">
<label>Company</label>
<input name="company" placeholder="Type Here">
<label>Project</label>
<input name="project" placeholder="Type Here">
<label>City</label>
<input name="city" placeholder="Type Here">
<label>Country</label>
<select id="countries" name="country">
</select>
<label>Email</label>
<input name="email" placeholder="Type Here">
<label>Phone</label>
<input name="phone" placeholder="Type Here">
<label>Subject</label>
<input name="subject" placeholder="Type Here">
<label>Inquiry</label>
<textarea name="inquiry" placeholder="Please voice your inquiry(ies) here."></textarea>
<button name="submit" type="submit" value="submit">SUBMIT</button>
</form>
I understand needs s; I've omitted them because they list all of the countries in the world, which is a lot of pointless info regarding this question.
Here is the PHP code later down the page:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$company = $_POST['company'];
$project = $_POST['project'];
$city = $_POST['city'];
$country = $_POST['country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject= $_POST['subject'];
$inquiry = $_POST['inquiry'];
$from = "From: $firstname $lastname representing $company";
$to = "dummyemail#gmail.com";
$subject= "INQUIRY, $subject";
$body = "From: $firstname $lastname\n Company: $company\n Project: $project\n City: $city, Country: $country\n Email: $email, Phone: $phone\n Message: \n $inquiry";
if ($_POST['submit']) {
if ($firstname === "" || $lastname === "" || $city === "" || $country === "NIL" || $email === "" || $phone === "" || $subject === "" || $inquiry === ""){
return false;
}
else{
if (mail ($to, $subject, $body, $from)){
<p> Your message has been sent successfully!</p>
}
else{
<p>Your message has failed to send; please check all fields of the form to make sure it is filled out correctly.</p>
}
}
}
?>
Again, if there is some blatantly stupid mistake, please forgive it; I'm only scraping together the syntax I know to form this script.
Also, if you answerers out there need any more info, please feel free to post about it.
Other info:
The server is currently outfitted with PHP 5.4.27, and is in working order (I downloaded a test page and tried using it; it logged all of the necessary info).
We're on a Linux box because Windows is SUPPOSEDLY less than adequate with file-permissions and whatnot (based on what I'm told).
Thanks for all of the help guys.
Is your mail just not sending or are your $_POST[] variables not being populated? Either way, here are a couple possible solutions/debugging options:
First, Instead of checking the values via if($firstname === '') etc... I would do it as you are declaring the variables like:
if(!isset($_POST['firstname'])) {
echo "Please enter your first name";
} else {
$firstname = $_POST['firstname'];
}
In your question, you said the PHP code you included is further down the page... your form action attribute is set to 'contactphp.php' ... if your PHP code is on the same page try setting the action attribute to <?php echo $_SERVER['PHP_SELF']; ?>
As mentioned in a comment to the question, try using var_dump($_POST); to see what is being gathered from the $_POST[] submit.
Also, set PHP to display any errors by adding ini_set('display_errors',1); to the top of your document.
You could also try sending the mail via SMTP instead of PHPs mail(); function.

I am trying to get my PHP and form working correctly

Hey I have a simple coding issue and I am hoping someone can spot the error of my ways.
I have a contact page http://mattmcdougall.ca/contact
this page has a form that is connected to a form PHP.
it is half working, I receive the email with ONLY the name subject and recipient (me). I do not get the other information in the form. (phone number and content do not get placed in the email) ALSO, I want the website to bring the person filling out the form to http://mattmcdougall.ca/contactdone currently it goes to a blank form.php page
here is the form code and PHP code, please feel free to clean up PHP as I used a template I found online.
<form action="form.php" method="post" class="formtext">
<input name="Name" type="text" value="enter name" maxlength="60"><br>
<input name="Email" value="enter email" type="text"><br>
<input name="Phone Number" value="enter phone#"type="text"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br><input name="Submit" type="submit" value="Submit"><form
and the PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Matt McDougall Photography</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<?php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n";
mail($recipient, $subject, $mail_body, $phone, $header); //mail command :)
if(mail)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
?>
</BODY>
</HTML>
You're going to need to change the name of your form elements to match the params that you're expecting to receive via $_POST.
Right now you have (for example):
<input name="Phone Number" value="enter phone#"type="text"><br><br>
But you're going to need to name it:
<input name="phone" value="enter phone#"type="text"><br><br>
The name attribute is what is going to be submitted via the form to your PHP script. You need to use the same name on both sides.
Also what is the $phone variable in your mail function call? Those areas are reserved for additional headers and not just $phone. You'll need to concatenate the phone number into the $mail_body like so (with additional formatting of course):
$mail_body = $mail_body . " " . $phone;
This is the correct signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Edit: In addition to all of this, your $header is also wrong as it's trying to use the $Name variable which does not exist. The $name variable does exist.
I think this below should solve your problems.
Form:
<form action="form.php" method="post" class="formtext">
<input type="text" name="name" placeholder="Enter Name" maxlength="60"><br>
<input type="text" name="email" placeholder="Enter Email"><br>
<input type="text" name="phone" placeholder="Enter Phone #"><br><br>
<textarea name="interest" cols="60" rows="10" placeholder="Interest?"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
form.php
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$interest = $_POST["interest"];
$to = "matt#mattmcdougall.ca";
$subject = "Interested Client";
$headers = "From: {$name} <{$email}>\r\n";
$mail_body = <<<EMAIL
Name: {$name}
Phone: {$phone}
Message:
{$interest}
EMAIL;
$mail_success = mail($to, $subject, $mail_body, $headers);
if($mail_success) {
echo "http://mattmcdougall.ca/contactdone.html"; // are you trying to redirect to here?
} else {
echo "Sorry, something went wonky! Please try again!";
}
?>
HTTP POST variables are case sensitive, and the names of the input fields do not match that of those in PHP. <input type="text" name="datanamehere" /> with $_POST["datanamehere"]. Like what other answers said:
HTML:
<form action="form.php" method="post" class="formtext">
<input name="name" placeholder="Full name" type="text" maxlength="60"><br>
<input name="email" placeholder="Email" type="email"><br>
<input name="phone" placeholder="Phone number" type="tel"><br><br>
<textarea name="interest" placeholder="Interested in?" cols="60" rows="10"></textarea><br>
<input type="submit" value="Submit">
</form>
Notice I changed the value to placeholder, and used type="email" and type="tel".
form.php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $name . " <" . $email . ">\r\n";
if (mail($recipient, $subject, $mail_body, $header)) //mail command :)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
As they said, mail() is a function returning a boolean. Wrap it around if to check for success.
Your element names do not match the ones in the PHP code and your form is not properly closed using .
<form action="form.php" method="post" class="formtext">
<input name="name" type="text" value="enter name" maxlength="60"><br>
<input name="email" type="text" value="enter email"><br>
<input name="phone" type="text" value="enter phone#"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br>
<input name="submit" type="submit" value="Submit">
</form>
mail is a function, so checking it in the 'if' by just writing mail isn't going to work.
try this instead
if(mail()):
// go to contactdone.html
header('Location: http://www.google.com');
exit();
else:
echo "Sorry Something Went Wonky, Please Try Again!";
endif;
Couple of things you can try,
if(mail)'http://mattmcdougall.ca/contactdone.html' is not going to do
anything, try using Header('Location
http://mattmcdougall.ca/contactdone.html')
Whenever you use $_POST the parameter name inside the [''] need to
match your input names (Example: <input name="interest" type="text">
would be $_POST['interest'])
If you want to show text inside an input use placeholder="enter
phone#" instead of value.

PHP Form Submit Button Unclickable

SOLVED - permissions
I want to walk through my debug process so that it might help anyone else working through the same thing... 1) I wiped both pages and replaced with the code that I knew worked. 2) I then changed the form piece by piece until I got it how i wanted and continued testing 3) I then copied the current php file completely and redirected my form to it. 4) it failed... I changed the permissions to 655 and wallah it worked. Now I can go about hacking about the PHP code to get what I want. thanks for all of the suggestions, you definitely led me down the road to my solution
SOLVED
I have two separate intake forms on a site. Intake form 1 works perfectly. I takes, name, email and comment and sends it through a sendmail script.
I also wanted an intake form for lead capture to track those that want to access the demo videos so I modified the code from the form (for the new page) and then created an additional php file called videoform.php - which is basically just a modified version of my sendmail.php file.
When I fill out the form it does nothing when I click on submit. It validates, as it not let you enter a null value in any of the fields but I am not sure what I am missing. Is it something simple (I am by no means PHP reliable) or can I simply not do that?
Here is the form and the php:
<div class="message"></div>
<form action="./php/videoform.php" method="POST" id="contact-form">
<p class="column one-half">
<input name="name" type="text" placeholder="Your Name" required>
</p>
<p class="column one-half">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="column one-half">
<input name="phone" type="text" placeholder="Your Phone" required>
</p>
<p>
<input name="submit" type="submit" value="Submit">
</p>
</form>
</div>
This is the PHP
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$content = $_POST['content'];
$subject = "You've been contacted by $name";
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
header("Location: /video-form.html");
exit;
}
?>
I am comfortable with a number of coding formats but I am so weak when it comes to PHP. Any insight would be both appreciated and get me on the road to understanding PHP better.
Working scripts for comparison
Form
Send us a message
<p class="column one-half last">
<input name="email" type="email" placeholder="Your Email" required>
</p>
<p class="clear">
<textarea name="comment" placeholder="Your Message" cols="5" rows="3" required></textarea>
</p>
<p>
<input name="submit" type="submit" value="Comment">
</p>
</form>
</div>
PHP sendmail.php file
<?php if(!$_POST) exit;
$to = "xxxxx#example.com";
$email = $_POST['email'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$subject = "You've been contacted by $name";
$content = "$name sent you a message from your enquiry form:\r\n\n";
$content .= "Contact Reason: $comment \n\nEmail: $email \n\n";
if(#mail($to, $subject, $content, "Reply-To: $email \r\n")) {
echo "<h5 class='success'>Message Sent</h5>";
echo "<br/><p class='success'>Thank you <strong>$name</strong>, your message has been submitted and someone will contact you shortly.</p>";
}else{
echo "<h5 class='failure'>Sorry, Try again Later.</h5>";
}?>
From your php:
//...
$content = "$name filled out a request to view the online videos:\r\n\n";
$content .= "Phone: $phone \n\nEmail: $email \n\n";
if ($success) {
header("Location: /videos.html");
exit;
} else {
//...
You never define $success. Since it doesn't have a value, if ($success) fails, and it always enters the else portion of the statement. It looks like you're missing a line that's something like $success = mail($to, $subject, $content);

Categories