Php contact form coding issue - php

I am having trouble with the contact form that I've created for my website. I am not a PHP expert but I thought a proper contact form would be more professional than a simple href mailto link.
I managed to get the email, it tells me the sender but there is no subject and is all just blank without text. Also I keep receiving 2 emails everyday from no sender.
This is what I done in PHP in the page named contact.php.
I hope you can help:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form1_services = $_POST['form1_services'];
$msg = $_POST['msg'];
$formcontent="From: $name \n Message: $message";
$recipient = "dandrea.alessandro81#gmail.com";
$subject = "Customer Inquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Keep in touch soon!" . " -" . "<a href='index.html' style='text-decoration:none;font-family: 'gooddogregular';color:#009999;'> Return Home</a>";
?>
And this is the actual form in HTML:
<form action="contact.php" method="post">
<fieldset>
<legend></legend>
<div class="controlgroup">
<label for="form1_name">Name *</label>
<input type="text" placeholder="Enter your full name*" name="name" value>
</div>
<div class="controlgroup">
<label for="form1_email">Email *</label>
<input type="text" placeholder="Enter a valid email address*" name="email" value>
</div>
<div class="controlgroup">
<label for="form1_services">Services Required</label>
<select id="form1_services" name="services">
<option value="Website Design"> Website Design (from scratch) </option>
<option value="Resposive Design"> Responsive Design </option>
<option value="Customize a Site"> Customize a Site </option>
<option value="Quotation"> Quotation </option>
</select>
</div>
<div class="controlgroup">
<label>Project Info*</label>
<textarea placeholder="Ciao Alessandro, I am contacting you because...*" id="msg" name="msg" required aria-required="true"></textarea>
</div>
<input type="submit" name="submit_btn" id="send" value="Hit me up!" class="wow rubberBand animated" data-wow-delay="2s">
</fieldset>
</form>
Thanks a lot in advance.
Alessandro

Taking a look at your PHP, you're collecting a number of fields, and then not actually using them. Try this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$form1_services = $_POST['services'];
$msg = $_POST['msg'];
$message = 'From: ' . $name . ' <' . $email . '>' . "\n";
$message .= 'Service: ' . $form1_services . "\n";
$message .= 'Message: ' . "\n";
$message .= $msg;
$recipient = "dandrea.alessandro81#gmail.com";
$subject = "Customer Inquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $message, $mailheader) or die("Error!");
echo "Thank You! Keep in touch soon!" . " -" . "<a href='index.html' style='text-decoration:none;font-family: 'gooddogregular';color:#009999;'> Return Home</a>";
Keep in mind that at this point, you're not actually validating any of this information, so you can't be sure that the email address or the name is actually valid at all, but this should at least show you what is getting posted.

Try adding some headers:
$mailheader = "From: $email \r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Related

Difficulty connecting a contact form using PHP

