I made a form for contacting me on my website.
This is the php code:
<?php
$errors = '';
$myemail = 'yourname#website.com';//<-----Put Your email address here.
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. ".
"name: $name \n\n email: $email_address \n\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_thanks.html');
}
?>
When I use the form on the website, I receive a mail with weird symbols. That is because I write in Hebrew in the form- English letters are delivered well.
When I change "myemail" to another mail, the message is delivered well, even in Hebrew, but I want the message to pass to the first mail.
I changed many times the encoding, but none seems to solve the problem, perhaps I hadn't tried the correct one yet.
I am not the one who wrote the code and my I don't know php well, so please give me answers that are easy to comprehend.
You have to specify content encoding in your mail header. For this reason, add this line:
$headers .= "\nContent-type: text/html; charset=UTF-8";
Related
This question already has answers here:
Relying on HTML 'required' for simple form validation
(4 answers)
Closed 1 year ago.
I am a beginner helping my aunt build a personal website. I have an HTML form where users can contact her (slightly simplified for clarity):
<form id = "contact-form" method = "post" action = "contact-form-handler.php">
<input name = "name" type = "text" required><br>
<input name = "email" type = "email" required><br>
<textarea name = "message" class = "form-control" required></textarea><br>
<input type = "submit" class = "form-control submit" value = "SEND MESSAGE">
</form>
The script contact-form-handler.php reads,
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = "SEND_EMAIL#ADDRESS.com";
$email_subject = "Message from fziastories";
$email_body = "Name: $name.\n".
"Email: $visitor_email.\n".
"Message: $message.\n";
$to = "RECIEVE_EMAIL#ADDRESS.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject, $email_body, $headers);
header("Location: index.html");
?>
This form works great. When I enter various tests, the message goes through perfectly. And when I don't enter a value for one of the three inputs, I am not able to hit the 'SEND MESSAGE' button.
Except, every once in a while, maybe once or twice a week, I receive an empty message with no values filled out. This is confusing to me because I figure the current setup precludes users from submitting the form without entering value but also through the tests I have ruled out the possibility that a valid response is being entered as blanks.
I would greatly appreciate any advice! If you have any follow-up questions, do not hesitate to ask. Thank you!
The required tag in HTML forms is not very secure!
You should always validate the data on your server.
This could look something like this:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
header("Location: index.html");
exit;
}
$email_from = "SEND_EMAIL#ADDRESS.com";
$email_subject = "Message from fziastories";
$email_body = "Name: $name.\n".
"Email: $visitor_email.\n".
"Message: $message.\n";
$to = "RECIEVE_EMAIL#ADDRESS.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject, $email_body, $headers);
header("Location: index.html");
?>
I have on the web email form, when I type all the informations it sends me an email, however when I type some czech characters (the site is czech) it doesnt work. Some of the characters shows as "čřžýáĂ" for example. I downloaded free template from some website, which I edited with my friend who knows HTML, but not PHP. I tried to find out some advice, but it says nothing to me, because I dont know almost anything about PHP and when I tried to copy some part of the code and hoped that it will worked it havent... Can you please help me?
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'xxxx#pl.cz'; // Add your email address inbetween the ''
replacing yourname#yourdomain.com - This is where the form will send a
message to.
$email_subject = "Website Contact Form: $name";
$email_body = "Nova zprava z webu.\n\n"."Udaje:\n\nJmeno: $name\nEmail:
$email_address\n\nMessage:\n$message";
$headers = "From: noreply#plantaen.cz\n"; // We recommend using something
like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Add header to mail
Try this:
$body = '<html><head><title>Title</title></head>'."\r\n";
$body .= 'Test';
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
$headers .= 'From: YourName <"from#yoruname.com">';
mail("to#yourname.com","title",$body,$headers);
Edit :
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
echo "No arguments Provided!";
exit();
} else {
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$email_subject = "Website Contact Form: $name";
$email_body = '<html><head><title>plantaen</title></head>'."\r\n";
$email_body .= "Nova zprava z webu.\n\n"."Udaje:\n\nJmeno: $name\nEmail: $email_address\n\nMessage:\n$message";
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
$headers .= 'From: plantaen <"noreply#plantaen.cz">';
mail("noreply#plantaen.cz", $email_subject, $email_body, $headers);
echo "Done!";
exit();
}
Contact form on site use script below. It send 2 messages - to me and to the person filling in the form. In second mail I have problem with characters like "ąźćęś"....I would like to use utf-8. I set it in header, but it doesn't work. I need to use in message body html tags also.
What wrong in the code?
<?php
// Check for empty fields
if(empty($_POST['email']) ||
empty($_POST['phone']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$email_address = $_POST['email'];
$phone = $_POST['phone'];
// Create the email and send the message
$to = 'me#test.com';
$email_subject = "Mail to you";
$email_body = "Hello\n\n"."Test\n\nEmail: $email_address\n\nPhone: $phone";
$headers = "From: test#test.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
$to2 = "$email_address";
$email_subject2 = "=?UTF-8?B?".base64_encode("Thanks for you message")."?=";
$email_body2 = "Test mail ążźćęśó\n\nBest regards\nTester";
$headers2 .= "Content-Type: text/html; charset=UTF-8";
$headers2 = 'From: Tester <test#test.com>' . "\r\n";
mail($to2,$email_subject2,$email_body2,$headers2);
return true;
?>
I currently have a contact form, from which e-mails are composed and sent using php. I use a separate php file for that.
$errors = '';
$myemail = 'mail#gmail.com';//
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']))
{
$errors .= "\n Будь ласка, заповніть усі поля";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['phone'];
$page = getRequestURI();
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "Некоректна адреса e-mail";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "Нове замовлення туру: $page".
"Деталі:\n Ім'я: $name \n Email: $email_address \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.html');
}
I need to put such forms on different pages. And there should be information about the page on which form was submitted in the message sent by e-mail.
Is there a possibility to do that without creating separate php files for every page?
In other words how can I get the name (or url) of the page, on which form was submitted?
You can get the current request URI by using $_SERVER['REQUEST_URI'].
Assuming your script is located at http://example.com/some/page-here/more, $_SERVER['REQUEST_URI'] would be /some/page-here/more.
I've recently started using HTML, Javascript and PHP to create my own website.
I have been trying to make a PHP script that takes inputs from a form, validates with javascript and php, then sends the email.
I know the script is executed and runs fine, as it sucessfully redirects me to the new page at the end. However, it does not actually send any emails for some reason.
Here is the code:
<?php
$errors = '';
$myemail = '<removed due to privacy>';
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');
}
?>
Now I did see a few similar questions, but as I am very incompetent at php, they didn't really help.
If you can identify the reason, I'd be very grateful.
$sent = mail($to,$email_subject,$email_body,$headers);
put a condition like this
if($sent){ header('Location: contact-form-thank-you.html'); } else { echo 'mail failed';}
if it show "mail failed" configure your localhost to sent mails
follow this tutorials
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/