PHP Mail Form Language Issues - php

Hello guyz and thank you in advance. I have made an email form using php to send an email through a form from my website
Although the form works fine and the emails are sent I have a problem when I sent a message in my native language (Greek). If I write the message in English everything is fine and the email is view-able. If I send a message in Greek then the email is sent , but looks like this :
³Î¾ÏƒÎ»Î´Î³Î·ÎºÎ»ÏƒÎ´ ασλαηκσλκαςξδςακσ³Î¾ÏƒÎ»Î´Î³Î·ÎºÎ»ÏƒÎ´ ασλαηκσλκαςξδςακσ
Here is the code I use in my php form :
<?php
if(isset($_POST['email'])) {
$email_to = "info#something.gr";
$email_subject = "Email Form - WebSite";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['message']; // required
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<center>
Thank you for contacting us. We will be in touch with you very soon.</br>
You will be redirected to our homepage in 5 seconds </center>
<script type="text/javascript">
setTimeout("window.location='/'",5000);
</script>
<?php
}
?>

The problem is with character encoding.
By default, PHP E-mails are sent with ISO-8859-1.
You'll need to swap to UTF-8 for languages like Greek :)
Fortunately, you can set this in the headers:
$headers = "Content-Type: text/html; charset=UTF-8";
Combined with your existing headers:
$headers = 'From: ' . $email_from . "\r\n" .
'Reply-To: ' . $email_from . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Hope this helps!

Related

Why does this only work once?

I'm trying to put together a simple feedback form which uses PHP to email the results to us. The script works once, we get the email as intended.. but every time afterwards, there's no email and no error. Anyone have any idea why?
<?php
$email_to = "admin#urbansushi.com";
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$date = $_POST['date']; // required
$email_subject = "New feedback from CUSTOMER";
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Date of Visit: ".clean_string($date)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
You are concatenating to $email_message but are not declaring it initially:
$email_message .= "Name: ".clean_string($name)."\n";
Try adding this
$email_message = "";
$email_message .= "Name: ".clean_string($name)."\n";
also you have whitespace before your php declaration - try removing it so that the php declaration is at the start of the file
<?php

how to Send Email details notification using php?

How can I send email notification to my users who gave registration details in my form using php?
I have code which runs perfectly to get emails to me, but now I also want to same details to my users.
I am trying to use "$from" into my array at "$to" but getting no email.
my mail.php
<?php
$subject = "Email Notification";
$message = "Thank you for your email";
$message = "Your Registering details are as follows:";
$message .= "<br><br>";
$message .= "<table border='1'>";
$message .= "<tr><td>Name</td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email</td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";
$from = $_POST['email'];
$to = array('my_address#example.com', 'my_address2#example.com', $from);
$lp = "notification#example.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= 'from: '.$lp .'' . "\r\n" .
'Reply-To: '.$lp.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
mail($row,$subject,$message,$headers);
}
echo "Mail Sent.";
die;
?>
It's pretty common that on shared hosting you need to send an email from an "existing email", at least match the domain. Maybe that's why the emails are not being sent?
So for example, if your domain is "www.my-awesome-domain.com", you can't send email headers
"from: example#example.com"
Instead, make sure to send emails from your domain, and ideally an existing email box, for example:
"from: office#my-awesome-domain.com"
Hope it helps! :)

Date from php contact form is wrong

All of a sudden contact forms filled in on my website are coming into my inbox from the date 1/1/1970???
They are ending up at the bottom of my inbox and I have missed a few leads...
Any ideas how this can start happening all of a sudden?
The code I am using on my contact page is : -
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "my email address";
$email_subject = "Website Contact Enquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['tel']) ||
!isset($_POST['message'])||
!isset($_POST['formtype'])
) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$tel = $_POST['tel']; // required
$message = $_POST['message']; // required
$formtype = $_POST['formtype'];
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Tel: ".clean_string($tel)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message .= "formtype: ".clean_string($formtype)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion().date();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Add this to your headers:
'Date: ' . date('r'),
Also, make sure to sanitize $email_from. Right now, you are allowing spammers to send E-Mail to other recipients and change the header. Read more here: http://www.securephpwiki.com/index.php/Email_Injection

How to send a welcome email and redirect page using php

Currently I have the code below which sends the contents of the html input field (email) to the welcome#domain.com address. There are two things which I am now struggling with. I have used the following bits of code to no avail -
$to = "welcome#domain.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thankyou.html');
How can I automatically send an email response from welcome#domain.com?
Instead of the page saying 'Thank you for contacting us, we will be in touch very soon' is there a way that they are redirected to domain.com/thankyou.html instead?
This is the rest of the code which seems to be working fine! If there are any suggestions for this though they are always more than welcome!
if(isset($_POST['email'])) {
$email_to = "welcome#domain.com";
$email_subject = "Your spot has been saved!";
function died($error) {
echo "We are very sorry, but there were error(s) found with the form your submitted";
echo "These errors appear below<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors<br /><br />";
die();
}
if(
!isset($_POST['email'])
) {
died("We are sorry, but there appears to be a problem with the form your submitted!");
}
$email_from = $_POST['email'];
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= "The Email Address you entered does not appear to be valid!<br />";
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= 'Email: '.clean_string($email_from)."\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
Regarding page being not redirected refer this post stackoverflow.com/questions/6396813/php-header-not-redirecting
It basically says you must not output any value to stream before header call.
For mail issue please elaborate more what error you are getting.

add a second email address to a script

i have a script that i have modified to meet my requirements however i now need to send the email to more than one person, could someone point me in the right direction as to how i could modify the script to send to more than one person.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "emailremoved#sample.com";
$email_subject = "Kro Catering Website Enquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['your_name']) ||
!isset($_POST['type']) ||
!isset($_POST['guests']) ||
!isset($_POST['date']) ||
!isset($_POST['phone']) ||
!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$your_name = $_POST['your_name']; // required
$type = $_POST['type']; // required
$guests = $_POST['guests']; // required
$date = $_POST['date']; // not required
$phone = $_POST['phone']; // required
$email_from = $_POST['email']; // required
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Your Name: ".clean_string($your_name)."\n";
$email_message .= "Type: ".clean_string($type)."\n";
$email_message .= "Guests: ".clean_string($guests)."\n";
$email_message .= "Date: ".clean_string($date)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
<?php
header( 'Location: /thanks.aspx' ) ;
?>
<?php
}
?>
Search for the line:
$email_to = "emailremoved#sample.com";
And keep adding e-mails with a comma separating them:
$email_to = "emailremoved#sample.com,emailremoved#sample.com,emailremoved#sample.com";
PHP's mail() function is quite versatile when it comes to the "to" field. See the documentation here. Any one of the listed examples would be fine:
user#example.com
user#example.com, anotheruser#example.com
User <user#example.com>
User <user#example.com>, Another User <anotheruser#example.com>
So since your $email_to variable is not cleaned or otherwise modified after you set it on line 5, you should be able to just put 2 there separated by a comma (as in the examples above that I copied from the documentation I linked to.)
Try this!
It was the only code that worked for me.
$header .= 'Bcc: someaddress#email.com';

Categories