PHP - 电子邮件未作为 HTML 发送。为什么?
PHP - Email not sending as HTML. Why?
我可以使用 php 从我的联系表单发送纯文本电子邮件,但我无法将内容作为 HTML 发送。以为我已经正确添加了 headers,但显然还有问题。
这是我正在使用的脚本:
<?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 编写以来已经过了很久,所以我可以很容易地破坏其中的一些语法。
$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
从页面...
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 发送。以为我已经正确添加了 headers,但显然还有问题。
这是我正在使用的脚本:
<?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 编写以来已经过了很久,所以我可以很容易地破坏其中的一些语法。
$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
从页面...
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);
?>