简体   繁体   English

PHP mail()函数,将邮件作为HTML代码发送

[英]PHP mail() function,sending mail as HTML code

I am facing a problem where when I send mail using mail() , it sends the HTML code in the mail. 我遇到一个问题,当我使用mail()发送邮件时,它会在邮件中发送HTML代码。 This is my code: 这是我的代码:

<?php
$message = "<table width='100%' border='0' cellspacing='0' cellpadding='3'>
  <tr>
    <td colspan=2>Dear ".$_POST['fname']."&nbsp;".$_POST['lname'].",</td>
  </tr>
   <tr>
    <td colspan=2>Thank you for contacting us.We will get back to you in next 48 hrs.Your Contacts Details are as follows:</td>
  </tr>
  <tr>
    <td width='28%' >Title</td>
    <td width='72%'>".$_POST['title']."</td>
  </tr>
  <tr>
    <td>Email</td>
    <td>".$_POST['email']."</td>
  </tr>
  <tr>
    <td>Your Message</td>
    <td>".$_POST['message']."</td>
  </tr>
  <tr>
    <td>How did you hear about us? </td>
    <td>".$checkBox."</td>
  </tr>
</table>
";  

$subject = "ContactUs";
$headers .= "From: ".$from_mail." \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

mail($_POST["coach"], $subject, $message, $headers);

?>

And the output which I am getting in my e-mail looks like this: 我在电子邮件中收到的输出如下所示:

<table width='100%' border='0' cellspacing='0' cellpadding='3'>
  <tr>
    <td colspan=2>Dear Test User,</td>
  </tr>
   <tr>
    <td colspan=2>Thank you for contacting us.Your Contacts Details are as follows:</td>
  </tr>
  <tr>
    <td width='28%' >Title</td>
    <td width='72%'>Mr.</td>
  </tr>
  <tr>
    <td>Email</td>
    <td>test@example.com</td>
  </tr>
  <tr>
    <td>Your Message</td>
    <td>Test Message</td>
  </tr>
  <tr>
    <td>How did you hear about us? </td>
    <td>www.example.com</td>
  </tr>
</table>

I don't know why is this happening. 我不知道为什么会这样。 Any suggestions will be appreciated. 任何建议将不胜感激。

try this 尝试这个

$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";

also try : PHPMailer 也尝试: PHPMailer

Use 采用

html_entity_decode() html_entity_decode()

eg. mail($_POST["coach"], $subject, html_entity_decode($message), $headers);

add the following code in header portion 在标题部分添加以下代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM