无法使用PHP邮件发送HTML邮件()

时间:2011-07-04 00:12:35

标签: php html email

奇怪的事发生在我身上。我正在尝试使用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>

我做错了什么?

3 个答案:

答案 0 :(得分:9)

我们无法看到变量$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;

答案 1 :(得分:1)

这是在php中发送html电子邮件的有用链接

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

答案 2 :(得分:0)

我建议你使用邮件程序类。它们为您提供了使用smtp auth的可能性,因此每封邮件都会通过。几个例子: