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

天大地大妈咪最大 提交于 2019-12-12 03:55:59

问题


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:


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,...);


来源:https://stackoverflow.com/questions/42259623/php-mail-how-to-set-charset-for-subject-and-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!