【问题标题】:Sending HTML email with PHP使用 PHP 发送 HTML 电子邮件
【发布时间】:2012-04-05 23:22:05
【问题描述】:

我正在尝试使用 PHP 发送一封 html 电子邮件,并且它一直以文本形式出现。所有值都是从 php 正确生成的,它只是电子邮件中的文本。代码如下:

    $to='xxxxxxx@xxxxxxx.com';
    $from = 'xxxxxxx@xxxxxxxx.com';
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers = "From: $from \r\n";
    $subject = "Print Run: " . $run . " is ordered";
    $body ="<html>
            <head>
            </head>
            <body>
            <table>
                <tr>
                    <th>Company</th>
                    <th>Quantity</th>
                    <th>Size</th>
                    <th>Date Added</th>
                    <th>
                    </th>
                </tr>";
            for($i = 0; $i < $arraySize; $i++){     
                $body .= "<tr><td>" . $companyName[$i] . "</td><td>" . $quantity[$i] . "</td><td>" . $cardSize[$i] . "</td><td>" . $dateAdded[$i] . "</td></tr>";
            }
            $body .= "<tr>
                        <td style=\"font-weight:bold; border-style:solid; border-top-width:1px;\">Totals</td>
                        <td style=\"font-weight:bold; border-style:solid; border-top-width:1px;\">" . $totals . "</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        </tr>
                        </table>
                        </body>
                        </html>";

    mail($to,"Print Run: " . $run . " is ordered",$body,$headers);

【问题讨论】:

  • 你检查过是不是你的邮件客户端设置造成的吗?

标签: php html email


【解决方案1】:

您覆盖了三行中最后一行的标题:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $from \r\n";

应该是(注意点):

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from \r\n";

【讨论】:

  • 发现它并不难,真的:)
  • 我看到的另一个问题是“\r\n”。这在某些系统上会失败,应该用 PHP_EOL 替换。
  • @Matt H:您的评论具有误导性,您应该使用 RFC 和 PHP 源代码来支持它。 "\r\n" 绝对正确,另见Which line break in php mail header, \r\n or \n?。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 2015-02-23
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多