发送邮件时出现错误字符


Wrong characters when sending mail

我使用PHP中的mail()函数发送的电子邮件的字符设置有问题。

我编写PHP代码的实际文件是UTF-8字符集。我还确保在电子邮件的标题中设置charset=UTF-8,甚至在基本HTML内容的标题中添加了一个元标记。尽管如此,它还是不起作用。

这就是PHP代码的样子:

// Prepare the e-mail to the GUEST
$to = $email;
$subject = "PÅMINNELSE: Bekräfta registrering för Event";
// Defining the headers
$headers = "MIME-Version: 1.0" . "'r'n";
$headers .= "Content-type: text/html; charset=UTF-8" . "'r'n";
$headers .= "From: Pre-registration <event@adomain.com>" . "'r'n";
include("gemailreminder.php");
// Send the mail now!
mail($to, $subject, $message, $headers);

包含的文件gemailreminder.php的内容如下:

<?php
$message = <<< MESSAGE
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>REMINDER E-MAIL</title>
</head>
<body>
<p >text...</p >
</body>
</html>
MESSAGE;
?>

有什么建议可以帮助我解决这个问题吗?

提前感谢!

//Andreas

包含非ascii字符的电子邮件内容必须使用base64编码发送。

使用PHP的base64_encode()函数很容易做到这一点,但坦率地说,在使用PHP的mail()函数时,有太多类似的怪癖需要注意。

我的建议是使用一个已经编写的现有类mailer类,它为您做了所有的艰苦工作——不再麻烦标题,不再担心编码,发送附件变得很容易,等等。

通常推荐的类是Swiftmailer和phpMailer。。

使用它们中的任何一个,我保证您再也不想直接调用糟糕的旧mail()函数了。

您必须在utf-8中保存gemailreminder.php,请使用编辑器设置。

FYI:

出于某种奇怪的原因,它突然又开始工作了。也许我的网络酒店提供商在那里的服务器或设置上有一段时间出现了一些问题。

我感谢你为提供答案付出的时间和努力。我仍然会更加熟悉Swiftmailer和phpMail这两个类。

真诚地,Andreas