Home > Blockchain >  PHP Mail - receiving wrong words on the mail server
PHP Mail - receiving wrong words on the mail server

Time:12-04

When I try to send mail from a web page - on the mail.bg and abv.bg mail I receive letters like this :

РќРѕРІРѕ

where I must receive "Ново" What can I do so I can fix it - on gmail.com I receive the normal "Ново" and when I use echo - I also receive the normal "Ново" , but on the mail.bg and abv.bg - I receive this strange words...is there something wrong with the encoding ?

<?php 
header('Content-Type: text/html; charset=utf-8');
header('Content-Transfer-Encoding: base64');
$errors = '';
$myemail = '[email protected]';//<-----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']; 
$subject = $_POST['subject'];


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 = "$subject, $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";
    $headers .= 'Content-Type: text/plain; charset=utf-8' . "\r\n";
    $headers .= 'Content-Transfer-Encoding: base64'. "\n\r\n";
    mail($to, '=?utf-8?B?'.base64_encode($email_subject).'?=', $email_body, $headers);

    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');


} 

?>

When I try to send mail from a web page - on the mail.bg and abv.bg mail I receive letters like this :

РќРѕРІРѕ

where I must receive "Ново" What can I do so I can fix it - on gmail.com I receive the normal "Ново" and when I use echo - I also receive the normal "Ново" , but on the mail.bg and abv.bg - I receive this strange words...is there something wrong with the encoding ?

CodePudding user response:

Your headers are specifying Content-Transfer-Encoding: base64 but you have not applied base64_encode on your contents. (in your case $email_body)

On the other hand, please use \r\n to separate respective header lines

Hence, change the block

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";
$headers .= 'Content-Type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64'. "\n\r\n";
mail($to, '=?utf-8?B?'.base64_encode($email_subject).'?=', $email_body, $headers);

to

$email_body=base64_encode($email_body);

$headers = "From: $myemail\r\n"; 
$headers .= "Reply-To: $email_address\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64'. "\r\n";
mail($to, '=?utf-8?B?'.base64_encode($email_subject).'?=', $email_body, $headers);

Alternatively, remove Content-Transfer-Encoding: base64 specification, like this:

//$email_body=base64_encode($email_body);

$headers = "From: $myemail\r\n"; 
$headers .= "Reply-To: $email_address\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8' . "\r\n";
//$headers .= 'Content-Transfer-Encoding: base64'. "\r\n";
mail($to, '=?utf-8?B?'.base64_encode($email_subject).'?=', $email_body, $headers);

CodePudding user response:

It looks like you are having an issue with the encoding of your email messages when you send them from your web page. In order to fix this, you can try adding the following line of code to your PHP script:

mb_internal_encoding("UTF-8");

This line of code sets the internal encoding of your script to UTF-8, which is a common character encoding that supports most languages, including Bulgarian. This should ensure that your email messages are properly encoded and displayed correctly on the mail.bg and abv.bg mail servers.

  • Related