无法使用PHP邮件()发送HTML邮件 [英] Failed to send HTML mails using PHP mail()

查看:84
本文介绍了无法使用PHP邮件()发送HTML邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  $ message =< html>< body>; 
$ message。=< table rules ='all'style ='border-color:#666;'cellpadding ='10'>;
$ message。=< tr style ='background:#eee;'>< td>< strong>名称:< / strong>< / td>< / tr>
$ message。=< tr>< td>< strong>电子邮件:< / strong>< / td>< / tr>
$ message。=< / table>;
$ message。=< / body>< / html>;

$ to ='me@gmail.com';

$ subject ='Website Change Reqest';

$ headers =From:。 $电子邮件。 \r\\\
;
$ headers。=回覆:。 $电子邮件。 \r\\\
;
$ headers。=MIME-Version:1.0\r\\\
;
$ headers。=Content-Type:text / html; charset = ISO-8859-1\r\\\
;

if(mail($ to,$ subject,$ message,$ headers)){
echo'您的消息已发送。
} else {
echo'发送电子邮件时出现问题。
}

这是我的电子邮件看起来像...: p>

 回复:me@gmail.com 

MIME版本:1.0

Content-Type:text / html; charset = ISO-8859-1
Message-Id:< 20110703234551.A9D6153DAB@apache10.hostbasket.com>
日期:2011年7月4日,星期一01:45:51 +0200(CEST)

< html>< body>< table rules =allstyle =颜色:#666; cellpadding =10>< tr style ='background:#eee;'>< td>< strong>名称:< / strong> < / TD>< / TR>< TR>< TD><强>电子邮件:其中/强> < / td>< / tr>< tr>< td>< strong>变更类型:< / strong> < / TD>< / TR>< TR>< TD><强>紧急:其中/强> < / td>< / tr>< tr>< td>< strong>要更改的URL(主):< / strong> < / td>< / tr>< tr>< td>< strong>新内容:< / strong> < / TD>< / TR>< /表>< /体>< / HTML>

我做错了什么?

解决方案

我们看不到变量 $ email 的设置,但是我猜测可能会有一个额外的换行符您的 $ email 变量的结尾。这可能会在 From:标题之后和回复:之前放入两个换行符,其中表示消息正文的开始和标题的完成。



尝试:

  $ email = trim($ email); 

在构建邮件之前。由于在 Reply-to 标题之后似乎有额外的换行符,我的情况在 $ email



更新



还尝试更改换行符到代码将运行的系统上的PHP的本机格式。这是通过用 PHP_EOL


$ b $替换 \r\\\
b

  $ headers =From:。 $电子邮件。 PHP_EOL; 
$ headers。=回覆:。 $电子邮件。 PHP_EOL;
$ headers。=MIME-Version:1.0。 PHP_EOL;
$ headers。=Content-Type:text / html; charset = ISO-8859-1。 PHP_EOL;


Strange things happening to me. I'm trying to send a HTML mail, using the php mail() function, but no luck here. Even when I copy/paste a piece of code literally, it doesn't work. What am I doing wrong? Here is the piece of code I use:

        $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.';
        }

And this is what my e-mail looks like...:

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>

What do I do wrong?

解决方案

We can't see where the variable $email was set, but I'm guessing that there might be an extra line break at the end of your $email variable. This would have the effect of putting in two linebreaks after the From: header and before the Reply-to: , which signals the start of the body of the message and the completion of the headers.

Try:

$email = trim($email);

before constructing the message. Since there appears to be an extra line break after the Reply-to header as well, my case is even stronger for an extra break in $email.

UPDATE

Also try changing the linebreaks to PHP's native format on the system where the code will run. This is done by replacing \r\n with 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;

这篇关于无法使用PHP邮件()发送HTML邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