2

im creating a form in my site. I want the costumers to be able to write and send Greek characters but in the mail im getting something like this Ï≥εÏ∞Ï≥ ελληÎ∏Βκα istead of greek characters. I tryed to change the encoding to UTF-8 with this code:

mail($recipient, $subject, '=?UTF-8?B?'.base64_encode($content).'?=');

Thsis code works in another server that ive tested it but doesnt work in my server. Can anybody help?


Here is my full php code

<?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
 } else {$name = 'No name entered';
 }
if ((isset($_POST['lastname'])) && (strlen(trim($_POST['lastname'])) > 0)) {
$lastname = stripslashes(strip_tags($_POST['lastname']));
 } else {$lastname = 'No name entered';
 }
if ((isset($_POST['nomos'])) && (strlen(trim($_POST['nomos'])) > 0)) {
$nomos = stripslashes(strip_tags($_POST['nomos']));
 } else {$nomos = 'No name entered';
 }
if ((isset($_POST['polh'])) && (strlen(trim($_POST['polh'])) > 0)) {
$polh = stripslashes(strip_tags($_POST['polh']));
 } else {$polh = 'No name entered';
 }
if ((isset($_POST['address'])) && (strlen(trim($_POST['address'])) > 0)) {
$address = stripslashes(strip_tags($_POST['address']));
 } else {$address = 'No name entered';
 }
if ((isset($_POST['TK'])) && (strlen(trim($_POST['TK'])) > 0)) {
$TK = stripslashes(strip_tags($_POST['TK']));
 } else {$TK = 'No name entered';
 }
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
 } else {$email = 'No email entered';
 }
if ((isset($_POST['phone'])) && (strlen(trim($_POST['phone'])) > 0)) {
$phone = stripslashes(strip_tags($_POST['phone']));
 } else {$phone = 'No phone entered';
 }
if ((isset($_POST['sxolia'])) && (strlen(trim($_POST['sxolia'])) > 0)) {
$sxolia = stripslashes(strip_tags($_POST['sxolia']));
 } else {$sxolia = 'Δεν υπάρχουν σχόλια';
 }

 ob_start();
 ?>
<html>
<head>

<style type="text/css">
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">

<tr bgcolor="#eeffee">
 <td>Όνομα</td>
 <td><?=$name; ?></td>
</tr> 
<tr bgcolor="#eeffee">
 <td>Επίθετο</td>
 <td><?=$lastname; ?></td>
</tr>  
<tr bgcolor="#eeffee">
 <td>Νομός</td>
 <td><?=$nomos; ?></td>
</tr>
<tr bgcolor="#eeffee">
 <td>Πόλη</td>
 <td><?=$polh; ?></td>
</tr>
<tr bgcolor="#eeffee">
 <td>Διεύθυνση</td>
 <td><?=$address; ?></td>
</tr>
<tr bgcolor="#eeffee">
 <td>T.Κ</td>
 <td><?=$TK; ?></td>
</tr>

<tr bgcolor="#eeffee">
 <td>Τηλέφωνο</td>
 <td><?=$phone; ?></td>
</tr>

<tr bgcolor="#eeffee">
 <td>Email</td>
 <td><?=$email; ?></td>
</tr>

<tr bgcolor="#eeffee">
 <td>Σχόλια</td>
 <td><?=$sxolia; ?></td>
</tr>

</table>
</body>
</html>
<?
$body = ob_get_contents();

$to = 'mymail@yahoo.gr';
$email = 'mymail@yahoo.gr';
$fromaddress = "address";
$fromname = "Online Contact";

require ("phpmailer.php");

$mail = new PHPMailer();

$mail -> From = "address";
$mail -> FromName = "Book Order";

$mail -> AddAddress("mymail@gmail.com", "Name 5");

$mail -> WordWrap = 50;
$mail -> IsHTML(true);

$mail -> Subject = "Book Form:  Book form submitted";
$mail -> Body = $body;
$mail -> AltBody = "This is the text-only body";

if (!$mail -> Send()) {
$recipient = 'mymail@yahoo.gr';
$subject = 'Contact form failed';
$content = $body;

    $header = 'Content-type: text/html; charset=UTF-8' . "\r\n";
    mail($recepient,$subject, '=?UTF-8?B?'.base64_encode($content).'?=', $header);


exit ;
  }
 ?>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
man_or_astroman
  • 648
  • 1
  • 17
  • 39

6 Answers6

3

The encoded-word syntax is expected for the subject or other header field values but not for the body; there you can use MIME and Content-Type:

$headerFields = array('MIME-Version: 1.0', 'Content-Type: text/plain;charset=utf-8');
mail($recipient, '=?UTF-8?B?'.base64_encode($subject).'?=', $content, implode("\r\n", $headerFields));
Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

Try this.

$header = 'Content-type: text/plain; charset=UTF-8' . "\r\n";
mail($recepient,$subject, '=?UTF-8?B?'.base64_encode($content).'?=', $header);

use Content-type: text/html if you are sending a html email.

Eswar Rajesh Pinapala
  • 4,841
  • 4
  • 32
  • 40
0

Where does $content come from? Do you do anything like substr($content) that's not multibyte-safe, before mail()ing it?

dogglebones
  • 387
  • 1
  • 8
0
/* Set internal character encoding to UTF-8 */
mb_internal_encoding("UTF-8");

$headers  = 'MIME-Version: 1.1';
$headers .= 'Content-type: text/html; charset=utf-8';
$headers .= "From: $name <$email>";
$headers .= "Return-Path: $emailTo";
$headers .= "Reply-To: $email";
$headers .= "X-Mailer: PHP/". phpversion();
0

That happens with Greek Characters φίλε μου. I edited the settings of my mailbox. For example in Roundcube Settings > Displaying Messages > Advanced Settings > Default Character Set = UTF-8 (Unicode)

  • 2
    Welcome to StackOverflow. I suggest you improve your answer, have a look here → [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Federico Baù Jan 06 '21 at 15:41
-2

I found a solution at web after 8 hours dealing with the same problem...

It worked for me (php, html form and mysql) =>

http://akrabat.com/php/utf8-php-and-mysql/

gogo
  • 1
  • 1