简体   繁体   English

如何在utf8中发送电子邮件

[英]How to send an email in utf8

i want to send message of this email in utf8 coding .. 我想以utf8编码发送此电子邮件的消息..

what can i do for this 我该怎么办

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());

what can i send this in utf8 ? 我可以在utf8中发送什么?

You can specify the encoding in the email headers, like so: 您可以在电子邮件标题中指定编码,如下所示:

$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());

You can use php's utf8_encode function. 您可以使用php的utf8_encode函数。 Like so: 像这样:

$message = utf8_encode(stripslashes($_POST['message']));

This will store the string utf8 encoded in your $message variable or any other for that matter. 这将存储在$ message变量或与此相关的任何其他变量中编码的字符串utf8。

Edit : 编辑

If you use the swiftmailer library, it will default to utf8 encoding. 如果使用swiftmailer库,它将默认为utf8编码。

You need to set the utf-8 encoding in the headers and also encode the subject, because it easily gets corrupted. 您需要在标头中设置utf-8编码,并对主题进行编码,因为它很容易损坏。 I personally create my own function which also adds the ability to set the sender's adress: 我亲自创建了自己的函数,该函数还增加了设置发件人地址的功能:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM