我不能做好PHP。但我可以阅读并理解这段代码的作用。
请忍受并帮助我。
这是我的HTML表单
<html>
<?php header("Content-type: text/html; charset=utf-8"); ?>
</head>
<body style="background-color:#F9F0EB;">
<form action="contact.php" method="post" accept-charset="utf-8">
<p>Ваше имя:<input type="text" name="name" /></p>
<p>E-mail:<input type="text" name="email" /></p>
<p>Тема:<input type="text" name="subject" /></p>
<p>Сообщение:<br />
<textarea name="message" rows="5" cols="45"> </textarea></p>
<p><input type="submit" value="Отправить"></p>
</form>
</body>
</html>
我的PHP代码是这样的:
<?php
/* Set e-mail recipient */
$myemail = "tang.adil@mail.ru";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Write a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your comments");
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your contact form has been submitted by:
Name: $name
E-mail: $email
Comments:
$message
End of message
";
/* Send the message using mail() function */
if(mail($myemail, $subject, $message))
{
$status = '<html style="background-color:#F9F0EB;">
<meta charset="utf-8"></meta>
<p>Спасибо за ваше сообщение!</p>
</html>';
}
else
{
$status = "There was a problem sending your feedback, please try again later.<br><br>";
}
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
<?php print $status; ?>
说清楚。我以utf-8格式保存了所有文件。当我发送给GMAIL时,我正确收到了电子邮件。
然而,它对Yandex或Mail.ru不起作用。
PHP在发送之前发送但不编码。我尝试了很多脚本,但没有一个工作。可能是因为我根本不认识php。
答案 0 :(得分:1)
首先:在任何html之前使用php标头代码 - 如果你愿意的话。我仍然不知道你为什么要使用header()函数来设置html编码。输入元标记就足够了:
<?php header("Content-type: text/html; charset=utf-8"); ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="background-color:#F9F0EB;">
<form action="contact.php" method="post" accept-charset="utf-8">
<p>Ваше имя:<input type="text" name="name" /></p>
<p>E-mail:<input type="text" name="email" /></p>
<p>Тема:<input type="text" name="subject" /></p>
<p>Сообщение:<br />
<textarea name="message" rows="5" cols="45"> </textarea></p>
<p><input type="submit" value="Отправить"></p>
</form>
</body>
</html>
但这一切都适用于HTML页面。对于编码电子邮件,请使用以下代码:
// set header
$headers = 'Content-type: text/html; charset=utf-8' . "\r\n";
// Send email
mail($to, $subject, $message, $headers);