PHP mail(); How to set charset for subject and ema

2019-09-15 04:48发布

I'm using the following code to send an e-mail:

<?php
$to = $_POST['email'];
$subject = "Subject!";
$message = "Hello!";
$headers = "From: Me<example@example.com>\r\n";
$headers .= "Return-Path: example@example.com\r\n";
$headers .= "BCC: example@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=utf-8\r\n";
mail($to,$subject,$message,$headers);
?>

Although using charset=utf-8 the characters in the subject and the email address line still doesn't get decoded right. I'm getting characters like this: ößä.

How can I set the charset for subject and email address line, too?

1条回答
ら.Afraid
2楼-- · 2019-09-15 05:19

To encode the subject of an email you should use:

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

And then inside the mail function:

mail($to,$subject,...);
查看更多
登录 后发表回答