Javascript 编写电子邮件php,转义html字符

Javascript 编写电子邮件php,转义html字符,javascript,php,html,css,Javascript,Php,Html,Css,我有一个电子邮件的html,我想用PHP发送它。但将其保存在变量中会给我带来很多错误 我如何在php变量中保存如此多的html和css,有什么方法吗 以下是代码,它不完整: <?php $corpo='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://

我有一个电子邮件的html,我想用PHP发送它。但将其保存在变量中会给我带来很多错误

我如何在php变量中保存如此多的html和css,有什么方法吗

以下是代码,它不完整:

<?php 
  $corpo='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <!-- NAME: 1 COLUMN - BANDED -->
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>*|MC:SUBJECT|*</title>

        <style type="text/css">
            body,#bodyTable,#bodyCell{
                height:100% !important;
                margin:0;
                padding:0;
                width:100% !important;
            }
            table{
                border-collapse:collapse;
            }
            img,a img{
                border:0;
                outline:none;
                text-decoration:none;
            }
            h1,h2,h3,h4,h5,h6{
                margin:0;
                padding:0;
            }
            p{
                margin:1em 0;
                padding:0;
            }
            a{
                word-wrap:break-word;
            }
            .ReadMsgBody{
                width:100%;
            }
            .ExternalClass{
                width:100%;
            }
            .ExternalClass,.ExternalClass p,.ExternalClass span,.ExternalClass font,.ExternalClass td,.ExternalClass div{
                line-height:100%;
            }
            table,td{
                mso-table-lspace:0pt;
                mso-table-rspace:0pt;
            }
            #outlook a{
                padding:0;
            }
            img{
                -ms-interpolation-mode:bicubic;
            }
            body,table,td,p,a,li,blockquote{
                -ms-text-size-adjust:100%;
                -webkit-text-size-adjust:100%;
            }
            #bodyCell{
                padding:0;
            }
            .mcnImage{
                vertical-align:bottom;
            }
            .mcnTextContent img{
                height:auto !important;
            }
        /*
        @tab Page
        @section background style
        @tip Set the background color and top border for your email. You may want to choose colors that match your companys branding.
        */
            body,#bodyTable{
                /*@editable*/background-color:#F2F2F2;
            }
            h3{
                /*@editable*/color:#606060 !important;
                display:block;
                /*@editable*/font-family:Helvetica;
                /*@editable*/font-size:18px;
                /*@editable*/font-style:normal;
                /*@editable*/font-weight:bold;
                /*@editable*/line-height:125%;
                /*@editable*/letter-spacing:-.5px;
                margin:0;
                /*@editable*/text-align:left;
            }
        /*
        @tab Page
        @section heading 4
        @tip Set the styling for all fourth-level headings in your emails. These should be the smallest of your headings.
        @style heading 4
        */
            h4{
                /*@editable*/color:#808080 !important;
                display:block;
                /*@editable*/font-family:Helvetica;
                /*@editable*/font-size:16px;
                /*@editable*/font-style:normal;
                /*@editable*/font-weight:bold;
                /*@editable*/line-height:125%;
                /*@editable*/letter-spacing:normal;
                margin:0;
                /*@editable*/text-align:left;
            }
        /*
        @tab Preheader
        @section preheader style
        @tip Set the background color and borders for your emails preheader area.
        */
            #templatePreheader{
                /*@editable*/background-color:#FFFFFF;
                /*@editable*/border-top:0;
                /*@editable*/border-bottom:0;
            }
        /*
        @tab Preheader
        @section preheader text
        @tip Set the styling for your emails preheader text. Choose a size and color that is easy to read.
        */
            .preheaderContainer .mcnTextContent,.preheaderContainer .mcnTextContent p{
                /*@editable*/color:#606060;
                /*@editable*/font-family:Helvetica;
                /*@editable*/font-size:11px;
                /*@editable*/line-height:125%;
                /*@editable*/text-align:left;
            }
        /*
        @tab Preheader
        @section preheader link
        @tip Set the styling for your emails header links. Choose a color that helps them stand out from your text.
        */
            .preheaderContainer .mcnTextContent a{
                /*@editable*/color:#606060;
                /*@editable*/font-weight:normal;
                /*@editable*/text-decoration:underline;
            }
        /*
        @tab Header
        @section header style
        @tip Set the background color and borders for your emails header area.
        */
            #templateHeader{
                /*@editable*/background-color:#FFFFFF;
                /*@editable*/border-top:0;
                /*@editable*/border-bottom:0;
            }
        /*
        @tab Header
        @section header text
        @tip Set the styling for your emails header text. Choose a size and color that is easy to read.
        */
            .headerContainer .mcnTextContent,.headerContainer .mcnTextContent p{
                /*@editable*/color:#606060;
                /*@editable*/font-family:Helvetica;
                /*@editable*/font-size:15px;
                /*@editable*/line-height:150%;
                /*@editable*/text-align:left;
            }
        /*
        @tab Header
        @section header link
        @tip Set the styling for your emails header links. Choose a color that helps them stand out from your text.
        */
            .headerContainer .mcnTextContent a{
                /*@editable*/color:#6DC6DD;
                /*@editable*/font-weight:normal;
                /*@editable*/text-decoration:underline;
            }
        /*
        @tab Body
        @section body style
        @tip Set the background color and borders for your emails body area.
        */
            #templateBody{
                /*@editable*/background-color:#FFFFFF;
                /*@editable*/border-top:0;
                /*@editable*/border-bottom:0;
            }
        /*
        @tab Body
        @section body text
        @tip Set the styling for your emails body text. Choose a size and color that is easy to read.
        */
            .bodyContainer .mcnTextContent,.bodyContainer .mcnTextContent p{
                /*@editable*/color:#606060;
                /*@editable*/font-family:Helvetica;
                /*@editable*/font-size:15px;
                /*@editable*/line-height:150%;
                /*@editable*/text-align:left;
            }
        /*
        @tab Body
        @section body link
        @tip Set the styling for your emails body links. Choose a color that helps them stand out from your text.
        */
            .bodyContainer .mcnTextContent a{
                /*@editable*/color:#6DC6DD;
                /*@editable*/font-weight:normal;
                /*@editable*/text-decoration:underline;
            }
        /*
        @tab Footer
        @section footer style
        @tip Set the background color and borders for your emails footer area.
        */
            #templateFooter{
                /*@editable*/background-color:#F2F2F2;
                /*@editable*/border-top:0;
                /*@editable*/border-bottom:0;
            }
        /*
        @tab Footer
        @section footer text
        @tip Set the styling for your emails footer text. Choose a size and color that is easy to read.
        */
            .footerContainer .mcnTextContent,.footerContainer .mcnTextContent p{
                /*@editable*/color:#606060;
                /*@editable*/font-family:Helvetica;
                /*@editable*/font-size:11px;
                /*@editable*/line-height:125%;
                /*@editable*/text-align:left;
            }
        /*
        @tab Footer
        @section footer link
        @tip Set the styling for your emails footer links. Choose a color that helps them stand out from your text.
        */
            .footerContainer .mcnTextContent a{
                /*@editable*/color:#606060;
                /*@editable*/font-weight:normal;
                /*@editable*/text-decoration:underline;
            }
        @media only screen and (max-width: 480px){
            body,table,td,p,a,li,blockquote{
                -webkit-text-size-adjust:none !important;
            }

    }   @media only screen and (max-width: 480px){
            body{
                width:100% !important;
                min-width:100% !important;
            }

    }   @media only screen and (max-width: 480px){
            table[class=mcnTextContentContainer]{
                width:100% !important;
            }

    }   @media only screen and (max-width: 480px){
            table[class=mcnBoxedTextContentContainer]{
                width:100% !important;
            }

    }

    }

    }   @media only screen and (max-width: 480px){
            td[class=mcnImageCardTopImageContent]{
                padding-top:18px !important;
            }

    }   @media only screen and (max-width: 480px){
            table[class=mcnCaptionLeftContentOuter] td[class=mcnTextContent],table[class=mcnCaptionRightContentOuter] td[class=mcnTextContent]{
                padding-top:9px !important;
            }

    }   @media only screen and (max-width: 480px){
            td[class=mcnCaptionBlockInner] table[class=mcnCaptionTopContent]:last-child td[class=mcnTextContent]{
                padding-top:18px !important;
            }

    }   @media only screen and (max-width: 480px){
            td[class=mcnBoxedTextContentColumn]{
                padding-left:18px !important;
                padding-right:18px !important;
            }

    }   @media only screen and (max-width: 480px){
            td[class=mcnTextContent]{
                padding-right:18px !important;
                padding-left:18px !important;
            }

    }   @media only screen and (max-width: 480px){
        /*
        @tab Mobile Styles
        @section Preheader Visibility
        @tip Set the visibility of the emails preheader on small screens. You can hide it to save space.
        */
            table[id=templatePreheader]{
                /*@editable*/display:block !important;
            }

    }   @media only screen and (max-width: 480px){
        /*
        @tab Mobile Styles
        @section Preheader Text
        @tip Make the preheader text larger in size for better readability on small screens.
        */
            td[class=preheaderContainer] td[class=mcnTextContent],td[class=preheaderContainer] td[class=mcnTextContent] p{
                /*@editable*/font-size:14px !important;
                /*@editable*/line-height:115% !important;
            }

    }   @media only screen and (max-width: 480px){
        /*
        @tab Mobile Styles
        @section Header Text
        @tip Make the header text larger in size for better readability on small screens.
        */
            td[class=headerContainer] td[class=mcnTextContent],td[class=headerContainer] td[class=mcnTextContent] p{
                /*@editable*/font-size:18px !important;
                /*@editable*/line-height:125% !important;
            }

    }   @media only screen and (max-width: 480px){
        /*
        @tab Mobile Styles
        @section Body Text
        @tip Make the body text larger in size for better readability on small screens. We recommend a font size of at least 16px.
        */
            td[class=bodyContainer] td[class=mcnTextContent],td[class=bodyContainer] td[class=mcnTextContent] p{
                /*@editable*/font-size:18px !important;
                /*@editable*/line-height:125% !important;
            }

    }   @media only screen and (max-width: 480px){
        /*
        @tab Mobile Styles
        @section footer text
        @tip Make the body content text larger in size for better readability on small screens.
        */
            td[class=footerContainer] td[class=mcnTextContent],td[class=footerContainer] td[class=mcnTextContent] p{
                /*@editable*/font-size:14px !important;
                /*@editable*/line-height:115% !important;
            }

    }   @media only screen and (max-width: 480px){
            td[class=footerContainer] a[class=utilityLink]{
                display:block !important;
            }

    }</style></head>
        <body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0">
            <center>
                <table align="center" border="0" cellpadding="0" cellspacing="0" height="100%" width="100%" id="bodyTable">
                    <tr>
                        <td align="center" valign="top" id="bodyCell">
                            <!-- BEGIN TEMPLATE // -->
                            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                                <tr>

                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" valign="top">
                                        <!-- BEGIN HEADER // -->
                                        <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateHeader">
                                            <tr>
                                                <td align="center" valign="top">
                                                    <table border="0" cellpadding="0" cellspacing="0" width="600" class="templateContainer">
                                                        <tr>
                                                            <td valign="top" class="headerContainer" style="padding-top:10px; padding-bottom:10px;"><table border="0" cellpadding="0" cellspacing="0" width="100%" class="mcnImageBlock">
        <tbody class="mcnImageBlockOuter">
                <tr>
                    <td valign="top" style="padding:9px" class="mcnImageBlockInner">
                        <table align="left" width="100%" border="0" cellpadding="0" cellspacing="0" class="mcnImageContentContainer">
                            <tbody><tr>
                                <td class="mcnImageContent" valign="top" style="padding-right: 9px; padding-left: 9px; padding-top: 0; padding-bottom: 0;">


                                            <img align="left" alt="" src="https://gallery.mailchimp.com/09dc4b1b18f3f16d3f259eb11/images/803e3bf6-65ed-46f0-8786-cfd7b1a84e4e.png" width="500" style="max-width:500px; padding-bottom: 0; display: inline !important; vertical-align: bottom;" class="mcnImage">


                                </td>
                            </tr>
                        </tbody></table>
                    </td>
                </tr>
        </tbody>
    </table></td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                        <!-- // END HEADER -->
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" valign="top">
                                        <!-- BEGIN BODY // -->
                                        <table border="0" cellpadding="0" cellspacing="0" width="100%" id="templateBody">
                                            <tr>
                                                <td align="center" valign="top">
                                                    <table border="0" cellpadding="0" cellspacing="0" width="600" class="templateContainer">
                                                        <tr>
                                                            <td valign="top" class="bodyContainer" style="padding-top:10px; padding-bottom:10px;"><table border="0" cellpadding="0" cellspacing="0" width="100%" class="mcnTextBlock">
        <tbody class="mcnTextBlockOuter">
            <tr>
                <td valign="top" class="mcnTextBlockInner">

                    <table align="left" border="0" cellpadding="0" cellspacing="0" width="600" class="mcnTextContentContainer">
                        <tbody><tr>

                            <td valign="top" class="mcnTextContent" style="padding-top:9px; padding-right: 18px; padding-bottom: 9px; padding-left: 18px;">

                                <h1>Designing Your Email</h1>

    <h3>Creating an elegant email is simple</h3>

    <p>Now that you’ve selected a template to work with, drag in content blocks to define the structure of your message. Don’t worry, you can always delete or rearrange blocks as needed. Then click “Design” to define fonts, colors, and styles.</p>

    <p>Need inspiration for your design? <a class="mc-template-link" href="http://inspiration.mailchimp.com">Here’s what other MailChimp users are doing.</a></p>

                            </td>
                        </tr>
                    </tbody></table>

                </td>
            </tr>
        </tbody>
    </table><table border="0" cellpadding="0" cellspacing="0" width="100%" class="mcnFollowBlock">
        <tbody class="mcnFollowBlockOuter">
            <tr>
                <td align="center" valign="top" style="padding:9px" class="mcnFollowBlockInner">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%" class="mcnFollowContentContainer">
        <tbody><tr>
            <td align="center" style="padding-left:9px;padding-right:9px;">
                <table border="0" cellpadding="0" cellspacing="0" width="100%" class="mcnFollowContent" style="border: 1px solid #EEEEEE;background-color: #FAFAFA;">
                    <tbody><tr>
                        <td align="center" valign="top" style="padding-top:9px; padding-right:9px; padding-left:9px;">
                            <table border="0" cellpadding="0" cellspacing="0">
                                <tbody><tr>
                                    <td valign="top">


                                                <table align="left" border="0" cellpadding="0" cellspacing="0" class="mcnFollowStacked">

                                                    <tbody><tr>
                                                        <td align="center" valign="top" class="mcnFollowIconContent" style="padding-right:10px; padding-bottom:5px;">
                                                            <a href="https://www.facebook.com/investemais" target="_blank"><img src="http://cdn-images.mailchimp.com/icons/social-block-v2/color-facebook-96.png" alt="Facebook" class="mcnFollowBlockIcon" width="48" style="width:48px; max-width:48px; display:block;"></a>
                                                        </td>
                                                    </tr>


                                                    <tr>
                                                        <td align="center" valign="top" class="mcnFollowTextContent" style="padding-right:10px; padding-bottom:9px;">
                                                            <a href="https://www.facebook.com/investemais" target="_blank" style="color: #606060;font-family: Arial;font-size: 11px;font-weight: normal;line-height: 100%;text-align: center;text-decoration: none;">Facebook</a>
                                                        </td>
                                                    </tr>

                                                </tbody></table>


                                    <!--[if gte mso 6]>
                                    </td>
                                    <td align="left" valign="top">
                                    <![endif]-->


                                                <table align="left" border="0" cellpadding="0" cellspacing="0" class="mcnFollowStacked">

                                                    <tbody><tr>
                                                        <td align="center" valign="top" class="mcnFollowIconContent" style="padding-right:10px; padding-bottom:5px;">
                                                            <a href="https://twitter.com/InvesteMais" target="_blank"><img src="http://cdn-images.mailchimp.com/icons/social-block-v2/color-twitter-96.png" alt="Twitter" class="mcnFollowBlockIcon" width="48" style="width:48px; max-width:48px; display:block;"></a>
                                                        </td>
                                                    </tr>


                                                    <tr>
                                                        <td align="center" valign="top" class="mcnFollowTextContent" style="padding-right:10px; padding-bottom:9px;">
                                                            <a href="https://twitter.com/InvesteMais" target="_blank" style="color: #606060;font-family: Arial;font-size: 11px;font-weight: normal;line-height: 100%;text-align: center;text-decoration: none;">Twitter</a>
                                                        </td>
                                                    </tr>

                                                </tbody></table>


                                    <!--[if gte mso 6]>
                                    </td>
                                    <td align="left" valign="top">
                                    <![endif]-->


                                                <table align="left" border="0" cellpadding="0" cellspacing="0" class="mcnFollowStacked">

                                                    <tbody><tr>
                                                        <td align="center" valign="top" class="mcnFollowIconContent" style="padding-right:10px; padding-bottom:5px;">
                                                            <a href="https://www.linkedin.com/company/investe-mais" target="_blank"><img src="http://cdn-images.mailchimp.com/icons/social-block-v2/color-linkedin-96.png" alt="LinkedIn" class="mcnFollowBlockIcon" width="48" style="width:48px; max-width:48px; display:block;"></a>
                                                        </td>
                                                    </tr>


                                                    <tr>
                                                        <td align="center" valign="top" class="mcnFollowTextContent" style="padding-right:10px; padding-bottom:9px;">
                                                            <a href="https://www.linkedin.com/company/investe-mais" target="_blank" style="color: #606060;font-family: Arial;font-size: 11px;font-weight: normal;line-height: 100%;text-align: center;text-decoration: none;">LinkedIn</a>
                                                        </td>
                                                    </tr>

                                                </tbody></table>


                                    <!--[if gte mso 6]>
                                    </td>
                                    <td align="left" valign="top">
                                    <![endif]-->


                                                <table align="left" border="0" cellpadding="0" cellspacing="0" class="mcnFollowStacked">

                                                    <tbody><tr>
                                                        <td align="center" valign="top" class="mcnFollowIconContent" style="padding-right:0; padding-bottom:5px;">
                                                            <a href="http://investemais.pt" target="_blank"><img src="http://cdn-images.mailchimp.com/icons/social-block-v2/color-link-96.png" alt="Website" class="mcnFollowBlockIcon" width="48" style="width:48px; max-width:48px; display:block;"></a>
                                                        </td>
                                                    </tr>


                                                    <tr>
                                                        <td align="center" valign="top" class="mcnFollowTextContent" style="padding-right:0; padding-bottom:9px;">
                                                            <a href="http://investemais.pt" target="_blank" style="color: #606060;font-family: Arial;font-size: 11px;font-weight: normal;line-height: 100%;text-align: center;text-decoration: none;">Website</a>
                                                        </td>
                                                    </tr>

                                                </tbody></table>


                                    <!--[if gte mso 6]>
                                    </td>
                                    <td align="left" valign="top">
                                    <![endif]-->

                                    </td>
                                </tr>
                            </tbody></table>
                        </td>
                    </tr>
                </tbody></table>
            </td>
        </tr>
    </tbody></table>

                </td>
            </tr>
        </tbody>
    </table></td>
                                                        </tr>
                                                    </table>
                                                </td>
                                            </tr>
                                        </table>
                                        <!-- // END BODY -->
                                    </td>
                                </tr>
                                <tr>
                                    </td>
                                </tr>
                            </table>
                            <!-- // END TEMPLATE -->
                        </td>
                    </tr>
                </table>
            </center>
        </body>
    </html>' ; 
 ?>
使用PHP实现这些功能。如果打开输出缓冲,打印输出的所有内容都将被缓冲=存储,而不是打印输出给用户。然后,您可以获取缓冲区并将其保存在变量中

你为什么要这样做

通过输出缓冲,您可以将PHP用作简单的模板引擎。这意味着,您可以创建包含HTML甚至PHP的模板文件

一个简单的例子:

使用以下电子邮件发送至_template.php:


h1标记将包含我的名字Pedro Piloto。

请删除该片段,因为它不适用于phpdidn。我不明白你的意思,对不起,我对php有点陌生。我只想知道如何在php变量中保存大量html和css代码以发送电子邮件这是Mailchimp模板吗?如果是的话,仅仅使用Mailchimp API为您发送电子邮件似乎不是更容易吗?@Pedropilto:您需要进一步的帮助吗?
<!DOCTYPE html>
<html>
    <body>
        <h1>My name is <?=$test?></h1>
    </body>
</html>
$test = "Pedro Piloto";
ob_start();
require('email_template.php');
$body = ob_get_clean();