I'm building a site in wordpress and usually use the contactform7 plugin to handle forms, but am trying to learn how to set up a form without having to rely on the plugin. I've had success with this recently, but on this latest site my content is being sent to the recipient. I get an email, but none of the values from the form are being sent.
Here's my form (I know I have to also learn about validation/ sanitization, that will be another topic!):
<form action="<?php echo home_url('/'); ?>sent/" method="POST" class="col-sm-7">
<input id="name" placeholder="name:" name="name" type="text" class="form-control" required></label>
<input id="email" placeholder="email:" name="email" type="text" class="form-control" required>
<textarea id="message" placeholder="message:" name="message" class="form-control" rows="8" required></textarea>
<button id="contact-submit" type="submit" class="btn form-control">Submit</button>
</form>
When the user presses submit the page redirects to url/sent/ in that file I have this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "myemail#email.com";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
Now when I get the email in my inbox it shows up like this:
From: , ,
Message:
That's it. Is there something obvious I'm missing here? Any help is, as always, greatly appreciated.
thanks!
Added note: It seems that when the submit button is pressed and the page redirects, it's pulling a 404, though the url it's redirecting to IS correct, in fact, even if I just refresh the page without changing the url it drops the 404 and loads the page content. I guess this is why the content is not being sent, but then why is this happening?
As it turns out this problem is due to an issue with wordpress. Wordpress does not allow you to use the name="name" - this causes a 404 and the form values are not sent. I changes the name to 'form_name' and it works perfectly now.
Thanks for your help, hope this can help someone else too!
Thanks to Tom Elliot at http://www.webdevdoor.com for the help!
http://www.webdevdoor.com/wordpress/submitting-form-wordpress-redirects-404-page/
You should always check to see if the values are set
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name, $email, \nMessage: $message";
$recipient = "myemail#email.com";
$subject = "Contact Form Submission";
$mailheader = "From: $email \r\n";
if(mail($recipient, $subject, $formcontent, $mailheader)){
echo "message sent!";
}else{
die("Error!");
}
}else{
echo "email or name is not set";
}
Also try to change your button to <input type="submit"> even though it might not make a difference.
Other than that the code looks fine
Related
The PHP code sends me an email from the server, but the user input is empty or "blank". I only receive: "From: \ Email: \ Subject: \ Message: " and that's it.
How do I fix my PHP and/or HTML code to receive user input from the form? Here is my existing HTML and PHP code that doesn't send me any "user input" from the form.
PHP
HTML
Thanks to anyone who can help!!
Consider this is your form
<form action="yourpage.php" method="post">
<input type="text" name="name" placeholder="enter name" required>
<input type="submit" name="submit" placeholder="enter name">
Make sure that <form action="" is linking to correct page and method="post" should be there.
If form is there on current page i.e. in my case its yourpage.php then leave form action blank. Also i recommend adding required in end of input field e.g.
<input type="text" name="name" placeholder="enter your name" required>
as it forces user to write some value there so you dont get a empty value
Next will be your php file, just add a code in first line
if(isset($_POST['submit'])) {
above code verifies that user clicks on submit button so overall code will look like
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phoneT'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST[itypel];
$message = $_POSTUmessagefl;
$formcontent = " From:$name \n Phone: $phone \n CallBack: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "YOUREMAIL#HERE.COM";
$subject - "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href=iform.htmll
style='text-decoration:none;color:#ff0099;'> Return
Home</a>";
}
?>
I've found some similar questions to my query, but none that fully match my situation.
I had a basic mail() PHP setup for use on the contact form on my website but, turns out this is blocked on my hosting server to avoid spam/abuse.
The web host recommends using SMTP as a workaround, but the tutorial they link to doesn't seem to provide a working solution for me!
This is my HTML:
<form action="php/mail.php" method="POST">
<p class="form-field-title">Name</p>
<input class="contact-field focus-hide" type="text" id="name" name="name" required autocomplete="off">
<p class="form-field-title">Email Address</p>
<input class="contact-field focus-hide" type="email" id="email" name="email" required autocomplete="off">
<p class="form-field-title">Phone</p>
<input class="contact-field focus-hide" type="text" id="number" name="number" autocomplete="off">
<p class="form-field-title">Message</p>
<textarea class="contact-field focus-hide" id="message" name="message" data-gramm_editor="false" required autocomplete="off"></textarea>
<input class="contact-button" type="submit" value="Send Message">
</form>
And this is my original (non-SMTP) mail.php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$formcontent="Name: $name \nEmail: $email \nPhone Number: $number \nMessage: $message";
$recipient = "email#email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ../thanks.html');
exit();
?>
I have the email server details for my web hosting, but none of the SMTP solutions I've found are working for me!
Is anyone able to help me? I've seen a few things about using PEAR, but their website doesn't seem to be working...?
Thanks!
If your form is in an email, I think you can't post data (maybe I'm wrong). But you can send it to your script with method get.
<form action="php/mail.php" method="GET">
</form>
Then update your php :
<?php
$name = $_GET['name'];
$email = $_GET['email'];
$number = $_GET['number'];
$message = $_GET['message'];
$formcontent="Name: $name \nEmail: $email \nPhone Number: $number \nMessage: $message";
$recipient = "email#email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ../thanks.html');
exit();
?>
It should do the job.
Well, I have gotten the submit to work... somehow. But the email comes and it only contains my email as the body of the message (not even the email entered into the form)... no name, no phone number, no radio answers, ect.
Ok. I have finally gotten the html form to do SOMETHING. Unfortunately, when you hit "submit" it directs to a page that says this: Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Here is the PHP code:
<?php
if (!isset($_POST['submit'])) {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$telephone = $_POST['phone'];
$visitor_email = $_POST['email'];
//Validate first
if (empty($name) || empty($visitor_email)) {
echo "Name and email are mandatory!";
exit;
}
if (IsInjected($visitor_email)) {
echo "Bad email value!";
exit;
}
$email_from = 'heather#thetrinitydesign.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $message" . $to = "heather#thetrinitydesign.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
//done. redirect to thank-you page.
header('Location: thank_you.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array(
'(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if (preg_match($inject, $str)) {
return true;
} else {
return false;
}
}
?>
And the HTML. I feel like I need to have each of the questions I asked represented in the PHP file. Is this correct? And I have seen so many things preface the $POST thing that I am completely lost.
<form action="form-to-email.php" method="post" enctype="multipart/form-data" name="Info Form" id="Info Form">
<p class="form">Name:
<input name="Name" type="text" class="formbox" id="Name" size="20" />
</p>
<p class="form"> Phone:
<input name="Phone" type="text" class="formbox" id="Phone" size="12" maxlength="12" /></p>
<p class="form">Email:
<span id="sprytextfield2">
<input name="Email" type="text" class="formbox" id="Email" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span> </p>
<p class="form">
<label>Have you ever had Custom Interior Design work before?<br />
Here is the submit:
<br />
<input name="Submit" type="submit" id="button" value="Submit" />
<br />
</p>
<p class="form">
Just above the validate first comment is a $ that should not be there.
As well as all of the suggestions mentioned, I am going to hazard a guess that the internal error is because the form is posting to 'form-to-email.php', this needs to match the filename of your php file.
change this
<input name="Submit" type="submit" id="button" value="Submit" />
to
<input name="submit" type="submit" id="button" value="Submit" />
^ This should be lowercase here, see below
The code below needs to match the name above.... exactly...
if (!isset($_POST['submit'])) {
^ here!!!!
These two values need to match exactly...
$_POST['THIS'] and <input name="THIS" .../>
As Quinny has pointed out, you make this error with almost all the form inputs...
<input name="Email" .../> should be <input name="email" .../>
<input name="Phone" .../> should be <input name="phone" .../>
<input name="Name" ..../> should be <input name="name" ..../>
It looks to me like you are flying by the seat of your pants, and are just trying to get it working. Spend a little more time learn what is actually happening.
PHP Forms a great tutorial on how to get forms working.
a few things to note:
santize your $_POST data. make sure all data is safe. not just your email data.
look into coding standards, there a bunch, pick one drupals coding standards
give all form elements a name and id, not just an id, when you get into javscript later on, which you may or may not, this will be much easier.
avoid spaces in ids/names form name='Info Form' not a huge deal, could get ugly later on.
you can run the php file from command line, and get some good error details there.
turn on php error reporting Show Errors
these are just a few, not trying to be smug, trying to point you in the right direction.
EDIT:
change this:
$email_from = 'heather#thetrinitydesign.com'; //<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $message" . $to = "heather#thetrinitydesign.com"; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
to this:
$message = "Hello My Friend";
$email_from = 'heather#thetrinitydesign.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n" . "Here is the message:\n $message";
$to = $_POST['Email']; //<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to, $email_subject, $email_body, $headers);
This will send an email from heather#thetrinitydesign.com to "what ever you type in email"
with the message
"You have received a new message from the user "what ever you type in name"
"Here is the message:
Hello My Friend";
I recently got some help in another topic and was able to receive an email from my contact form, but all of the information save for the message text was excluded, and it was sent from "Apache". Is there any reason this might be happening?
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "______#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
$to ='______#gmail.com';
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've received your information, thank you"
if($send_contact){
echo "We've received your contact information, thank you";
}
else {
echo "Error, please try again";
} }
?>
<form action = "../mail.php"method="POST">
<p>Name</p> <input type="text" name="name">
<p>Company</p> <input type="text" name="company">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="phone">
<p>Message</p><textarea name="message" rows="4" cols="25"></textarea><br />
<input type="submit" value="Submit" name="submit">
</form>
Wrong variable name you have used.
Please change the last param of the mail call from $header to $mailHeader
$send_contact=mail($to,$subject,$message,$header);
to
$send_contact=mail($to,$subject,$message,$mailHeader);
Append all information what you need before sending. Use the below code,
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$messageContent = $name."\n".$company."\n".$email."\n".$phone."\n".$message."\n";
$send_contact=mail($to,$subject,$messageContent,$mailHeader);
Edit this
send_contact=mail($to,$subject,$message,$header);
to
send_contact=mail($to,$subject,$message,$mailheader);
and you can also send like this
send_contact = main($to,$subject,$formcontact,$mailheader);
if you want...and
sending mail in php is not a one-step process. mail() returns true/false, but even if it returns true, it doesn't mean the message is going to be sent. all mail() does is add the message to the queue(using sendmail or whatever you set in php.ini)
there is no reliable way to check if the message has been sent in php. you will have to look through the mail server logs.
I have a simple php form for contact purposes. However, it won't send the email or go to the correct page after submit. It redirects to the mail.php instead.
My contact form named contact.php is as follows:
<form id="contact-form" action="mail.php" method="POST">
<fieldset>
<label><span class="text-form">Your Name:</span> <input type="text" name="name"></label>
<label><span class="text-form">Your Email:</span><input type="text" name="email"></label>
<label><span class="text-form">Your Contact No:</span><input type="text" name="contact"></label>
<div class="wrapper">
<div class="text-form">Your Message:</div>
<textarea name="message" rows="6" cols="25"></textarea><br />
<div class="clear">
</div>
<div class="buttons">
<a class="button" href="#"><input class="button" type="submit" value="Send"></a>
<a class="button" href="#"><input class="button" type="reset" value="Clear"></a>
</div>
</fieldset>
</form>
And the php code named mail.php is as follows:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location:contact.php");
?>
What am i doing wrong. It just wont send the message to email or redirect to the correct page??
Your syntax is ever so slightly wrong. &contact on line 5 should be $contact. I assume you're on a production server, so error reporting would be disabled and you wouldn't get any warnings.
Try using # before mail function
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
#mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("location: contact.php");
?>
this will let you pass even if mail function produce any issue. Also please check if there are any other header are not sent using http://php.net/manual/en/function.headers-sent.php function.
I would strongly suggest to use PHPMailer or some other class to send any kind of email.
Please check the contact.php file whether some redirection is happening from contact.php to mail.php
I think there may be an issue sending mail from your server. The mail.php file is probably tripping up on the mail function and not doing the redirect.
Create a new file with just the php mail function in there (with your email address and a test message) and browse it from your web browser, does it send?
Try to use ob_start() at the start of script, and exit(); after header("Location: contact.php");
smth like this ...
<?php
ob_start();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
&contact = $_POST['contact'];
$formcontent="From: $name \n Message: $message";
$recipient = "info#whatever.co.za";
$subject = "Contact form message";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header("Location: contact.php");
exit();
?>
Change the mail.php to the following:
<?php
mail("info#whatever.co.za", "Contact form message", $_POST['message'], "From: ".$_POST['name']." <".$_POST['email'].">");
header("Location: contact.php");
?>