PHP and Russian Letters - php

What is happening with Russian letters when sending via PHP request to ... a mail, by e.g.?
the "hardcoded" russians letters are displayed properly, but from the Form's textboxex with hieroglyphs:
HTML page:
<tr>
<td style="width: 280px">Содержание работ</td>
<td><input type="text" id="workContent"/></td>
</tr>
PHP page:
$WorkContent = $_REQUEST["workContent"]; //Содержание работ
// ...
$WorkContentLabel = "Содержание работ";
// ...
$message .= $WorkContentLabel . ":\t" . $WorkContent . "\n";
// ...
// email stuff (data below changed)
$to = "test#gmail.com";
$from = "me#domain.com";
$from_header = "From: Russian site command ";
$subject = "Message with russian letters";
$subject = '=?utf-8?B?'.$subject.'?=';
$message .= $subject;
// send message
mail($to, $subject, $message, $from_header);
User enter some content in the textbox:
alt text http://lh3.ggpht.com/_1TPOP7DzY1E/S1y6Y0wb9tI/AAAAAAAAC88/OkdMQkO47HQ/s800/works.png
and the submits the form.
What do I receive (in GMAIL):
Содержание работ: 1)Содержание 2)RABOT
So, hard-coded Russian text - OK, sent by the form Russian text - NOK, sent by the form ASCII text - OK.
Does somebody know what could be the cause of that strange behavior with the encoding?
EDIT:
used
$subject = " оборудования - subject with russian letters";
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
$message .= $subject;
obtains a subject like
�����������ÿ - subject with russian letters http://lh6.ggpht.com/_1TPOP7DzY1E/S1zFqFe9ohI/AAAAAAAAC9E/PZ7C4JtEHTU/s800/subject.png

You need to base64_encode() your $subject, like this:
$subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';
Make sure you're also saving your .php file encoded as UTF-8 no BOM.
This question might also interest you: Is this the correct way to send email with PHP?

Check your encodings:
HTML encoding (in the <meta http-equiv..> tag)
PHP/HTML/template file encoding (what encoding your editor saves the file in)
Database encoding (if applicable) (in what encoding the data in the tables is in)
Database connection encoding (if applicable) (what encoding is used for database connections)
and use UTF-8 for everything.

As well as what Alix said about base64 in the RFC2047 encoded-word in your Subject line, you also need to tell the mailer to expect UTF-8-encoded text in the body of the mail, by adding headers:
MIME-Version: 1.0
Content-Type: text/plain;charset=utf-8
otherwise it's up to the mailer to guess, probably wrongly.

Related

