【问题标题】:HTML within PHP not showing up as HTMLPHP 中的 HTML 未显示为 HTML
【发布时间】:2020-11-17 21:23:09
【问题描述】:

我修改了现有的 php 电子邮件文件以添加确认电子邮件部分以获取用于确认的 HTML 电子邮件,电子邮件有效但不显示 html,它显示代码。有人请让我知道我需要在这里修复什么? 我怀疑它与标题有关。 好久没做PHP了。


<?php
$toEmail = "my@gmail.com";
if(isset($_POST['txtfirstname']))
{
    $txtfirstname=$_POST['txtfirstname'];
    $txtlastname=$_POST['txtlastname'];
    $txtcompanyname=$_POST['txtcompanyname'];
    $txttitle=$_POST['txttitle'];
    $txtemail=$_POST['txtemail'];
    $txtphone=$_POST['txtphone'];
    $txtaddress=$_POST['txtaddress'];
    $txtcity=$_POST['txtcity'];
    $txtstate=$_POST['txtstate'];
    $txtzipcode=$_POST['txtzipcode'];
    $txtcountry=$_POST['txtcountry'];
    $txtshirtsize=$_POST['txtshirtsize'];
 
    $mimeHeaders .= "MIME-Version: 1.0\r\n";
    $mailHeaders .= "X-Priority: 3\r\n";
    $mailHeaders .= "X-Mailer: PHP". phpversion() ."\r\n";
    //$mailHeaders .= "Content-Type: text/html; charset=iso-8859-1\n";
  
    //headers
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    
    $headers = "From: " . $txtfirstname." ".$txtlastname . "<". $txtemail .">\r\n";
    $headers .= "Reply-To: ". $txtfirstname." ".$txtlastname . "<". $txtemail .">\r\n";
    $headers .= "Return-Path: ". $txtfirstname." ".$txtlastname . "<". $txtemail .">\r\n";
  
    $subject = "New Registration";
    $content = "New Registration \r\n";
    
    $content = $content. "First Name : ".$txtfirstname. "\r\n";
    $content = $content. "Last Name : ".$txtlastname . "\r\n";
    $content = $content. "Company : ".$txtcompanyname . "\r\n";
    $content = $content. "Title : ".$txttitle . "\r\n";
    $content = $content. "Email : ".$txtemail . "\r\n";
    $content = $content. "Phone : ".$txtphone ."\r\n";
    
    $content = $content. "City : ".$txtcity . "\r\n";
    $content = $content. "State : ".$txtstate . "\r\n";
    $content = $content. "Zip Code : ".$txtzipcode . "\r\n";
    $content = $content. "Country : ".$txtcountry . "\r\n";
    

    // Subject of confirmation email.

    $conf_subject = 'Confirmation Emailer';

    // Who should the confirmation email be from?
    $conf_sender = 'Me <mygmail.com>';

   
    // HTML email message
    $conf_message = '
        <html>
        <head>
        <title>HTML email</title>
        </head>
        <body>
        <table border="0" style="width: 500px; border-collapse: collapse; margin-left: auto; margin-right: auto;" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="width: 100%;"><img src="/banner.jpg" alt="Title" /></td> </tr> <tr> <td style="width: 100%;"><p><span><strong>Congratulations!</strong></span></p>
        <p>&nbsp;</p>
        <p>You have successfully registered &rsquo;</p> <p>&nbsp;</p> <p>Click below to add this event to your calendar-</p>
        <p><br /><a href="invite.ics"><u><strong>Add to your calendar</strong></u></a></p>
        <p><br />If you require any further assistance or have any queries, please reach out to us at&nbsp;<a href="mailto:me@gmail.com" rel="noopener" target="_blank"><b>me@<wbr />gmail.com</b></a></p>
        <p><br />We&rsquo;re looking forward to your presence at the event.</p>
        <p><br />23 th December 2020 | 8:45 AM .</p> 
        </td> </tr> 
        </tbody> </table>
        </body>
        </html>
        ';


    mail( $_POST['txtemail'], $conf_subject, $conf_message,'From: ' . $conf_sender );
    
    if(isset($_POST['chkgiveaway']) && $_POST['chkgiveaway']=="Yes")
    {
        $content = $content. "Want to receive Give aways: ".$_POST['chkgiveaway']. "\r\n";
        $content = $content. "Shirt Size: ".$_POST['txtshirtsize']. "\r\n";
        $content = $content. "Mailing Address : " . "\r\n";
        $content = $content. "Address : ".$_POST['txtaddressgiveaway'] . "\r\n";
        $content = $content. "City : ".$_POST['txtcitygiveaway'] . "\r\n";
        $content = $content. "State : ".$_POST['txtstategiveaway'] . "\r\n";
        $content = $content. "Zip Code : ".$_POST['txtzipcodegiveaway'] . "\r\n";
        $content = $content. "Country : ".$_POST['txtcountrygiveaway'] . "\r\n";
    
    }
    
    if(mail($toEmail, $subject, $content, $mailHeaders)) {
        $message = "<h4>Thank you, your details have been successfully submitted.</h4>";
        echo $message;      
    }
    
} else {
    echo "Invalid submission";
}
?>

【问题讨论】:

  • 你为什么要连接所有这些字符串? PHP 双引号字符串让您可以直接在其中粘贴 var。而不是$headers = "From: " . $txtfirstname." ".$txtlastname . "&lt;". $txtemail ."&gt;\r\n";,只需使用$headers = "From: $txtfirstname $txtlastname &lt;$txtemail&gt;\r\n";,并为$content$conf_message 字符串使用heredoc 字符串。
  • 你试过取消注释吗:` //$mailHeaders .= "Content-Type: text/html; charset=iso-8859-1\n";`???
  • 您的确认电子邮件不包含将内容类型设置为 HTML 的标题。您创建了$headers 变量(确实包含内容类型)时遇到了麻烦,但您在发送电子邮件时并未使用它。附:如果您使用诸如 PHPMailer 之类的库,它会使处理此类事情变得更加容易 - 您只需设置一个“html”标志,而不必担心手动创建确切的标题等,该库会处理所有乱七八糟的东西。
  • @A.Meshu $mailHeaders 未在确认电子邮件中使用,因此无关紧要。它只在另一封电子邮件中使用,当然不是 HTML 格式的。

标签: php html email


【解决方案1】:

你没有添加标题

mail( $_POST['txtemail'], $conf_subject, $conf_message, $headers);

【讨论】:

  • 你的回答有错别字。
  • 已更新。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 2011-07-04
相关资源
最近更新 更多