Getting a white screen when trying to submit my contact form which has these entries:
- Name
- Email
- Subject
- Message
Im attempting to recieve emails through my website. Ive checked all variable names and such and it seems that everything is correct. Im new to PHP so im a little cloudy on what to try next.
Thanks
<form method"POST" action="action/form-submit.php"> <!--NO FOR ATTRIBUTE, NOT ADDING FUNCTIONALITY-->
<h2>Contact Me:</h2>
<label>Your Name:</label>
<input name="name" type="text" placeholder="Your Name..." required/>
<label>Email:</label>
<input name="email" type="email" placeholder="Email..." required/>
<label>Query Type:</label>
<select id="qry" name="query" required>
<option value="" disabled selected>Please Select:</option>
<option value="jobs">Jobs</option>
<option value="website">Website Issues</option>
<option value="info">Information</option>
</select>
<label>Your Message:</label>
<textarea name="info" placeholder="Your Message..." required></textarea>
<input type="submit" value="Submit">
</form>
Then the PHP code:
<?php
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$email_from = "test#gmail.com";
$email_subject = "New Website Submission";
$email_body = "Visitor Name: $vname.\n".
"Visitor Email: $vemail.\n".
"Visitor Subject: $vquery.\n".
"Visitor Message: $vmessage.\n";
$to = "bradleyarcher98#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $vemail \r\n";
mail($to,$email_subject,$email_body,$headers);
header("location: contact.html");
?>
In order to see what is wrong, you need to turn on the PHP error handling.
When this is on, you won't just see a white page anymore, an error message with filename, line number and a message describing the error is sent to the browser.
<?php
session_start();
$vname = $_POST['name'];
$vemail = $_POST['email'];
$vquery = $_POST['query'];
$vmessage = $_POST['info'];
$subject = " YOUR MESSAGE Title ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: "YOUR SITE NAME" . "\r\n";
$message = "
<!-- you can add CSS Aswell -->
<div>
<p>Visitor Name: ".$_POST['name']."<p>
<p>Visitor Email: ".$vemail."<p>
<p>Subject: ".$vquery."<p>
<p>Message :".$vmessage."<p>
</div>
";
mail($to,$subject,$message,$headers);
?>
Related
I'm trying to create contact form that would let me attach file. I'm very new to PHP. I have tried looking for youtube videos but was not able to find any ussefull information. I'm hoping that you could help me with my PHP.
Also if there is better alternataive or the way of doing it , please share it with me.
HTML
<form style="display:flex; flex-direction: column;" action="../mail/mail.php" method="post">
<input type="text" id="name" name="name" placeholder="Your full name.." required="required" />
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="Your e-mail.." required="required" />
<label for="country">Country</label>
<select id="country" name="country" required="required">
<option value="Netherlands">Netherlands</option>
<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option <option value="Tajikistan">Tajikistan</option>
</select>
<label for="subject">Subject</label>
<select id="subject" name="subject" required="required">
<option value="Choose">Click here to select..</option>
<option value="Choose1">Click here to select1..</option>
<option value="Choose2">Click here to select.2.</option></select>
<div class="attachment-row">
<input id="attachment-file" type="file" class="input-field" name="attachment[]">
</div>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write your message here.."style="height:200px"></textarea>
<input type="submit" class="btn-send-message" name="submit" value="Send" /></form>
PHP
<?php
ob_start();
if(isset($_POST['submit'])){
$to = "info#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$country = $_POST['country'];
$subject = $_POST['subject'];
$message = "From: ". $from . "\n\n" . "Subject: ". $subject . "\n\n" . "Country: ". $country ."\n\n"."Name: ". $name ."\n\n". "Wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
mail($to,$subject,$message);
mail($from,$subject2,$message2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
header("Location: ../thank-you-for-contacting.php");
}
ob_end_flush();
?>
First of all you don't need the output buffer at all. What you're doing here is simply loading a single echo line into memory and dumping it on the end.
Have a read on:
https://www.php.net/manual/en/function.ob-start.php
Your current code doesn't even look for attachments, you'd need something along the lines of:
if (count($_POST['attachment'])) {...}
Next thing you need to figure out is the structure of the email itself. For basics have a quick read on:
https://www.php.net/manual/en/function.mail
Keeping the headers and message itself separate is always a good idea, creating variables only used once is a bad one. SO you might try something like:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$headers[] = 'To: '.filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}
// They're both really bad filters, for application security You should do your research and define your own. Character encoding is a big deal btw.
Once you have a email formatted properly you should look at the attachments mentioned earlier. To understand how those are sent have a read on:
https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
TLDR; // If you want to code things, nothing is too long... but this should provide some clarity
header[] = "Content-Type: multipart/mixed; boundary=$UNIQUE_string";
$message = "--$UNIQUE_string".PHP_EOL;
$message .= "Content-type: text/plain;".PHP_EOL;
$message .= $content.PHP_EOL;
$message .= "--$UNIQUE_string".PHP_EOL;
$message .= 'Content-Disposition: form-data; name="myFile"; filename="foo.txt"'.PHP_EOL;
$message .= "Content-Type: text/plain".PHP_EOL;
$message .= file_get_contents('foo.txt').PHP_EOL;
$message .= "--$UNIQUE_string--".PHP_EOL;
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");
}
?>
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.
Not a PHP expert. With that said, I've used this system before and it seemed to stop working one day. I can't seem to get emails sent anymore even if I'm not getting any error messages. I don't know what's wrong.
Form page:
<form method="POST" name="contactform" action="contact-form-handler.php">
<label for='name'>Your Name:</label> <br>
<input type="text" name="name">
<label for='email'>Email Address:</label> <br>
<input type="text" name="email"> <br>
What does this concern?<br>
<select name="options">
<option value="null">--</option>
<option value="IEP">IEP</option>
<option value="Section 504">Section 504</option>
<option value="Other">Other</option>
</select>
<p style="width: 50%">Please include in your message what type of service you are inquiring about. Please be as descriptive as possible so we can help you more efficiently. Thank you.</p>
<label for='message'>Message:</label> <br>
<textarea name="message" cols="45" rows="7"></textarea>
<input type="image" src="img/submit.png" alt="Submit"> <br>
</form>
Handling:
<?php
$errors = '';
$myemail = 'louie540x#gmail.com;';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['options']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$options = $_POST['options'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Regarding a(n): $options \n \n Message: \n \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.php');
}
?>
<?php include 'template/top.php'; ?>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
<?php include 'template/bottom.php'; ?>
$myemail = 'louie540x#gmail.com;';
Should be:
$myemail = 'louie540x#gmail.com';
However that may not be your only problem...
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>