[英]How to send an email in utf8
我想以utf8编码发送此电子邮件的消息..
我该怎么办
include 'functions.php';
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = stripslashes($_POST['message']);
$cap=strtoupper($_POST['cap']);
$error = '';
$mail = mail(WEBMASTER_EMAIL,$subject,$message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
我可以在utf8中发送什么?
您可以在电子邮件标题中指定编码,如下所示:
$mail = mail(WEBMASTER_EMAIL,$subject,$message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."Content-type: text/html; charset=UTF-8\r\n"
."X-Mailer: PHP/" . phpversion());
您可以使用php的utf8_encode函数。 像这样:
$message = utf8_encode(stripslashes($_POST['message']));
这将存储在$ message变量或与此相关的任何其他变量中编码的字符串utf8。
编辑 :
如果使用swiftmailer库,它将默认为utf8编码。
您需要在标头中设置utf-8编码,并对主题进行编码,因为它很容易损坏。 我亲自创建了自己的函数,该函数还增加了设置发件人地址的功能:
function mail_utf8 ($to, $subject='', $message='', $from='', $header='') {
if (preg_match("/\<html.*\>/i", $message))
$content_type = "html";
else
$content_type = "plain";
$header .= "\nMIME-Version: 1.0";
$header .= "\nContent-type: text/$content_type; charset=UTF-8";
if ($from)
$header .= "\nFrom: ".$from;
mail($to, "=?UTF-8?B?".base64_encode($subject)."?=", $message, trim($header));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.