如何将PHP变量字符串转换为输出HTML?

时间:2013-06-13 19:46:44

标签: php string email variables output

我正在尝试创建自动回复邮件,内容是PHP变量。我希望它输出HTML代码,而现在它不是。

//Example:
$respondmessage = " Hello $fullname,
 We are confirming your Appointment today!
Please <a href="http://yourlink.com/">click here to confirm</a>!
";

输出:

Hello Your Name,
 We are confirming your Appointment today!
Please <a href="http://yourlink.com/">click here to confirm</a>!

在电子邮件中。有没有办法让电子邮件接受HTML代码?

3 个答案:

答案 0 :(得分:0)

假设您在PHP中使用内置的mail函数:

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

此示例来自:http://us2.php.net/manual/en/function.mail.php

重要的部分是$headers变量。

顺便说一句,正如安德鲁斯所提到的 - 你需要逃避你的报价:

$respondmessage = "Hello $fullname,
    We are confirming your Appointment today!
    Please <a href=\"http://yourlink.com/\">click here to confirm</a>!";

答案 1 :(得分:0)

您可以在代码中将以下行作为标题:

$ headers =“Content-type:text / html \ r \ n”;

这将帮助您创建HTML格式的电子邮件

答案 2 :(得分:0)

您必须将此标题包含在电子邮件脚本中

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

来自http://php.net/manual/en/function.mail.php示例#4

<?php

// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>