【问题标题】:PHP: Why email not support htmlPHP:为什么电子邮件不支持 html
【发布时间】:2014-12-24 17:42:02
【问题描述】:

我尝试从我的网站发送电子邮件,我收到了电子邮件但不支持 HTML。

这是我的代码:-

////////
        $headers = "From: $email\n" . "Reply-To: $email\n"; 
        $headers .= "Register: Super market\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .= "X-Priority: 3\r\n";
        $headers .= "X-Mailer: PHP". phpversion() ."\r\n";


        //$header = "From: $email\n" . "Reply-To: $email\n";
        $subject = "Register in Super market";
        $email_to = EMAIL;

        $emailMessage = "<b>NO :</b>  " . $aid."\n";    
        $emailMessage .= "<b>Name :</b>  " . $firstname.' '.$fathername.' '.$familyname. "\n"; 
        $emailMessage .= "<b>Tell :</b>  " . $tell . "\n\n";
        $emailMessage .= "<b>Fax :</b>  " . $fax . "\n\n";
        $emailMessage .= "<b>Email :</b>  " . $email . "\n\n";      

        //use php's mail function to send the email
        @mail($email_to, $subject ,$emailMessage ,$headers );  

更新:

大家好。

我在这里设置了question 并且solved 成功

但现在我需要添加一些文件作为附件。

所以,我有 2 个变量,将文件上传保存到数据库中,如下所示:-

$filenameword
$filenamezip

如何将此拖曳变量设置为电子邮件收件箱中的文件附件。

////////
    $headers = "From: $email\n" . "Reply-To: $email\n";  // See the s at the end of $headers
    $headers .= "Register: Super market\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n";


    //$header = "From: $email\n" . "Reply-To: $email\n";
    $subject = "Register in Super market";
    $email_to = EMAIL;

    $emailMessage = "<b>NO :</b>  " . $aid."\n";    
    $emailMessage .= "<b>Name :</b>  " . $firstname.' '.$fathername.' '.$familyname. "\n"; 
    $emailMessage .= "<b>Tell :</b>  " . $tell . "\n\n";
    $emailMessage .= "<b>Fax :</b>  " . $fax . "\n\n";
    $emailMessage .= "<b>Email :</b>  " . $email . "\n\n";      

    //use php's mail function to send the email
    mail($email_to, $subject ,$emailMessage ,$headers ); 

【问题讨论】:

  • 嘿,你错过了 和 标签..
  • 第 1 行必须是 $headers 而不是 $header

标签: php html email html-email


【解决方案1】:

您的代码中有错字。

您将标题字符串分配给$headers,但您将$header 传递给邮件函数。

另外请不要在函数前@。这是一种不好的做法。

////////
    $headers = "From: $email\n" . "Reply-To: $email\n";  // See the s at the end of $headers
    $headers .= "Register: Super market\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "X-Priority: 3\r\n";
    $headers .= "X-Mailer: PHP". phpversion() ."\r\n";


    //$header = "From: $email\n" . "Reply-To: $email\n";
    $subject = "Register in Super market";
    $email_to = EMAIL;

    $emailMessage = "<b>NO :</b>  " . $aid."\n";    
    $emailMessage .= "<b>Name :</b>  " . $firstname.' '.$fathername.' '.$familyname. "\n"; 
    $emailMessage .= "<b>Tell :</b>  " . $tell . "\n\n";
    $emailMessage .= "<b>Fax :</b>  " . $fax . "\n\n";
    $emailMessage .= "<b>Email :</b>  " . $email . "\n\n";      

    //use php's mail function to send the email
    mail($email_to, $subject ,$emailMessage ,$headers );  // See the s at the end of $headers

【讨论】:

  • 我不懂你,,你能编辑我的代码并把它放在你的答案中
  • 小补充:使用@ 的问题不仅仅是“坏习惯”,而是通过抑制错误,调试变成了在黑暗中搜索。还考虑到$headers .= 会抛出一个 E_NOTICE 顺便说一句,所以可能是错误报告也太低,错误抑制器与否。对@RashaSami 的建议:如果您要在更多地方发送电子邮件,或者需要更强大的解决方案,请考虑使用经过验证的 php 库,如 SwiftMailer 或 PHPMailer
  • 查看webcheatsheet.com/php/send_email_text_html_attachment.php 了解如何将文件附加到电子邮件的示例
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 2016-01-08
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
相关资源
最近更新 更多