PHP email message in UTF-8

Asked

Viewed 443 times

0

I’m trying to make a form able to send an email and I’m having a problem understanding how to send the email message in UTF-8, which I managed to do quietly with the subject of the email, but not with the message.

Below is the PHP code:

if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $mailFrom = $_POST['mail'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $subject = '=?UTF-8?B?'.base64_encode("$subject").'?=';

    $mailTo = "[email protected]";
    $headers = "From: ".$mailFrom;
    $txt = "Foi recebida uma nova mensagem de: ".$name.".\r\n".$message;

     mail($mailTo, $subject, $txt, $headers);
    header("Location: index.html");
}

2 answers

1

Hello try instead of:

 $subject = '=?UTF-8?B?'.base64_encode("$subject").'?=';

use:

$subject = "Content-Type:text/html; charset=UTF-8\n";
  • Unfortunately it didn’t work, when it includes this code, all it did was change the subject of the email to: Content-Type:text/html; charset=UTF-8.

1

You need to add two headers below the variable $headers so that your email is sent in the correct way.

$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.