Utf8 encoding in php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 1 year ago.
I am getting some data from a html form and sending them by email with PHP.
The issue is that i am getting weird characters because the language that the user writes in the form is Czech.
For example the name Anežka becomes Anežka
This is the code i have for my php which being used for sending the email.
<?php
if(isset($_POST['submit'])){
$to_alex = "myemail#honeywell.com"; // this is your Email address
$first_name = $_POST['fname'];
$first_name=mb_convert_encoding($first_name, "UTF-8", "ISO-8859-1");
$last_name = $_POST['lname'];
$email = $_POST['email'];
$skola = $_POST['vysoka_skola'];
$obor = $_POST['obor'];
$prace = $_POST['prace'];
$phone_num=$_POST['phone_number'];
$linkedin=$_POST['linkedin'];
$oponenta=$_POST['oponenta'];
$vedouciho=$_POST['vedouciho'];
$files = $_FILES['myfile']['name'];
$kategorie = $_POST['kategorie'];
//echo gettype($files);
$all_thesis_string="";
$all_vedouciho_string="";
for($index=0;$index<count($_FILES['myfile']['name']);$index++){
$all_thesis_string.=$_FILES['myfile']['name'][$index].","; //create a string with all bachelor attachments of user
}
for($index=0;$index<count($_FILES['vedouciho_files']['name']);$index++){
$all_vedouciho_string.=$_FILES['vedouciho_files']['name'][$index].","; //create a string with all vedouciho attachments of user
}
for($index=0;$index<count($_FILES['oponenta_files']['name']);$index++){
$all_vedouciho_string.=$_FILES['oponenta_files']['name'][$index].","; //create a string with all oponenta attachments of user
}
$subject = "Form submission of ".$first_name." ".$last_name;
$message = "User Details: \n\n Jméno: $first_name \n Příjmení: $last_name \n Email: $email \n Telefon: $phone_num \n Linkedin: $linkedin \n Vysoká škola: $skola \n Studijní obor: $obor \n Odkaz na bakalářskou práci: $prace \n Kategorie: $kategorie \n Posudek oponenta: \n $oponenta \n Posudek vedouciho: \n $vedouciho \n Bakalářská práce: $all_thesis_string \n Vedouciho files: $all_vedouciho_string";
$headers = "From:" . $first_name;
mail($to_alex,$subject,$message,$headers);
echo '<span style="color:#AFA;text-align:center;"><strong>"Přihláška úspěšně odeslána. Děkuji "</strong></span>';
//include 'upload.php';
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
I have tried mb_convert_encoding in my last attempt but still i have the issue.
You need to tackle possible issues on both input and output stages of your program:
make sure, the browser knows, that you want UTF-8: It needs the tag <meta charset="utf-8"> somewhere in the <head> of your HTML. Otherwise it is possible, that it falls back to some legacy encoding when sending form data back to you.
make sure, e-mail clients know, that you send UTF-8 to them. They, too, will fall back to legacy encodings without explicitly telling them. Try adding these headers to the mail:
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
If you want UTF-8 in the subject and/or To fields, you need a separate way of quoting. I suggest you take a look at the excellent PHPMailer package that will handle all that for you.

The e-mail header from the online form is displayed incorrectly

I have a questionnaire form on the web. After filling in this form, I send it to me by email, using the PHP function mail(). The form body and the data it contains, including the private message are displayed correctly on gmail.com. The problem, however, occurs in the header of the email itself. Some characters are displayed incorrectly.
Here is a sample header:
$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;
Required display of email subject:
Nový dotaz -- námět, od Fořt Petr <p.fort1990#gmail.com>
Simultaneous displaying of the subject:
Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990#gmail.com>
The squared times symbol is more like a rectangle.
Is anything wrong? Or where should I look for a mistake?
I'm not sure \r\n works on all platforms
see : Which line break in php mail header, \r\n or \n?
instead
("xxx\r\n\yyy");
use
Header('xxx');
Header('yyy');
or use PHP_EOL, not "\r\n"
Problem solved. My hosting provider uses different character encoding for the headers - I can't explain why, but the following php function will do it all.
function recode_to_utf8 ($text, $encoding = "utf-8")
{
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
And now all you have to do is send an email using the mail () method in combination with the method defined above recode_to_utf8(). Like this:
mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));
I hope it helps others if they have the same problem as me.

PHP: Line Breaks on email sending not working

