PHP Form with Captcha doesn't work (white screen) - php

So the PHP contact form for my website doesn't work. No idea why. Tried many different templates. Only one worked for a while, before I tried to insert captcha there, and then it also started making 500 error.
No idea how PhP works from the word du tout - I just need a simple form for my personal website to have a captcha and that's all I ever want from PHP at this stage.
Below is the code that doesn't work (supposedly it should, as I have done it from this tutorial https://www.youtube.com/watch?v=4cr85kvM8I0
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$email_from = 'contact#mywebsite.com';
$email_subject = 'New email that you have received';
$email_body = "Name: $name.\n".
"Subject: $subject.\n".
"Email: $email_address.\n".
"Message: $message.\n";
$to_email = "contact#mywebsite.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-to: $email_address\r\n";
//your site secret key
$secretKey = 'mykey';
$responseKey = $_POST['g-captcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response-> success)
{
mailto($to_email,$email_subject,$email_body,$headers);
echo "Message Sent Successfully";
}
else {
echo "Invalid Captcha, try again";
}
}
?>
and this is the contact form (part of it on the html)
Head code:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
...
<div class="container">
<h3>Contact Form</h3>
<form action="contact-form-handler.php" method="POST"
enctype="multipart/form-data" name="contact_form">
<div class="nameemail">
<label for="name">Name</label>
<input name="name" type="text" required placeholder="Name"/>
<label for="subject">Subject</label>
<input name="subject" type="text" required placeholder="Subject"/>
<label for="email">Email</label>
<input name="email" type="email" required placeholder="you#domain.com"/>
</div>
<br>
<label for="message">Message</label><br>
<textarea name="message" cols="30" rows="10" placeholder="Enter your message here ..." required> </textarea>
<div class="center">
<div class="g-recaptcha" data-sitekey="mykey"></div>
<input type="submit" value="Submit">
</div>
</form>
</div>
So why in the mother of God this doesn't work?

Related

Contact Form submission failing?

I'm currently working on my portfolio. As of right now I'm trying to get the contact form to work. So I don't really know php that well and I'm not that good at it, but I've been googling and trying different ideas for myself. As of right now none of them have worked. Below I will attach the contact form html portion as well as the php I have now. Any help would be appreciated greatly
<section id="contact-form">
<!--Contact form section-->
<form method="POST" name="emailForm" action="index.php">
<!--Beginning of form and mail to my email address-->
<div class="contact-left">
<!--Contact info on the left side-->
<h1 class="c-l-heading">Contact</h1>
<!--Major heading-->
<div class="f-name">
<!--Name portion-->
<font>Name</font>
<input type="text" name="name" placeholder="Full Name" required/>
<!--Input field with a placeholder-->
</div>
<div class="f-email">
<!--email portion-->
<font >Email</font>
<input type="email" name="name" placeholder="Example#gmail.com" required/>
<!--Input field with a placeholder-->
</div>
</div>
<div class="contact-right">
<!--Contact info on the right side-->
<div class="message">
<!--Message portion-->
<font >Message</font>
<textarea name="message" rows="5" cols="20" placeholder="Write Message..." required></textarea>
<!--Textbox created-->
</div>
<button>submit</button>
<!--Submit button-->
</div>
</form>
</section>
PHP section now:
<?php
if(!isset($_POST['submit']))
{
echo "Error! You need to submit the form!";
}
//Collection
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validation
if(empty($name) || empty($visitor_email))
{
echo "Name and email are required"
exit;
}
$email_from = 'desmondlambkin17#gmail.com';
$email_subject = "dnjls.ca Contact Submission";
$email_body = "Name: $name\n"
"Email address: $visitor_email\n"
"Message: $message";
$to = "desmondlambkin17#gmail.com";
$headers = "From: $email_from";
//Send Email
mail($to, $email_subject, $email_body, $headers);
?>
Currently when I try to submit an email it says This page isn't working, and is currently unable to handle the request

E-mail contact form data with PHP

I have a html form and I want to take the data that is entered and send it to myself in an e-mail, I'm not at all familiar with PHP but after some Googleing it seemed to be the way to go.
I'm not too sure what's not quite working, but any insight would be awesome!!
HTML:
<section class="contact" id="contact">
<div class="container">
<div class="section-heading">
<h1 data-aos="fade-right" data-aos-delay="150">Contact</h1>
<h6 data-aos="fade-left" data-aos-delay="150">Contact Me</h6>
</div>
<form method="post" name="contact_form" action="contact-form-handler.php" data-aos="fade-up" data-aos-delay="200" onsubmit="return false">
<label for="name">Name:</label>
<input type="text" id="name" name="name" placeholder="Enter Your Name..." required>
<label for="name">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter Your E-mail..." required>
<label for="number">Contact Number:</label>
<input type="number" id="number" name="number" placeholder="Enter Your Contact Number...">
<label for="message">Message:</label>
<textarea name="subject" id="subject" cols="10" rows="10" placeholder="Enter Your Messgage..."></textarea>
<input type="submit" value="Submit" onclick="sendContact();">
</form>
<?php include 'contact-form-handler.php';?>
</div>
PHP: [separate file in same directory called contact-form-handler.php]
<?php
if(!empty($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["number"];
$subject = $_POST["email"];
$content = $_POST["subject"];
$toEmail = "admin#phppot_samples.com";
$mailHeaders = "From: " . $name . "<". $email .">\r\n";
if(mail($toEmail, $subject, $content, $mailHeaders)) {
$message = "Your contact information is received successfully.";
$type = "success";
}
}
?>
Again, any kinda advice is very appreciated!
Everything seems fine in your HMTL. But I think that you misunderstood the PHP part about sending an email.
The "FROM:" field in your header should be the address that you own in the mail server, see the example below.
Also, setting the content type and charset is recommended :)
<?php
$mailHeaders = "Content-type:text/html;charset=UTF-8" . "\r\n";
if(!empty($_POST["submit"])) {
$name = $_POST["name"];
$email = $_POST["number"];
$subject = $_POST["email"];
$content = $_POST["subject"];
$toEmail = "admin#phppot_samples.com";
$mailHeaders .= "From: <Your#DomainName.com>\r\n";
if(mail($toEmail, $subject, $content, $mailHeaders)) {
$message = "Your contact information is received successfully.";
$type = "success";
}
}
?>
Furthermore, I would recommend to read threw the documentation :)
https://www.php.net/manual/en/function.mail.php

html form with php is not sending email

I'm writing to create a contact form on my website and then get the information sent to my inbox, but it is not working. Please take a look at my code below (PHP is not my thing) and let me know where I've gone wrong.
here is my html form
<form action="" method="post">
<div class="row">
<div class="col-md-6">
<input type="text" name="name" placeholder="Name"class="form-control" required>
</div>
<div class="col-md-6">
<input type="text" name="number" class="form-control" placeholder="Phone"required>
</div>
</div>
<input type="email" class="form-control" placeholder="Email" name="email" required>
<textarea name="message" class="form-control"style="resize:none;" placeholder="Message" rows="8" cols="30" required></textarea>
<input type="submit" name="submit" value="contact us">
</form>
This is my php code
<?php
$message ='';
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['message'];
$number = $_POST['number'];
$msg = $name ."<br>".$email."<br>".$comments."<br>".$number;
$to = "karunagoyal7#gmail.com";
$subject = "Email from Contact Us from";
if(mail( $to, $subject, $msg)){
$message = "<div class='alert alert-success'> your message has been send to saisoftlinktechnologies </div>";
}else{
$message = "<div class='alert alert-primary'> your message has not been send to saisoftlinktechnologies </div>";
}
}
?>
Both html from and php script is in same file.After submitting the form it is not even displaying the message of success or failure.

PHP code with google reCaptcha not sending email

My php webform was working until the point I added an if(isset) and also a reCaptcha.
Help?
Tried to debug the code - could not find an issue
html
<form class = "form-inline d-flex justify-content-center" action="contact.php" method = "post">
<div class="mc-field-group-1">
<input name="name" type="text" placeholder="Name" id="mce-LNAME"/>
</div>
<div class="mc-field-group-2">
<input name = "email" type="text" placeholder="Email address*" class="required email" id="mce-EMAIL" required/>
</div>
<div class="mc-field-group-3">
<input name = "subject" type="text" placeholder="Subject" class="required email" id="mce-EMAIL"/>
</div>
<div class="mc-field-group-4">
<textarea name="message" rows="10" cols="39" placeholder="Your message"></textarea>
</div>
<div class="g-recaptcha" data-sitekey="my_key(have the real key here in my code)"></div>
<div class="clear">
<input type="submit" name="submit" id="mc-embedded-subscribe" class="button" value="Send">
</div>
</form>
php
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "realemail#gmail.com";
$mailheader = "From: $email \r\n";
$secretKey = "my_key(have the real key here in my code)";
$responseKey = $_POST['g-recaptcha-response'];
$UserIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$UserIP";
$response = file_get_contents($url);
$response = json_decode($response);
if ($response ->success)
{
mail($recipient, $subject, $formcontent, $mailheader);
echo "Thank You!";
}
else
{
echo "Invalid Captcha, Please Try Again";
}
}
?>
When I click 'Send' in the form takes me to the contact.php page but email not sent to my gmail inbox. Has nothing to do with gmail as I said was working before I tweaked the code to add the reCaptcha.

Sending emails via PHP server

Straight to the point. I'm trying to set up a contact form on my website but struggling to make it work. Also have read through numerous of related topics here on SO. I followed tutorial thus I've got decent familiarity with this topic. I'm sure my php.ini file is configured just fine since sending emails worked until recently (although it always ended up in junk mail). Until I made changes to the code. Changes that I needed to make sooner or later. Strangely, sending emails worked when I ran it with hard-coded details with no variables. But when I implemented variables then it stopped working. Obviously I need PHP to process user's input through variables. How can get PHP to read user's input ? I think the most important part is using the attribute name="something" in input tags.
Sharing relevant code:
PHP:
<?php
$fullName = $errorMsg = $sucMsg = "";
$errMsg = $email = $subject = $emailErr = $message = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (isset($_POST['submit'])) {
$fullName = test_input($_POST['fullName']);
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$emailErr = "Your email address is not valid !";
} else {
$email = $_POST['email'];
}
$subject = test_input($_POST['subject']);
$message = test_input($_POST['message']);
if($emailErr == "") {
$emailTo = "mark.alexa.uk#gmail.com";
$headers = "From ".$fullName." ".$email." via contact form";
$headers .= "Reply-To: ".$email;
$headers .= "Return-Path: webmaster#ubuntuserver\r\n";
$headers .= "CC: alexa.mark#mail.com\r\n";
$headers .= "BCC: alexa.mark#mail.com\r\n";
if (mail($emailTo, $subject, $message, $headers)) {
$sucMsg = "<p>The email was sent successfully !</p>";
} else {
$errMsg = "<p>The email could not be sent !</p>";
}
}
}
?>
HTML:
<div class="container" id="form">
<div id="successMsg" class="alert alert-success" role="alert"><?php
echo $sucMsg; ?></div>
<div id="errMsg" class="alert alert-danger" role="alert"><?php echo
$errMsg; ?></div>
<form method="POST" action="contact.php">
<fieldset class="form-group">
<label for="fullName">Full name</label>
<input type="text" class="form-control" name="fullName"
id="fullName" placeholder="full name" required>
</fieldset>
<fieldset class="form-group">
<label for="email">Email</label>
<input type="email" id="email" placeholder="email" class="form-
control" required>
<small id="emailHelp" name="email" class="form-text text-muted">I
won't share your email with anyone</small>
</fieldset>
<fieldset class="form-group">
<label for="subject">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" placeholder="subject" required>
</fieldset>
<fieldset class="form-group">
<label for="message">Your message</label>
<input type="text" class="form-control" name="message" id="message" placeholder="message" required>
<input type="submit" id="submit" class="btn btn-success" value="Submit">
</fieldset>
</form>
</div>
UPDATE:
How should I modify the PHP code so it won't end up in junk mail ?
Add name="submit" to your submit button. Forms pass on the name attribute to the post array. So your code test for:
if (isset(`$_POST['submit']`)) {
$fullName = test_input($_POST['fullName']);
..isn't valid because $_POST['submit'] isn't set.
<input type="submit" id="submit" name="submit" class="btn btn-success" value="Submit">

Categories