Search code examples
phpphpmailer

PHP mail encoding issue


i found an issue with php mail sending. I need to encode the mail in UTF-8. The subject works fine, but the message is corrupted. This is my code

    <?php
header('Content-Type: text/html; charset=utf-8'); 
if (isset($_POST['submit'])) {
    $name = $_POST['fname'];
    $mail = $_POST['fmail'];
    $discord = $_POST['fdiscord'];
    $vek = $_POST['fage'];
    $q1 = $_POST['fq1'];
    $q2 = $_POST['fq2'];
    $q3 = $_POST['fq3'];
    $q4 = $_POST['fq4'];
    $q5 = $_POST['fq5'];

    $mailto = "[email protected]";
    $headers = 'Content-Type: text/plain; charset=utf-8' . "\r\n";
    $headers .= 'Content-Transfer-Encoding: base64' . "\r\n";
    $headers = "From: ".$mail;
    $mailsub = '=?UTF-8?B?' . base64_encode("Nábor - ".$name) . '?=';
    $mailmsg =  '=?UTF-8?B?' . base64_encode("Odpověď na nábor:" .$name.".\n\n\nDiscord:".$discord.".\n\nVěk:".$vek);

    mail($mailto, $mailsub, $mailmsg, $headers);
    header("Location: done.html?mailsend");
}

?>

And this is how the mail looks like

Subject Nábor - Kolombooo From [email protected] Message T2Rwb3bEm8SPIG5hIG7DoWJvcjpLb2xvbWJvb28uCgoKRGlzY29yZDpLb2xvbWJvb28jMzI1Ny4KClbEm2s6MTU=


Solution

  • The problem seems to be that you are encoding your message as base64:

    Change this:
    base64_encode("Odpověď na nábor:" .$name.".\n\n\nDiscord:".$discord.".\n\nVěk:".$vek);
    To this:
    "Odpověď na nábor:" .$name.".\n\n\nDiscord:".$discord.".\n\nVěk:".$vek;