Wrong message encoding

My site has a form handler like this:

$username = @ trim ($_POST['username']);
$email = @ trim ($_POST['email']);
$message = @ trim ($_POST['message']);
if($username == NULL or $email == NULL or $message == NULL)
{
  echo '    .        .';
}
else
{
mail("[email protected]",
      "Message from www.my-site.ru",
      "$message \n\n Name: $username \n The sender's e-mail: $email");
echo "<div style='background:seashell;width:80%;margin:3em auto;text-align:center;border:1px solid grey;'>";
echo "!<br />      e-mail- $email   .";
echo "<br /><a href='http://www.my-site.ru'>  .</a></div>";
}

The problem is that in a letter coming to a mailbox, be it mail.ru or rambler.ru, the Cyrillic alphabet is displayed incorrectly. Doctype of the page with utf-8 form.
I saved the handler in both utf-8 and windows-1251. All the same, instead of Russian letters, krakozyabry. I googled, found this code for converting encodings, but, frankly, I didn’t understand anything in it, since I don’t understand php very well.

<?php
function send_mime_mail($name_from, //  
                        $email_from, // email 
                        $name_to, //  
                        $email_to, // email 
                        $data_charset, //   
                        $send_charset, //  
                        $subject, //  
                        $body //  
                        ) {
  $to = mime_header_encode($name_to, $data_charset, $send_charset)
                 . ' <' . $email_to . '>';
  $subject = mime_header_encode($subject, $data_charset, $send_charset);
  $from =  mime_header_encode($name_from, $data_charset, $send_charset)
                     .' <' . $email_from . '>';
  if($data_charset != $send_charset) {
    $body = iconv($data_charset, $send_charset, $body);
  }
  $headers = "From: $from\r\n";
  $headers .= "Content-type: text/plain; charset=$send_charset\r\n";
  return mail($to, $subject, $body, $headers);
}
function mime_header_encode($str, $data_charset, $send_charset) {
  if($data_charset != $send_charset) {
    $str = iconv($data_charset, $send_charset, $str);
  }
  return '=?' . $send_charset . '?B?' . base64_encode($str) . '?=';
}
?>

Answer 1, authority 100%

In the header of the email, explicitly specify the encoding of the email and its type (html/text, usually). And you will be Happy!


Answer 2, authority 100%

$rem_mail = '[e-mail]';
mail($rem_mail, "", "  ", "Content-Type: text/html; charset=UTF-8");

Answer 3, authority 100%

Solving the problem with crocozyabras, abracadabra, incorrect encoding, incorrect encoding of letters sent to mail.ruusing the mail() php function in utf-8 encoding :

Specify as the subject of the email: =?utf-8?B?”. base64_encode(“Subject”). “?=

I.e. should be:

$to = "[email protected]";
$subject="=?utf-8?B?". base64_encode(" "). "?=";
$header="From: mysite.ru"; 
$header.="\nContent-type: text/html; charset=\"utf-8\"";
$message = " ";
mail($to, $subject, $message, $header);

Answer 4, authority 50%

$bound="raZdeLitel_9988";
$subj="";
$headers="From: \"\" <[email protected]>\n";
$headers.="To: \n";
$headers.="Subject: $subj\n";
$headers.="Mime-Version: 1.0\n";
$headers.="Content-Type: multipart/alternative; boundary=\"$bound\"\n";
$body="--$bound\n";
$body.="Content-type: text/html; charset=\"windows-1251\"\n";
$body.="Content-Transfer-Encoding: 8bit\n\n";
$body.="
  <br /><br /><br />";
$body.="\n\n--$bound\n";
mail("[email protected]", $subj, $body, $headers);

Content must be converted to Windows-1251 in this example. This design also allows you to send an attachment, it is enough to divide the $ Bound separator in the body ($ Body) and specify the headers to the file.