简体   繁体   中英

Send HTML email message in PHP

I'm trying to send an HTML formatted invoice but it is sending the message as plain text rather than formatted HTML.

The code is:

$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from('sample@email.com', 'Sample');
$this->email->to('sample2@email.com');
$this->email->cc('sample3@email.com');
$this->email->subject('Sample Test');
$this->email->message($message);
$this->email->send();
echo $this->email->print_debugger();

and the content:

$message ='
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <style>
<![CDATA[
        body, td { color:#a6a7a3; font-size: 14px; font-family:Arial; font-weight:normal; text-decoration:none; }
        a { color:#a6a7a3; font-weight:normal; text-decoration:none; }
        table td { border-collapse: collapse;}
]]>
    </style>
</head>
<body>
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <td>
                            <!-- // Begin Template Header \ -->

                            <table>
                                <tr>
                                    <td><!--IMAGE--></td>
                                </tr>
                            </table><!-- // End Template Header \ -->
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <!-- // Begin Template Body \ -->

                            <table>
                                <tr>
                                    <td>
                                        <!-- // Begin Module: Standard Content \ -->

                                        <table>
                                            <tr>
                                                <td>
                                                    <div>
                                                        <strong style="font-size:14px;font-family:Arial;">Dear Sample,</strong><br />
                                                        <br />
                                                        Thank you for being with us. <!-- Start Transaction Information -->

                                                        <table>
                                                            <tr>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td><strong>Sample</strong></td>
                                                            </tr>
                                                        </table>
                                                        <table>
                                                            <tr>
                                                                <td></td>
                                                            </tr>
                                                            <tr>
                                                                <td>Item Name</td>
                                                                <td>Quantity</td>
                                                                <td>Item Price</td>
                                                                <td>Item Code</td>
                                                                <td>Shipping</td>
                                                            </tr>
                                                            <tr>
                                                                <td>'.$item.'</td>
                                                                <td>'.$quantity.'</td>
                                                                <td>'.$price.'</td>
                                                                <td>'.$code.'</td>
                                                                <td>'.$shipping.'</td>
                                                            </tr>
                                                        </table><!-- End Transaction Information -->
                                                    </div>
                                                </td>
                                            </tr>
                                        </table><!-- // End Module: Standard Content \ -->
                                    </td>
                                </tr>
                            </table><!-- // End Template Body \ -->
                        </td>
                    </tr>
                </table><br />
            </td>
        </tr>
    </table><br />
</body>
</html>';
$this->email->set_mailtype("html");

Here you might also find this useful.. I was bored. You'll get much better device compatibility with this code..

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta content="telephone=no" name="format-detection" /><!-- ios -->
    <meta content="on" http-equiv="cleartype" /><!-- Internet explorer -->

    <style>
<![CDATA[
    html,body,.ReadMsgBody, .ExternalClass,.ExternalClass * { background-color:#ffffff;width:100%!important;line-height:100%;margin:0;-webkit-text-size-adjust:100%; }
    table,table td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }
    img { display:block;border:0 none;outline:none;height:auto;line-height:100%;margin:0;padding:0;text-decoration:none;-ms-interpolation-mode:bicubic; }
    .container { width:600px; }
    #outlook a { padding:0; }
    .yshortcuts,.yshortcuts a,.yshortcuts a:link,.yshortcuts a:visited,.yshortcuts a:hover,.yshortcuts a span { color:#a6a7a3;text-decoration:none!important;border-bottom:none!important;background:none!important;}
]]>
    </style>

    <title></title>
</head>

<body bgcolor="#FFFFFF" style="min-width:100%;margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;padding-top:0;padding-bottom:0;padding-left:0;padding-right:0;color:#a6a7a3;font-family:Arial,sans-serif;">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <!-- // Begin Template Header \ -->

                            <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td><!--IMAGE (style-display:block;!!)--></td>
                                </tr>
                            </table><!-- // End Template Header \ -->
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <!-- // Begin Template Body \ -->

                            <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td>
                                        <!-- // Begin Module: Standard Content \ -->

                                        <table border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td style="color:#a6a7a3;font-size:14px;font-family:Arial,sans-serif;text-align:left;">
                                                    <b>Dear Sample,</b><br />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="color:#a6a7a3;font-size:14px;font-family:Arial,sans-serif;text-align:left;">
                                                    Thank you for being with us. <!-- Start Transaction Information -->
                                                </td>
                                            </tr>
                                            <tr>
                                                <td style="color:#a6a7a3;font-size:14px;font-family:Arial,sans-serif;text-align:left;">
                                                    <b>Sample</b>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table border="0" cellpadding="3" cellspacing="0"><tr><td height="3"><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr></table></td></tr></table>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <table border="0" cellpadding="0" cellspacing="0">
                                            <tr>
                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Item Name</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Quantity</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Item Price</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Item Code</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">Shipping</td>
                                            </tr>
                                            <tr>
                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$item.'</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$quantity.'</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$price.'</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$code.'</td>

                                                <td style="text-align:left;font-family:Arial,sans-serif;font-size:14px;color:#a6a7a3;">'.$shipping.'</td>
                                            </tr>
                                        </table><!-- End Transaction Information -->
                                    </td>
                                </tr>
                            </table><!-- // End Module: Standard Content \ -->
                        </td>
                    </tr>
                </table><!-- // End Template Body \ -->
            </td>
        </tr>
    </table>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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