php - php 邮件中的 html 和 css 代码不可读

标签 php html css email

我真的很难使用 php mail() 函数制作一封带有附件的电子邮件,我已经成功附加了我的文件,但消息只显示了 HTML 和 CSS 代码,而不是像这样的电子邮件正文消息:

enter image description here

这是我的代码:

 $htmlbody = '
                    <!doctype html>
                    <html>
                    <head>
                        <title>Invoice</title>

                        <style>
                        .invoice-box{
                            background-color: rgb(244, 244, 244);
                            max-width:800px;
                            margin:auto;
                            padding:30px;
                            border:1px solid #eee;
                            box-shadow:0 0 10px rgba(0, 0, 0, .15);
                            font-size:16px;
                            line-height:24px;
                            font-family:"Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
                            color:#555;
                            border-color: #fe7100;
                            border-width: medium;
                        }

                        .invoice-box table{
                            width:100%;
                            line-height:inherit;
                            text-align:left;
                        }

                        .invoice-box table td{
                            padding:5px;
                            vertical-align:top;
                        }

                        .invoice-box table tr td:nth-child(2){
                            text-align:right;
                        }

                        .invoice-box table tr.top table td{
                            padding-bottom:20px;
                        }

                        .invoice-box table tr.top table td.title{
                            font-size:45px;
                            line-height:45px;
                            color:#333;
                        }

                        .invoice-box table tr.information table td{
                            padding-bottom:40px;
                        }

                        .invoice-box table tr.heading td{
                            background:#33665e;
                            border-bottom:1px solid #ddd;
                            font-weight:bold;
                            color: white;
                        }

                        .invoice-box table tr.details td{
                            padding-bottom:5px;
                        }

                        .invoice-box table tr.item td{
                            border-bottom:1px solid #eee;
                        }

                        .invoice-box table tr.item.last td{
                            border-bottom:none;
                        }

                        .invoice-box table tr.total td:nth-child(2){
                            border-top:2px solid #eee;
                            font-weight:bold;
                        }

                        @media only screen and (max-width: 600px) {
                            .invoice-box table tr.top table td{
                                width:100%;
                                display:block;
                                text-align:center;
                            }

                            .invoice-box table tr.information table td{
                                width:100%;
                                display:block;
                                text-align:center;
                            }
                        }

                        .block-garis{
                          width: 100%;
                          height: 3px;
                          background-color: #fe7100;
                        }
                        </style>
                    </head>

                    <body>
                        <div class="invoice-box">
                            <table cellpadding="0" cellspacing="0">
                                <tr class="top">
                                    <td colspan="2">
                                        <table>
                                            <tr>
                                                <td class="title">
                                                    <img src="https://4travelo.com/logo.png" style="width:100%; max-width:200px;">
                                                </td>

                                                <td>
                                                    Invoice #: '.$id.'<br>
                                                    Created: '.$tglbook.'<br>
                                                    Due: '.$timeexpiry.'
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>

                                <tr>
                                  <td colspan="2"><div class="block-garis"></div></td>
                                </tr>

                                <tr class="information">
                                    <td colspan="2">
                                        <table>
                                            <tr>
                                                <td>
                                                    4travelo.<br>
                                                    Jl. Kebonsari Menanggal 2B<br>
                                                    Surabaya.
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr class="heading">

                                    <td>
                                        Keterangan Hotel
                                    </td>

                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>

                                <tr class="details">
                                    <td>
                                        Nama Hotel
                                    </td>

                                    <td>
                                        '.$hotelname.'
                                    </td>
                                </tr>
                                <tr class="details">
                                    <td>
                                        Alamat Hotel
                                    </td>

                                    <td>
                                        '.$alamat.'
                                    </td>
                                </tr>
                                <tr class="details">
                                    <td>
                                        Checkin
                                    </td>

                                    <td>
                                       '.$checkin.'
                                    </td>
                                </tr>
                                <tr class="details">
                                  <td>
                                      Checkout
                                  </td>

                                  <td>
                                     '.$checkout.'
                                  </td>
                                </tr>

                                <tr class="heading">
                                    <td>
                                        Guest
                                    </td>

                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>

                                <tr class="item">
                                    <td>
                                        Guest Name
                                    </td>

                                    <td>
                                        '.$paxesname.'
                                    </td>
                                </tr>

                                <tr class="item">
                                    <td>
                                        Type Kamar
                                    </td>

                                    <td>
                                       '.$room.'
                                    </td>
                                </tr>

                                <tr class="item">
                                    <td>
                                        Include BF/Net
                                    </td>

                                    <td>
                                        '.$meal.'
                                    </td>
                                </tr>

                                <tr class="item">
                                    <td>
                                        Dewasa
                                    </td>

                                    <td>
                                        '.$adult.'
                                    </td>
                                </tr>

                                <tr class="item">
                                    <td>
                                        Anak - Anak
                                    </td>

                                    <td>
                                        '.$child.'
                                    </td>
                                </tr>

                                <tr class="heading">
                                    <td>
                                        Total
                                    </td>

                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>

                                 <tr class="item">
                                    <td>
                                        Total
                                    </td>

                                    <td>
                                        <b>Rp '.$payment.'</b>
                                    </td>
                                </tr>
                                <tr>
                                  <td>&nbsp;</td>
                                  <td>&nbsp;</td>
                                </tr>

                                <tr>
                                  <td>&nbsp;</td>
                                  <td>&nbsp;</td>
                                </tr>

                                <tr>
                                  <td>&nbsp;</td>
                                  <td>&nbsp;</td>
                                </tr>
                                <tr style="font-size:10px; padding-top:50px;">
                                  <td><b>Hotel cancelation policy :
                                  <br>1. Pemesanan non refundable<br>
                                  2. Tanggal inap & tipe kamar tidak dapat dirubah</b>
                                  </td>
                                  <td><b>our partner : </b> <br><img src="https://4travelo.com/hb.png" alt=""></td>
                                </tr>

                                <tr>
                                  <td>&nbsp;</td>
                                  <td style=""></td>
                                </tr>
                            </table>
                        </div>

                    </body>
                    </html>';
        $to = $email; //Recipient Email Address
        $subject = "Test email with attachment"; //Email Subject
        $headers = "From: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e1e4ede9eec0b4f4f2e1f6e5ecefaee3efed" rel="noreferrer noopener nofollow">[email protected]</a>\r\nReply-To: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7110151c181f314505031007141d1e5f121e1c" rel="noreferrer noopener nofollow">[email protected]</a>";
        $random_hash = md5(date('r', time()));
        $headers .= "\r\nContent-Type: multipart/mixed; 
        boundary=\"PHP-mixed-".$random_hash."\"";
        // Set your file path here
       $attachment = chunk_split(base64_encode(file_get_contents('/home/travelo/public_html/4travelo.com/triomacan/4travelo_beta-2.0/document/'.$invoice.'.pdf'))); 


        //define the body of the message.
        $message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative; 
        boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
        $message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; 
        charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";


        //Insert the html message.
        $message .= "$htmlbody";
        $message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";


        //include attachment
        $message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip; 
        name=\"invoice.pdf\"\r\n"."Content-Transfer-Encoding: 
        base64\r\n"."Content-Disposition: attachment\r\n\r\n";

        $message .= $attachment;
        $message .= "/r/n--PHP-mixed-$random_hash--";


        //send the email
        $mail = mail( $to, $subject , $message, $headers, '-f <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcddd8d1d5d2fc88c8ceddcad9d0d392dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>' );

        echo $mail ? "Mail sent" : "Mail failed";

有人可以帮我吗?

最佳答案

而不是

$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain; 

尝试

$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/html; 

关于php - php 邮件中的 html 和 css 代码不可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42019659/

相关文章:

javascript - 在 FF 上使用 native Javascript 进行样式属性操作

php - 通过 DateTime 类正确格式化 iCalender 文件的日期字符串?

javascript - vue.js/laravel 无法设置 null 的属性 'ondataavailable'

javascript - 如何从循环输入中扣除总数 type=number

php - 如何在常规登录网站上集成 facebook 注册/注册?

javascript - 保持无序列表对齐

css - 将标题、图像和 <ol> 的第一个 <li> 放在一起

html - 通过带有 data 属性的 CSS 添加内容

html - 倾斜限制下的 div 元素管理

javascript - jPlayer 进度条旋钮