簡體   English   中英

如何在utf8中發送電子郵件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM