PHP 电子邮件转 HTML 问题

发布于 2024-12-01 13:58:31 字数 1846 浏览 4 评论 0原文

我正在尝试使用 PHP 发送电子邮件,但收到一个奇怪的错误。

在 PHP 中,我有:

<?php

mail($email_to, $email_subject, $headers, $message);

$email_to = '[email protected]';

$email_subject = 'Hello World';

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['test1']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['test2']) . "</td></tr>";
$message .= "</table>";
$message .= '</body></html>';

   if(mail($email_to, $email_subject, $headers, $message)){
        echo 'sent';    
    }else{
        echo 'failed';
    }
?>

有了这个,我收到纯文本:

<html><body><table rules="all" style="border-color: #666;" cellpadding="10"><tr><td>
<strong>Email:</strong> </td><td>[email protected]</td></tr><tr><td>
<strong>Message:</strong> </td><td>hello world</td></tr></table></body></html>

From: [email protected]
MIME-Version: 1.0
Content-Type: text/html; charset=ISO-8859-1

谁能发现我做错了什么?我在两台不同的服务器上进行了测试,结果相同。

I am trying to send an email with PHP but I get a weird error.

In the PHP I have:

<?php

mail($email_to, $email_subject, $headers, $message);

$email_to = '[email protected]';

$email_subject = 'Hello World';

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['test1']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['test2']) . "</td></tr>";
$message .= "</table>";
$message .= '</body></html>';

   if(mail($email_to, $email_subject, $headers, $message)){
        echo 'sent';    
    }else{
        echo 'failed';
    }
?>

With this I receive as a plain text:

<html><body><table rules="all" style="border-color: #666;" cellpadding="10"><tr><td>
<strong>Email:</strong> </td><td>[email protected]</td></tr><tr><td>
<strong>Message:</strong> </td><td>hello world</td></tr></table></body></html>

From: [email protected]
MIME-Version: 1.0
Content-Type: text/html; charset=ISO-8859-1

Can anyone spot what I'm I doing wrong? I tested this on 2 different servers with the same results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生漫 2024-12-08 13:58:31

应该是

 if(mail($email_to, $email_subject, $message, $headers)){ ...

You've got $message and $headers in the bad order.

It should be

 if(mail($email_to, $email_subject, $message, $headers)){ ...

You've got $message and $headers in the wrong order.

情未る 2024-12-08 13:58:31

您混淆了 $message 和 $headers 的位置。试试这个 mail($email_to, $email_subject, $message, $headers)

http://php.net/manual/en/function.mail.php

You have mixed up the position of the $message and $headers. Try this instead mail($email_to, $email_subject, $message, $headers)

http://php.net/manual/en/function.mail.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文