我的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 = "[email protected]";
/* 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。
第一:如果需要,在任何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);
此处的更多信息:http : //php.net//manual/en/function.mail.php
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句