I've tried multiple different ways to set up a contact form and connect it to an email address using PHP. This is my first time using PHP, however I know I've written the .php form correctly.
Whenever I send the form, I get redirected to an "Error 405" page.
I've Googled for a few hours, and some websites give confusing instructions about downloading PHP (which I have tried about 5 times with no success), others say that VS Code on Mac should already be able to use PHP.
I'll leave my HTML and PHP forms below (I've tried two different PHP forms with no success).
It is implied that this should be a very easy process, simply connect the HTML to PHP via "action: mail.php" attribute in the form tag, and that should be everything. Yet I have scoured the internet and have had no luck so far... Any help would be appreciated!
(I'll leave the code below, first is HTML and second/third are the two different PHP forms I have tried).
The link to my Github is here as well: "https://github.com/cjmaret/abt-website"
<form class="contact-form__form-section" action="mail.php" method="POST" accept-charset="UTF-8">
<h2 class="contact-form__form-section__title">Get In Touch</h2>
<p class="contact-form__form-section__subtitle">Fill out the form or call ###-###-#### to request an
estimate or more information. We look forward to assisting you!</p>
<fieldset class="fieldset">
<select class="input input_select" id="dropdown" name="dropdown" required>
<option value>Subject*</option>
<option value="accounts-receivable">Accounts Receievable</option>
<option value="accounts-payable">Accounts Payable</option>
<option value="human-resources">Human Resources</option>
<option value="quotes-estimates">Quotes/Estimates</option>
<option value="w9-certificates">W9/Certificates of Insurance</option>
<option value="careers">Careers</option>
<option value="general-feedback">General Feedback</option>
<option value="request-service">Request Service</option>
</select>
<div class="input">
<input class="text-input" type="text" name="name" placeholder="Name*" required>
</div>
<div class="input">
<input class="text-input" type="text" name="company" placeholder="Company">
</div>
<div class="input">
<input class="text-input" type="email" name="email" placeholder="Email*" required>
</div>
<div class="input">
<input class="text-input" type="tel" name="phone" placeholder="Phone">
</div>
<div class="input">
<textarea class="textarea" type="text" name="message" placeholder="How Can We Help?"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="#########################"></div>
<button type="submit" class="button-primary button-primary_place_contact-form">
<p class="button-primary__text button-primary__text_place_contact-form">Get Started</p>
</button>
</fieldset>
</form>
<?php
if (isset($_POST['Email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "cjmaret#gmail.com";
$email_subject = "New form submissions";
function problem($error)
{
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br><br>";
echo $error . "<br><br>";
echo "Please go back and fix these errors.<br><br>";
die();
}
// validation expected data exists
if (
!isset($_POST['Name']) ||
!isset($_POST['Email']) ||
!isset($_POST['Message'])
) {
problem('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['Name']; // required
$email = $_POST['Email']; // required
$message = $_POST['Message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email)) {
$error_message .= 'The Email address you entered does not appear to be valid.<br>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br>';
}
if (strlen($message) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br>';
}
if (strlen($error_message) > 0) {
problem($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Name: " . clean_string($name) . "\n";
$email_message .= "Email: " . clean_string($email) . "\n";
$email_message .= "Message: " . clean_string($message) . "\n";
// create email headers
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your success message below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
#!/usr/bin/php
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n Company: $company \n Email: $email \n Subject: $dropdown \n Message: $message";
$recipient = "cjmaret#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

Contact Form Has No Content

Form submission is received, but the content fields are empty.
Changed "$_POST['Email']" to "$_GET['Email']"
Reviewed 10 other posts on the forum and troubleshooted for 3 hours.
HTML CODE
<form action="callform.php" method="post" class="request-form ftco-
animate">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Full
Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" placeholder="Email
Address">
</div>
<div class="form-group">
<div class="form-field">
<div class="select-wrap">
<div class="icon"><span class="ion-ios-arrow-down"></span></div>
<select name="dropdown" id="Selection" class="form-control">
<option value="">Select Your Services</option>
<option value="">White Paper</option>
<option value="">Brown Paper</option>
<option value="">Black Paper</option>
<option value="">Red Paper</option>
<option value="">Yellow Paper</option>
<option value="">Orange Paper</option>
<option value="">Blue Paper</option>
<option value="">Green Paper</option>
<option value="">Purple Paper</option>
<option value="">Clear Plastic</option>
<option value=""></option>
</select>
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<textarea type="text" name="message" id="message" cols="30" rows="2"
class="form-control" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-primary py-3 px-4"
name="submit">
</div>
</form>
PHP
<?php
$EmailFrom = ".$email";
$mailTo = "my_email";
$Subject = "New Request";
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$txt = "You have received a Request from ".$name.".\n\n".$message;
mail($mailTo, $Subject, $dropdown, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: ""?msg=success');
?>
This is what I receive:
From: CGI-Mailer
To: mailpost#mymail.com
Subject: New Call Request
You have received a Request from .
Please check this
You have used wrong mail format : This is right one
mail(to,subject,message,headers,parameters);
$mailTo = "my_email";
$subject = "New Request";
$name = $_POST['name'];
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$txt = "You have received a Request from ".$name." ".$message;
mail($mailTo, $subject, $txt, $headers);
/* Redirect visitor to the thank you page */
header('Location: ""?msg=success');
?>
You have an extra parameter in your mailTo function.
This:
mail($mailTo, $Subject, $dropdown, $message, $headers);
should become
mail($mailTo, $Subject, $message, $headers);
You were sendind the value of $dropdown as the message and $message as headers. The function was not throwing errors since it accepts an optional parameter value
The rest of your code looks fine if you keep using $_POST since you are using that method in the form definition
Please use this code for send your form email it's working
$body = 'Name:-'.$_POST['name'];
$body .= 'Email:-'.$_POST['email'];
$body .= 'Services:-'.$_POST['dropdown'];
$body .= 'Phone:-'.$_POST['phone'];
$body .= 'Message:-'.$_POST['message'];
$subject = "Your email subject here";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: noreply#yourdomainname.com \r\n";
$to_email = "Your email address here";
if(#mail($to_email,$subject,$body,$headers))
{
echo 'email send sucessfully';
}
else
{
echo "Message could not be sent.";
exit();
}
As I see, after submission, form fields values are sent correctly. I dont find any major issues. But you should do some basic validation before catch fields data in this way -
<?php
if (isset($_POST['submit'])){
$EmailFrom = "your from email goes here";
$Subject = "New Request";
$name = $_POST['name'];
$mailTo = 'mail to email goes here';
$email = $_POST['email'];
$dropdown = $_POST['dropdown'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$headers = 'MIME-Version: 1.0' . "\r\n";
// Additional headers
$headers = 'From: ' .$EmailFrom. "\r\n";
$headers .= 'Reply-To: '.$email. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$txt = "You have received a Request from ".$name.".\n\n".$message;
mail($mailTo, $Subject, $txt, $headers);
/* Redirect visitor to the thank you page */
header('Location: ""?msg=success');
}
?>
However, your mail function contains wrong parameter as I made the correction above. also you can concatenate other fields to $txt variable if you need and add the html markup.

Trouble with PHP

I'm new to PHP. Basically I'm a graphic designer turned frontend designer. I know html css and simple jquery. I need to include this contact form in a website, but doesn't seem to work. Plz help. If someone could point out if anything is wrong with the code, it would be great help.
Thanks in advance.
This is the html part of the contact form :
<div class="form">
<form action="mail.php" method="POST">
<label>Name</label>
<br>
<input type="text" name="name" placeholder="Type Here">
<br><br><br>
<label>Email</label>
<br>
<input type="email" name="email" placeholder="Type Here">
<br><br><br>
<label>Message</label>
<br>
<textarea name="message" rows="20" cols="20" placeholder="Type Here"> </textarea>
<br><br><br>
<label>*What is 2+2? (Anti-spam)</label>
<br>
<input type="text" name="human" placeholder="Type
<br><br><br><br>
<input id="submit" name="submit" type="submit" value="SUBMIT">
</form>
This is the php :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $name \n Message: $message";
$recipient = "saurabhdey84#gmail.com";
$subject = "Vibhor Sogani Website Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your message has been sent." . "-" . <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
Change your echo like this,
echo "Thank You! Your message has been sent." . "- <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
^// no need of contenation here
If you're calling php page, it is just taking blank values, try these changes and let me know,
include_once("home.html");
if(isset($_POST['submit']))
{
**Your php code here**
}
HTML:
Change your text control to
<input type="text" name="human" placeholder="Type the answer"/>
Formatting mistake detected:
<input type="text" name="human" placeholder="Type
<br><br><br><br>
<input id="submit" name="submit" type="submit" value="SUBMIT">
PHP:
Last echo statement should be like this: (missed double-quotes)
echo "Thank You! Your message has been sent." . "-" . "<a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
not like this:
echo "Thank You! Your message has been sent." . "-" . <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
To be better:
echo "Thank You! Your message has been sent. - <a href='home.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
No need any concatenation here!
Check if your POSTS are getting sent to the server script by echoing $_POST['name']. If it is , then it is a problem with the mail() function.
You are using the default php mail() function.It doesnt work without the correct headers. I see that you have used just one 'reply From' header.So you need to look deeper into using mail function(). An alternate for sending emails could be the phpmailer library which is used widely
try this, and put required at closing tags of input
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "maild#gmail.com";
$subject = " contact form" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mesg = '$name $email $message';
$kk=mail($to,$subject,$mesg,$headers);
if($kk){
?>
<script>
window.location.href='thankyou.html';
</script>
}
}
?>
This is how it is usually
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
Like Ive said before , phpmailer library is a more reliable alternate

PHP Contact form won't work

I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>

Sending mail directly from a form

I have an Contact us page on my website. what i want is when someone fills the form and click on send button. The message should be arrived to my gmail. i wrote the following code for it. its not working. is there any other way i can accomplish the same.
Html code:
<form id="ContactForm" action="contacts.php" method="post">
<div>
<div class="wrapper"> <strong>Name:</strong>
<div class="bg">
<input type="text" class="input" name="name">
</div>
</div>
<div class="wrapper"> <strong>Email:</strong>
<div class="bg">
<input type="text" class="input" name="email">
</div>
</div>
<div class="textarea_box"> <strong>Message:</strong>
<div class="bg">
<textarea cols="1" rows="1" name="message"></textarea>
</div>
</div>
<span>Send</span> <span>Clear</span> </div>
</form>
php code
<?php
session_start();
$to = "someemail#gmail.com";
$subject = "Someone Tried to contact you";
$message = $_POST['message'];
$fromemail = $_POST['email'];
$fromname = $_POST['name'];
$lt= '<';
$gt= '>';
$sp= ' ';
$from= 'From:';
$headers = $from.$fromname.$sp.$lt.$fromemail.$gt;
mail($to,$subject,$message,$headers);
echo "mail sent";
exit();
?>
Firstly, you should check your inputs for PHP injection.
$message = stripslashes($_POST['message']);
$fromemail = stripslashes($_POST['email']);
$fromname = stripslashes($_POST['name']);
Apart from that, there doesn't seem to be anything wrong with your mail script. The problem is most likely caused from your PHP server. Does your web hosting definitely provide PHP mail? Most free web hosts do not provide this as they are often used for spamming.
Sorry, but your code is crappy (especially, those concatenations). Use Swift mailer which provides OOP-style and does all the header job for you. And make sure you've got any mail server installed (did you check if you have any?).
PHP form:
<?php
header( 'Content-Type: text/html; charset=utf-8' );
// Your Email
$receiver = 'max.mustermann#domain.tld';
if (isset($_POST['send']))
{
$name = $_POST['name']
$email = $_POST['email'];
if ((strlen( $_POST['subject'] ) < 5) || (strlen( $_POST['message'] ) < 5))
{
die( 'Please fill in all fields!' );
}
else
{
$subject = $_POST['subject'];
$message = $_POST['message'];
}
$mailheader = "From: Your Site <noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "Reply-To: " .$name. "<" .$email. ">\r\n";
$mailheader .= "Return-Path: noreply#" .$_SERVER['SERVER_NAME']. "\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/plain; charset=UTF-8\r\n";
$mailheader .= "Content-Transfer-Encoding: 7bit\r\n";
$mailheader .= "Message-ID: <" .time(). " noreply#" .$_SERVER['SERVER_NAME']. ">\r\n";
$mailheader .= "X-Mailer: PHP v" .phpversion(). "\r\n\r\n";
if (#mail( $receiver, htmlspecialchars( $subject ), $message, $mailheader ))
{
echo 'Email send!';
}
}
?>
HTML form:
<form action="mail.php" method="post">
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Message: <textarea name="message" cols="20" rows="2"></textarea><br />
<input name="send" type="submit" value="Send Email" />
</form>

Categories