HTML未在电子邮件中呈现

时间:2015-04-30 14:45:42

标签: php html

$message = 'What are the other benefits to saying "nah" that you can think of? - ' . $_POST['other-benefits'] . '<br>' .
       'What are you pledging to do? - ' . $_POST['what-pledge'] . '</p>';

我试图在php中的每一行之后暂停但是使用嵌入式HTML。当我发送电子邮件(功能未包括在内)时,它不会呈现中断。它实际上只是输入字符串。有没有办法纠正这个?

2 个答案:

答案 0 :(得分:3)

鉴于缺乏有关该问题的信息,我只能假设问题是电子邮件的标题尚未设置为发送HTML电子邮件。

你必须按照以下方式做点什么:

// Setup email content.
$to = "example@email.com";
$subject = "Example HTML email";

$message = 'What are the other benefits to saying "nah" that you can think of? - ' . $_POST['other-benefits'] . '<br>' .
       'What are you pledging to do? - ' . $_POST['what-pledge'] . '</p>';

// Setup headers.
$headers = "From: from@email.com\r\n";
$headers .= "Reply-To: replyto@email.com\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";

// This next one is where the magic happens.
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

// Send!
mail($to, $subject, $message, $headers);

请参阅:https://css-tricks.com/sending-nice-html-email-with-php/

答案 1 :(得分:2)

要发送包含html的电子邮件,请特别注意以下标题:

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

示例代码:

$otherbenefits = $_POST['other-benefits'];
$whatpledge = $_POST['what-pledge'];

$email = <<< LOL
<html> 
  <body> 
    <p> What are the other benefits to saying "nah" that you can think of? - $otherbenefits <br>  What are you pledging to do? - $whatpledge </p>
  </body>
</html>
LOL;

$emailaddress = "personsemail@website.com";
$subject = "Test Email";
$headers = "From: noreply@server.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


mail($emailaddress, $subject, $email, $headers);

有关更多信息,请访问PHP.net,了解mail()功能以及如何以HTML格式发送电子邮件。