The following PHP code works perfectly, but it is not doing line breaks for some reason.
PHP:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: '".$title."' <".$store_email."> \n";
$subject = "New Payment Received";
//MESSAGE
$message = "New payment was successfully recieved through paypal payment terminal:";
$message .= "\r\n\nFrom ".$paypal->pp_data['payer_email'];
$message .= "\r\nPaid: ".$paypal->pp_data['payment_gross']." ".$paypal->pp_data['mc_currency'];
$message .= "\r\nDate: ".date('d/m/Y');
$message .= "\r\nTime: ".date('g:i A');
mail($admin_email,$subject,$message,$headers);
Any wonder what's wrong? Thanks in advance.
You're sending HTML e-mail. Line breaks have no meaning in HTML, you'll need <br /> tags.
The direct answer ceejayoz gives is correct and to the point in that the html element <br> is needed because it is a html email.
The bigger issue is that not all email is readable in html (example: user doesn't allow html emails). Anyone sending email should send it in 2 parts. One being a html formatted message and the other "alternative" in plain text. In that way the recipient will be able to read the email regardless of email reader.
The \r\n line break works in plain text alternative part and in html<br> or other elements as needed to format.
Doing this will avoid the next question. Recipients are complaining my emails are blank.

Ajax contact form and UTF-8

i'm using ajax contact form, downloaded from: http://youhack.me/2010/07/22/create-a-fancy-contact-form-with-css-3-and-jquery/
Everything works ok except UTF as i can't use cyrilic symbols when submitting.
The php:
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message
$receiver = "receiver#domain.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to
if (!empty($name) & !empty($email) && !empty($body)) {
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, "From: {$email}");
if ($send) {
echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
}
It uses jquery.validationEngine-en for validation.
My html already has "Content-Type" content="text/html; charset=utf-8" in header.
I'm new to php and jquery, so i would appriciate some guidance to make UTF-8 work when submitting.
Thanks :)
Edit: When i try to use cyrilic chars (čšćđ) on a required field i get ajax input error "Please use letters only". If i submit the form with cyrilic chars on a non-required field, i receive and email, all letters show ok except cyrilic, which are like this: Å¡.
Edit 2: When i set the recipient to gmail (webmail), cyrilic chars show up ok, except in one field, where Ajax doesnt let me use them (regex from Reinder answer).
When i set recipient in outlook (local) and submit the form, none of the cyrilic chars don't show up ok, example: ÄĹĄ oÄa ĹĄ ÄŽŠÄÄ
SOLVED Thanks to Reinder for guide and David! Will solve it today :)
having looked at the plugin you're using, I think this has to do with the validation regex inside jquery.validationEngine-en.js
when the validation is set to 'onlyLetter' it will check using
/^[a-zA-Z\ \']+$/
and none of your characters čšćđ are allowed here...
you need to create a language validation javascript for the language you're using and change that regular expression. For example, have a look at this post
The next thing is to check the encoding of your PHP file and your headers.
Place this at the top of your PHP code
header("Content-type: text/html; charset=utf-8");
Check if the values are correctly displayed when just outputting them in PHP, like so:
echo $name;
If they are correctly displayed in the browser and it's just the email that's incorrectly displaying the characters, then you need to pass an encoding header to the email too
example:
$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$body = "Name: {$name}\n\nSubject: {$web}\n\nMessage: {$body}";
$send = mail($receiver, 'Contact from domain.com', $body, $headers);
have a look at the mail function on the PHP.NET website
Rather than use the default PHP mail() function, I've found this come in handy when working with Japanese:
http://bitprison.net/php_mail_utf-8_subject_and_message

PHP Mail: special characters show different between Firefox and IE

I have a problem when sending a form via email using the PHP Mail function. This is the code that I'm using:
$name = $_POST['name'];
$last_name = $_POST['last_name'];
$company = $_POST['company'];
$email = $_POST['email'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$message = $_POST['message']); //This comes from the form
$formcontent="Name: $name $last_name <br> Company: $company <br> Email: $email <br> Country: $country <br> Telephone: $phone <br><br> Message: $message";
$mailheader = 'MIME-Version: 1.0' . "\r\n";
$mailheader .= 'Content-type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable' . "\r\n";
$mailheader .= 'From: ' . $email . "\r\n";
mail("my#email.com", "subject", $formcontent, $mailheader) or die("Error!");
This is a form that will be sending spanish and special characters, like ñ, accents, ç, etc...
The problem is that, if I use it like this, it works fine in Firefox 3.6.3, but when using the form in Internet Explorer 8, the special characters that sends are all messed up (like ç instead of a ç). However, if I add utf8_encode to the variables in the $formcontent, then it works in IE, but it stops working in Firefox, showing stuff like η instead of ç.
What can I do to make it work regardless of the browser? Any help would be much appreciated!
EDIT:
I've noticed that, if I echo the $formcontent variable before sending it with mail, if I'm using Firefox, the special characters are already messed-up. How can I avoid the browsers interfering with what's being sent? Please I'm totally clueless right now!!! I don't know what to change to have something working. Even if it's a dumbed down version, is there any alternative to have PHP Mail working with special characters in both browsers?
Basically you need to make sure that every charset on your page is the same. Make sure the page has a utf-8 charset aswel. Since you're sending it in utf8 the input must come from utf8 aswell.
Could you perhaps show us the code of the page (or a live demo) where the mail is being made?

Categories