PHP - 电子邮件不发送为HTML。为什么? [英] PHP - Email not sending as HTML. Why?
问题描述
我可以使用php从我的联系表格发送纯文本电子邮件,但我无法将内容作为HTML发送。以为我已经正确添加了标题,但显然还是有问题。
I am able to send plain text emails from my contact form using php, but I am not able to send the content as HTML. Thought I had added the headers correctly, but apparently there is still a problem.
这是我正在使用的脚本:
This is the script I am using:
<?php
$to = 'test@hotmail.com';
$subject = 'From your Web';
$email = $_REQUEST['email'] ;
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
//if "email" is filled out, send email
if (!trim($_REQUEST['name']) == "" )
{
if (!trim($_REQUEST['message']) == "" )
{
//send email
$name = $_REQUEST['name'] ;
$message = $_REQUEST['message'] ;
$mail = '
<html>
<body>
<table border=1>
<tr>
<td>Name:</td>
<td>'.$name.' </td>
</tr>
<tr>
<td>Email:</td>
<td>'.$email.'</td>
</tr>
<tr>
<td>Msg:</td>
<td>'.$message.'</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $mail, "From:" . $email, $headers);
//mail($to, $subject, "From: " . $name . "/" . $email . " Msg: ".$message, "From:" . $email);
echo 'Thank you!';
}
else{
echo 'No empty msg';
}
}
else{
echo 'This is not a name';
}
}
else{
echo 'No correct email';
}
?>
推荐答案
试试这个。代码未经测试。而且我已经用php编写过,所以我可以很容易地在那里修改一些语法。
try this. code is un-tested. and it's been eons since i've written in php so i could have easily mangled some syntax in there.
$tacos = "crunchy";
$to = "tacos4eva@email.com";
$subject = "tacos";
$mail = "the tacos are $tacos today.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: taco_master@email.com' . "\r\n";
$success = mail($to, $subject, $mail, $headers);
if ($success) echo "tacos were sent successfully";
else echo "tacos fell on the floor";
http://php.net/manual/en/function.mail.php
来自网页...
from the page...
发送邮件时,邮件必须包含From头。这可以使用additional_headers参数设置
,或者可以在
php.ini中设置默认值。
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
这篇关于PHP - 电子邮件不发送为HTML。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!