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">
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?
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
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.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to be able to collect all the data from the form of my site that is already live and send it to my email.
I based myself in this question here and adjusted my php based on that. However, when I click on submit button, the fields get empty but when I check my email I receive nothing.
How can I collect the data from the site and send to my email?
My code looks like this:
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option value="Unemployed">Unemployed</option>
<option value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'sidney#web2web.co.za';
$to = 'sidney#web2web.co.za';
$subject = '$subject';
$body ="From: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
?>
</form>
PHP requires an installed and running mail system. The program to be used is defined by the configuration settings in the php.ini file.
First you need change your PHP mail configuration:
Open your php.ini file.
Search [mail function].
Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
Save the php.ini file.
Restart your server.
You can use PHPMailer For mail
this is probably the world's most popular code for sending email from PHP!
Your Code is perfect. i checked it after some small changes, Like you put $subject in single quotation marks which is wrong , and you also forgot to print the output which is $result . other wise your code is perfect.
Run this code, if it shows error then please enable SEND MAIL option to your server.
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option value="Unemployed">Unemployed</option>
<option value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'sidney#web2web.co.za';
$to = 'sidney#web2web.co.za';
$subject = $subject;
$body ="From: $name\n E-Mail: $email\n Mobile:\n $mobile Subject: $subject\n Situation:\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
echo $result;
}
?>
I have a problem with checking if a email is valid. but the weird is that i have the same form on to different pages/urls, and on one of the forms it keeps saying that the email is invalid and on the form its valid.
The form on this page works - http://night.sendme.to/about
the form on this page doesnt - http://night.sendme.to/book/jokeren
The HTML on the forms is the same
<form action="" method="post" id="myform">
<div class="form-group">
<label for="name">Navn *</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Navn" required="required">
</div>
<div class="form-group">
<label for="corp">Virksomhed</label>
<input type="text" class="form-control" id="corp" name="corp" placeholder="Virksomhed">
</div>
<div class="form-group">
<label for="email">Email adresse *</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email adresse" required="required">
</div>
<div class="form-group">
<label for="tel">Telefon *</label>
<input type="tel" class="form-control" id="tel" name="tel" placeholder="Telefon" required="required">
</div>
<div class="form-group">
<label for="message">Kommentar</label>
<textarea class="form-control" id="message" name="message" rows="10" required="required"></textarea>
</div>
<button type="submit" class="btn btn-default" id="submit">Send</button>
</form>
<div id="success" style="color:red;"></div>
The PHP is this
<?php // Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOURMAIL';
$subject = 'the subject';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$header .= "from:".$_POST['email'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $header); //This method sends the mail.
echo "Your email was sent!";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
} else {
echo "Invalid Email, please provide an correct email.";
echo var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
The javascript is this
$(document).ready(function(){
$('#submit').click(function(){
$.post("email.php", $("#myform").serialize(), function(response) {
$('#success').html(response);
//$('#success').hide('slow');
});
return false;
});
});
Hope some one can help, why the form only works on the http://night.sendme.to/about and the others
So, to not leave this question answer-less:
In your HTML code, you actually had <form action="" method="post" id="myform"> in both pages – but in your second page, you had another <form> tag right before it … and because of this invalid HTML, the browser ignored the second form tag, and that made $("#myform").serialize() not return any data at all, because it could not find the form element with that id.
You should always validate your HTML code. This helps avoiding such errors.