【问题标题】:Failed to send HTML mails using PHP mail()使用 PHP mail() 发送 HTML 邮件失败
【发布时间】:2011-09-27 19:45:16
【问题描述】:

奇怪的事情发生在我身上。我正在尝试使用 php mail() 函数发送 HTML 邮件,但这里没有运气。即使我从字面上复制/粘贴一段代码,它也不起作用。我究竟做错了什么?这是我使用的一段代码:

        $message = "<html><body>";
        $message .= "<table rules='all' style='border-color: #666;' cellpadding='10'>";
        $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td></tr>";
        $message .= "<tr><td><strong>Email:</strong> </td></tr>";
        $message .= "</table>";
        $message .= "</body></html>";       

        $to = 'me@gmail.com';

        $subject = 'Website Change Reqest';

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

        if (mail($to, $subject, $message, $headers)) {
          echo 'Your message has been sent.';
        } else {
          echo 'There was a problem sending the email.';
        }

这就是我的电子邮件的样子...:

Reply-To: me@gmail.com

MIME-Version: 1.0

Content-Type: text/html; charset=ISO-8859-1
Message-Id: <20110703234551.A9D6153DAB@apache10.hostbasket.com>
Date: Mon, 4 Jul 2011 01:45:51 +0200 (CEST)

<html><body><table rules="all" style="border-color: #666;" cellpadding="10"><tr style='background: #eee;'><td><strong>Name:</strong> </td></tr><tr><td><strong>Email:</strong> </td></tr><tr><td><strong>Type of Change:</strong> </td></tr><tr><td><strong>Urgency:</strong> </td></tr><tr><td><strong>URL To Change (main):</strong> </td></tr><tr><td><strong>NEW Content:</strong> </td></tr></table></body></html>

我做错了什么?

【问题讨论】:

  • 您使用哪种操作系统?
  • Mac OS X 10.7 和我的电子邮件客户端是 Sparrow

标签: php html email


【解决方案1】:

我们看不到变量$email 的设置位置,但我猜$email 变量的末尾可能有一个额外的换行符。这将产生在 From: 标头之后和 Reply-to: 之前放置两个换行符的效果,这表示消息正文的开始和标头的完成。

试试:

$email = trim($email);

在构建消息之前。由于在 Reply-to 标头之后似乎还有一个额外的换行符,所以我的情况更适合在 $email 中额外的换行符。

更新

还可以尝试在将运行代码的系统上将换行符更改为 PHP 的本机格式。这是通过将\r\n 替换为PHP_EOL 来完成的

    $headers = "From: " . $email . PHP_EOL;
    $headers .= "Reply-To: ". $email . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-Type: text/html; charset=ISO-8859-1" . PHP_EOL;

【讨论】:

  • sendmail 是否会在邮件头发送带有额外换行符的邮件?
  • @Jarred Farrish 是的,我相信mail() 将未修改的标头块传递给 sendmail,然后是消息正文,并且 sendmail 会在假定它们格式正确的情况下发送它们。这就是邮件注入存在问题的原因。 phpsecure.info/v2/article/MailHeadersInject.en.php
  • 不错的链接。就换行而言,我一直认为 sendmail 真的很敏感。
  • 额外的$email = trim($email) 没有成功...仍然收到与之前相同的非 HTML 消息...
【解决方案2】:

这是一个用 php 发送 html 电子邮件的有用链接

http://blog.twigahost.com/how-to-send-html-email-with-php/

【讨论】:

    【解决方案3】:

    我建议您使用邮件程序类。它们使您可以使用 smtp auth,因此每封邮件都会通过。几个例子:

    【讨论】:

      猜你喜欢
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-19
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多