【问题标题】:Php contact form, how to make it send with charset utf-8? [duplicate]Php联系表格,如何使用charset utf-8发送? [复制]
【发布时间】:2014-03-06 14:31:05
【问题描述】:

我有以下 php 联系表单代码,它运行良好,只是它没有使用我的语言需要的字符集 UTF-8 发送。

有人知道如何使用 urf-8 发送吗?

提前感谢,杰克

<?php
$error    = ''; // error message
$name     = ''; // sender's name
$email    = ''; // sender's email address
$subject  = ''; // subject
$message  = ''; // the message itself
$spamcheck = ''; // Spam check
$telefon = ''; // telefon
$kategori = ''; // kategori

if(isset($_POST['send']))
{
$name     = $_POST['name'];
$email    = $_POST['email'];
$subject  = $_POST['subject'];
$message  = $_POST['message'];
$spamcheck = $_POST['spamcheck'];
$telefon = $_POST['telefon'];
$kategori = $_POST['kategori'];

if(trim($name) == '')
{ $error = '<div class="errormsg">Skriv in ditt namn.</div>'; }
else if(trim($email) == '')
{ $error = '<div class="errormsg">Skriv in din e-mail adress.</div>'; }
else if(!isEmail($email))
{ $error = '<div class="errormsg">Du har skrivit in en ogiltig e-mail adress. Försök igen!</div>'; }
if(trim($subject) == '')
{ $error = '<div class="errormsg">Skriv in ett ämne.</div>'; }
else if(trim($message) == '')
{ $error = '<div class="errormsg">Skriv in ditt meddelande.</div>'; }
else if(trim($spamcheck) == '')
{ $error = '<div class="errormsg">Svara på Antispam frågan!</div>'; }
else if(trim($spamcheck) != '5')
{ $error = '<div class="errormsg">Antispam: Felaktigt svar! 2 + 3 = ?</div>';}

if($error == '')
{
if(get_magic_quotes_gpc())
{ $message = stripslashes($message); }

// the email will be sent here
// make sure to change this to be your e-mail
$to      = "";

$subject = '[Sigster] : ' . $subject;

// the mail message ( add any additional information if you want )
$msg     = "Från : $name \r\nE-Mail : $email \r\nTelefon : $telefon \r\nKontakta mig genom : $kategori \r\nÄmne : $subject \r\n\n" . "Meddelande : \r\n$message";

mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
?>

【问题讨论】:

  • 确实,它适用于 header("Content-Type: text/html;charset=UTF-8");
  • 但它会将整个页面转换为 utf-8,我不想这样做;/

标签: php html utf-8


【解决方案1】:

尝试添加Content-Type 标头。换行:

mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

收件人:

$headers = array
(
    'Content-Type: text/html; charset="UTF-8";',
    'From: ' . $email,
    'Reply-To: ' . $email,
    'Return-Path: ' . $email,
);

mail($to, $subject, $msg, implode("\n", $headers));

你可以找到here一个发送utf-8邮件的函数。

【讨论】:

  • 我应该在哪里添加它?在 thr 脚本的顶部?
  • 只需用我的代码更改您的mail() 行。我编辑我的答案给你看(不确定缩进,你没有)
  • 我收到一条错误消息,解析错误:语法错误,意外';'
  • 我无法修复您的代码!这个想法是:保留你所有的代码(从 ),然后用我的代码删除你的 mail() 函数。
  • 仍然收到烦人的消息,我想用你的代码替换我的邮件吧?
猜你喜欢
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多