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
Related
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?
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.
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">
Hey guys so I have an HTML document with a contact form I created and it is not working. I have the PHP in a seperate PHP file like so:
HTML:
<form class="form-horizontal" action="form_process.php" method="post" name="contact_form">
<div class="form-group">
<label class="col-sm-2 control-label white-color">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="email" placeholder="Email" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label white-color">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name" placeholder="First Name" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label white-color">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" placeholder="Type your message here!" name="message" required></textarea>
<button type="submit" class="btn btn-default btn-lg text-center" id="send-btn" name="submit">Send</button>
</div>
</div>
</form>
And here is the PHP:
<?php
if (isset($POST['name']) && isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$to = 'j_goris#live.com';
$subject = 'JorgeGoris.com Form Submission';
$text = "Name: ".$name."\n"."Email: ".$email."\n". "Wrote the following: "."\n\n".$message;
if(mail($to, $subject, $text, "From: ".$name)){
echo '<h1>Thanks! I will get back to you shortly.</h1>';
}
else {
echo 'Sorry there was an error! Please try again.';
}
}?>
This is my first time tackling PHP contact forms. I uploaded all my files to my server and still no dice. Can you guys see whats wrong?
Replace:
$POST['name']
to:
$_POST['name']
Replace:
if(mail($to, $subject, $text, "From: ".$name)){
To:
if(mail($to, $subject, $text, "From: ".$email)){
Full Code:
<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
$email = $_POST['email'];
$name = $_POST['name'];
$message = $_POST['message'];
$to = 'j_goris#live.com';
$subject = 'JorgeGoris.com Form Submission';
$text = "Name: ".$name."\n"."Email: ".$email."\n". "Wrote the following: "."\n\n".$message;
if(mail($to, $subject, $text, "From: ".$email)){
echo '<h1>Thanks! I will get back to you shortly.</h1>';
}
else {
echo 'Sorry there was an error! Please try again.';
}
}
?>
I'm a total novice so please bear with me :)…I've managed to create a form and used PHP to send the data to an email address. However, once I click submit; the screen goes blank instead of staying on the current page and displaying a message. I'm guessing i'm missing some sort of PHP code?
Also, i'd like to use the JQuery validator plugin on my form, how can I add it without basically screwing up the form?
MY HTML:
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
MY PHP:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
PHP script that you create will return an empty page, because that script just to send email. I think you need to combine PHP script and HTML script together with PHP script in top of script to get that you want and edit form action to empty like this sample:
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
}
?>
<div>
<form id="form_id" name="form_name" action="" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
</div>
Your form will take the user to scripts/index.php. You are echoing the '$feedback' var on the page with the HTML form. Redirect from scripts/index.php using
header("location: filelocation");
exit();
You can achieve this in two ways :
1. Have php and html code in one page.
2. Use ajax to submit your form.
<div>
<form id="form_id" name="form_name" action="scripts/index.php" method="post">
<div>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="John Smith" required/>
</div>
<div>
<label for="email">Email: </label>
<input type="email" name="email" id="email" placeholder="name#mail.com" required/>
</div>
<div>
<label for="message">Message: </label>
<textarea name="message" id="message" rows="5" cols="30"></textarea>
</div>
<div>
<input id="submit" type="submit" name="submit" value="submit" />
</div>
</form>
</div>
<?php
$to = 'example#gmail.com';
$subject = 'Message from The Rocket Factory';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$body = <<<EMAIL
Hi, my name is $name.
$message
From $name
My Address is $email
EMAIL;
$header = "From: $email";
if($_POST){
mail($to, $subject, $body, $header);
$feedback = 'Thanks for your message';
echo '<p id="feedback">'.$feedback.'</p>'; <-- Notice this..
}
?>
You can also use ajax in jquery ($.ajax) or javascript.