php邮件语法错误

时间:2015-12-30 05:35:07

标签: php html email

$mail_body = '<html>
<body style="background-color:#CCC; color:#000; font-family: Arial, Helvetica, sans-serif; line-height:1.8em;">
<h3><a href="http://www.grillontherock.com"><img src="http://i.imgur.com/OODKT9h.png" alt="GR" width="194" height="123" border="0"></a> 
</h3>
<p>Hello ' . $name . ',</p>
<p>You can make this out to be just like most any web page or design format you require using HTML and CSS.</p>
<p>Grill on the Rock </p>
<hr>
<p>To opt out of receiving this newsletter,  <a href="http://grillontherock.x10host.com/email/optout.php?e=' . $email . '">click here</a> and we will remove you from the listing immediately.</p>
</body>
</html>';

        $to = "$email";
        $subject = "Example Grill on the Rock Email";
        $from="info@grillontherock.com";

        $mail_result = mail($to, $subject, $mail_body, "From:".$from);

    }
    if($mail_result){
        echo "Email has been sent successfully";
    }

我在使用php和html发送电子邮件时遇到问题。

此代码完全正常,但我收到的电子邮件是

enter image description here

但是当我为html文件使用双引号时,php代码由于某种原因而变灰。

$mail_body = "<html>
<body style="background-color:#CCC; color:#000; font-family: Arial, Helvetica, sans-serif; line-height:1.8em;">
<h3><a href="http://www.grillontherock.com"><img src="http://i.imgur.com/OODKT9h.png" alt="GR" width="194" height="123" border="0"></a> 
</h3>
<p>Hello ' . $name . ',</p>
<p>You can make this out to be just like most any web page or design format you require using HTML and CSS.</p>
<p>Grill on the Rock </p>
<hr>
<p>To opt out of receiving this newsletter,  <a href="http://grillontherock.x10host.com/email/optout.php?e=' . $email . '">click here</a> and we will remove you from the listing immediately.</p>
</body>
</html>";

        $to = "$email";
        $subject = "Example Grill on the Rock Email";
        $from="info@grillontherock.com";

        $mail_result = mail($to, $subject, $mail_body, "From:".$from);

    }
    if($mail_result){
        echo "Email has been sent successfully";
    }

有点新的php和html,我目前找不到类似的问题。

此外,是否可以将电子邮件作为html表单并通过发送php文件带来该html?

3 个答案:

答案 0 :(得分:1)

您需要在邮件标题中将Content-Type设置为text/html

Content Type的示例标头:

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

然后

$mail_result = mail($to, $subject, $mail_body, $headers);

@edit。

还要检查#4的问题: http://php.net/manual/en/function.mail.php

答案 1 :(得分:0)

使用第二个html你有双引号的问题,因为你在html中使用双引号,试图逃避它们。试试这个:

或尝试以下链接: What is the difference between single-quoted and double-quoted strings in PHP?

答案 2 :(得分:0)

您需要在邮件标头中将Content-Type设置为text/html,以便将邮件作为html邮件发送。

示例标题:

$headers = "From: mymail@gmail.com\r\n";
$headers .= "Reply-To: no-reply@mymail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

最后,在您的内容之后,您需要发送如下邮件。

$mail_result = mail($to, $subject, $mail_body, $headers);

然后为你的双引号问题:这里你在双引号内使用双引号。如果那样你需要用“/”来逃避它。