I made captcha by this [tutorial][1],
[1]: http://codechirps.com/how-to-add-a-completely-custom-captcha-to-any-web-form/ but it seems to me it doesn't complete.
I made code but i can send email even if i put wrong answer. I feel that i have to write extra code in php file but i don't know where. Any help greatly appriciated
<div class="modal-body">
<form class="contact" name="contact">
<label class="label" for="name">Имя</label><br>
<input type="text" name="name" class="input-xlarge"><br>
<label class="label" for="email">E-mail</label><br>
<input type="email" name="email" class="input-xlarge"><br>
<label class="label" for="message">Сообщение</label><br>
<textarea name="message" class="input-xlarge"></textarea>
</form>
</div>
<div class="modal-footer">
<p>2 + 3 =</p>
<input type="text" name="captcha" />
<input class="btn btn-warning" type="submit" value="Отправить" id="submit">
Закрыть
<?php
$myemail = '';
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
$captcha = check_input($_POST['captcha']);
echo "<span class=\"alert alert-success\" >Сообщение отправлено</span><br><br>";
if (!preg_match("/5/", $captcha))
{
show_error("Check your math, Dude");
}
$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\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}?>
Okay, so you need to check the values of your inputs to see if they are valid. If not, you display an error and the mail doesn't get sent. If all the checks pass, the maildoes get sent. So you need to check the $_POST['email'] and the $_POST['captcha'] field (and if you want to, check if the rest isn't empty or whatever).
In php, you can do this like this:
$myemail = "";
if(isset($_POST['name'])){ // check if a form is submitted
if(empty(trim($_POST['name'])) || empty(trim($_POST['message'])) || empty(trim($_POST['email'])) || empty(trim($_POST['captcha'])) ){ // check if values are not empty
echo "Please fill in all the required fields.";
}else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ // check email
echo "Please give a real e-mail address.";
}else if(!preg_match("/5/", $_POST['captcha'])){ // the code provided by your script
echo "Get your math right, dude!";
}else{ // all fields seem to be ok
// sanitize input using htmlspecialchars(), see http://stackoverflow.com/a/5788361/1319187
$name = htmlspecialchars($_POST['email']);
$email = $_POST['email']; // email doesn't need to be sanitized since it's been filtered
$message = htmlspecialchars($_POST['message']);
//Send the mail
$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\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
if(mail($to,$email_subject,$email_body,$headers)){
echo "Succes! Your mail has been sent!";
}else{
echo "Something went wrong with the sending of a mail.";
}
}
}
Should be rather straightforward, you can google some functions if you don't know what they do.
I also have no idea where check_input() comes from. It's not a native PHP function, and the link you provided doesn't show what it does. Also, the regex to check whether the value of the captcha is 5 is a bit stupid, you could just check $_POST['captcha'] == '5'. Also keep in mind you have to randomize these values a bit.
Related
How can I remove the ugly error caused by PHP? I don't know if the webhost is causing it or if I can fix it with PHP/CSS, I have no email validation in my code The error
<div class="row">
<div class="row">
<div class="input-field col s6">
<input id="email" type="email" class="validate" name="email">
<label for="email" data-error="Geen geldig e-mailadres" data-success="Dit e-mailadres word alleen gebruikt om een antwoord te versturen.">Email</label>
</div>
</div>
</div>
<?php
error_reporting(0);
$errors = '';
$myemail = 'my#mail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "/n fill in all fields";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
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 Message \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: bedankt.html');
}
?>
</body>
</html>
This is the full code for the email part, error_reporting(0); does not seem to work for the described problem
Change Your Html Email
From
<input type="email" >
To
<input type="text" >
As I commented, changing the mail type to text in your HTML will remove the validation. You mentioned you want to keep the field type as email. To disable the the HTML5 validation, you need to add novalidate to your form.
As in:
<form method="post" action="/yourAction" novalidate>...</form>
Reference
I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>
I have a very simple html contact form that asks from the user a name, an email and then the message area. The problem is that when the user enters their name in greek characters (as the site is in greek language), the message never gets delivered. I tested it thoroughly and I found out that there is no problem if in the textarea there are greek characters, the problem appears only in the name field.
The code for my contact form is this one:
<form id="contact" method="post" action="mailer-backup.php" enctype="multipart/form-data" accept-charset="UTF-8">
<input type="text" id="name" name="name" required placeholder="Όνομα">
<input type="email" id="email" name="email" required placeholder="Email">
<textarea id="message" name="message" required placeholder="Μήνυμα"></textarea>
<button id="submit" type="submit">Αποστολή</button>
</form>
As you can see, it calls an external php script, which after messing with for a whole day but without a positive result looks like this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
$options="-f contact#my-website.gr";
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo "All fields are required, please fill the form again.";
exit;
}
$recipient = "contact#my-website.gr";
$name = '=?utf-8?b?' . base64_encode($_POST['name']) . '?=';
$from="From: $name<$email>\r\nReturn-path: $email";
$subject = "New contact from $name - my-website.gr";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
if (mail($recipient, '=?UTF-8?B?'.base64_encode($from).'?=', $subject, $email_content, $from, $options)) {
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
http_response_code(500);
echo "Tragic! Something went wrong and we couldn't send your message.";
}
} else {
echo "There was a problem with your submission, please try again.";
}
?>
I spent my whole day doing all sorts of experiments but as I am not a programmer, I failed to make it work. For any kind people that will respond with a possible solution, please remember, I am NOT a programmer.
I think you actually have a problem with the mail command call arguments - looks like you put your sender's info as your second argument while it should be subject line.
So, when I replaced your call with
mail($recipient, $subject, $email_content, $from, $options)
it worked just fine for me with UTF in the name field.
Like the title says, sending a form to my email. I get no errors, but the email never comes. Here's my HTML form (I don't think you'll need anything else, but there is also some formatting and Java verification in the file).
<form method="POST" name="contactform" action="contact-form-handler.php">
<p>
<label for='name'>Your Name:</label>
<br>
<input type="text" name="name">
</p>
<p>
<label for='email'>Email Address:</label>
<br>
<input type="text" name="email">
<br>
</p>
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message"></textarea>
</p>
<input type="submit" value="Submit">
<br>
</form>
And here's my PHP. Obviously I took my email out and put in EMAIL instead, but other than that this is my complete PHP file. The thank you PHP file pulls up the submitted page just fine, and I get no errors. Just no email either.
<?php
$errors = '';
$myemail = 'EMAIL#gmail.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
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 Message \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.html');
}
?>
Thanks ahead of time for any help you can provide! I can give the rest of my HTML file or my other PHP file if you need it, this is where all the real functionality lies though.
*PHP to send form data to an email i have used this code as well as its work for me .you can try *
<?PHP
$email = $_POST["emailaddress"];
$to = "you#youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email\n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you#youremailaddress.com\n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
?>
after that you can addded your futher code
Try add in top file:
error_reporting(E_ALL);
and edit your code, see:
if(mail($to,$email_subject,$email_body,$headers)){
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
} else {
echo 'Error!';
}
Read this:
http://www.php.net/manual/en/function.error-reporting.php
http://www.php.net/errorfunc
http://php.net/manual/pt_BR/function.set-error-handler.php
http://www.php.net/register_shutdown_function
you have the variable $myemail as a string value after $to = Remove the parentheses and your code will work
The problem is with your From field in your $headers variable.
you can't just put any email address in there. For example: you are supposed to put an existing email address of your server that you create. Or if you want to use a gmail account in from field, you need to configure your gmail username and password with your server first before using that email.
The simplest solution is to create a new email address on your hosting server and then put that email address in your from field in $headers variable.
I've already mentioned it in details here.
I need a very simple, but effective PHP form handler that will work with this form:
<form id="contact" method="post" action="contact_handle.php">
<label for="name">Name:</label>
<input type="text" class="text_style" placeholder="John Doe" name="name" /><br />
<label for="email">Email:</label>
<input type="email" class="text_style" placeholder="email#example.com" name="email" /><br />
<label for="message">Subject:</label>
<textarea class="areawidth" rows="4" name="message" /></textarea><br />
<button id="contactbutton" type="submit">Submit</button>
</form>
This is what I currently have but it doesn't send an email and it doesn't redirect like I intended. And I cant seem to figure out whats going on.
<?php
$invalid = '';
$my_email = 'my#email.com';
// Validate input:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$invalid.= "\n All fields are required";
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Validate email:
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email))
{
$invalid .= "\n Invalid email address";
}
// Send email if no errors detected:
if( empty($invalid))
{
$to = $my_email;
$subject = "Contact form submission: $name";
$body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message:\n $message";
$headers = "From: $email\n";
$headers .= "Reply-To: $email";
mail($to,$subject,$body,$headers);
//redirect to the thank you page:
header('Location: contact_thanks.php');
}
?>
When it is submitted it takes me to contact_handle.php that displays blank and fails to redirect.
i think the redirect has to be the first output to the browser, so if anything else is being output it won't work.
why don't you just include the thank you page rather than redirecting to it, ie.
include 'contact_thanks.php';
If you're providing all of the code, the issue is that your form is throwing an error (by adding text to $invalid, but there is no code to handle $invalid not being empty. To solve this, add this after the closing } at the end:
echo $invalid;