dsebywql016137 2017-07-11 11:06
浏览 121
已采纳

php sprintf HTML模板

I have html mail template which I want to send to my users by php mail() function.

I'm putting this template to php variable and want to insert into that the specified variables using sprintf() function but I'm getting this-

Warning: sprintf(): Too few arguments

in my html template

<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>template</title>
    <style type="text/css">
        @media screen and (max-width: 600px) {
            table[class="container"] {
                width: 95% !important;
            }
        }
        
        #outlook a {
            padding: 0;
        }
        
        body {
            width: 100% !important;
            -webkit-text-size-adjust: 100%;
            -ms-text-size-adjust: 100%;
            margin: 0;
            padding: 0;
        }
        
        .ExternalClass {
            width: 100%;
        }
        
        .ExternalClass,
        .ExternalClass p,
        .ExternalClass span,
        .ExternalClass font,
        .ExternalClass td,
        .ExternalClass div {
            line-height: 100%;
        }
        
        #backgroundTable {
            margin: 0;
            padding: 0;
            width: 100% !important;
            line-height: 100% !important;
        }
        
        img {
            outline: none;
            text-decoration: none;
            -ms-interpolation-mode: bicubic;
        }
        
        a img {
            border: none;
        }
        
        .image_fix {
            display: block;
        }
        
        p {
            margin: 1em 0;
        }
        
        h1,
        h2,
        h3,
        h4,
        h5,
        h6 {
            color: black !important;
        }
        
        h1 a,
        h2 a,
        h3 a,
        h4 a,
        h5 a,
        h6 a {
            color: blue !important;
        }
        
        h1 a:active,
        h2 a:active,
        h3 a:active,
        h4 a:active,
        h5 a:active,
        h6 a:active {
            color: red !important;
        }
        
        h1 a:visited,
        h2 a:visited,
        h3 a:visited,
        h4 a:visited,
        h5 a:visited,
        h6 a:visited {
            color: purple !important;
        }
        
        table td {
            border-collapse: collapse;
        }
        
        table {
            border-collapse: collapse;
            mso-table-lspace: 0pt;
            mso-table-rspace: 0pt;
        }
        
        a {
            color: #000;
        }
        
        @media only screen and (max-device-width: 480px) {
            a[href^="tel"],
            a[href^="sms"] {
                text-decoration: none;
                color: black;
                /* or whatever your want */
                pointer-events: none;
                cursor: default;
            }
            .mobile_link a[href^="tel"],
            .mobile_link a[href^="sms"] {
                text-decoration: default;
                color: orange !important;
                /* or whatever your want */
                pointer-events: auto;
                cursor: default;
            }
        }
        
        @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
            a[href^="tel"],
            a[href^="sms"] {
                text-decoration: none;
                color: blue;
                /* or whatever your want */
                pointer-events: none;
                cursor: default;
            }
            .mobile_link a[href^="tel"],
            .mobile_link a[href^="sms"] {
                text-decoration: default;
                color: orange !important;
                pointer-events: auto;
                cursor: default;
            }
        }
        
        @media only screen and (-webkit-min-device-pixel-ratio: 2) {
            /* Put your iPhone 4g styles in here */
        }
        
        @media only screen and (-webkit-device-pixel-ratio:.75) {
            /* Put CSS for low density (ldpi) Android layouts in here */
        }
        
        @media only screen and (-webkit-device-pixel-ratio:1) {
            /* Put CSS for medium density (mdpi) Android layouts in here */
        }
        
        @media only screen and (-webkit-device-pixel-ratio:1.5) {
            /* Put CSS for high density (hdpi) Android layouts in here */
        }
        /* end Android targeting */
        
        h2 {
            color: #181818;
            font-family: Helvetica, Arial, sans-serif;
            font-size: 22px;
            line-height: 22px;
            font-weight: normal;
        }
        
        a.link1 {}
        
        a.link2 {
            color: #fff;
            text-decoration: none;
            font-family: Helvetica, Arial, sans-serif;
            font-size: 16px;
            color: #fff;
            border-radius: 4px;
        }
        
        p {
            color: #555;
            font-family: Helvetica, Arial, sans-serif;
            font-size: 16px;
            line-height: 160%;
        }
    </style>

    <script type="colorScheme" class="swatch active">
        { "name":"Default", "bgBody":"ffffff", "link":"fff", "color":"555555", "bgItem":"ffffff", "title":"181818" }
    </script>

</head>

<body>
    <!-- Wrapper/Container Table: Use a wrapper table to control the width and the background color consistently of your email. Use this approach instead of setting attributes on the body tag. -->
    <table cellpadding="0" width="100%" cellspacing="0" border="0" id="backgroundTable" class='bgBody'>
        <tr>
            <td>
                <table cellpadding="0" width="620" class="container" align="center" cellspacing="0" border="0">
                    <tr>
                        <td>
                            <!-- Tables are the most common way to format your email consistently. Set your table widths inside cells and in most cases reset cellpadding, cellspacing, and border to zero. Use nested tables as a way to space effectively in your message. -->


                            <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" class="container">
                                <tr>
                                    <td class='movableContentContainer bgItem'>

                                        <div class='movableContent'>
                                            <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" class="container">
                                                <tr height="40">
                                                    <td width="200">&nbsp;</td>
                                                    <td width="200">&nbsp;</td>
                                                    <td width="200">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td width="200" valign="top">&nbsp;</td>
                                                    <td width="200" valign="top" align="center">
                                                        <div class="contentEditableContainer contentImageEditable">
                                                            <div class="contentEditable" align='center'>
                                                                <img src="images/logo.png" width="155" height="155" alt='Logo' data-default="placeholder" />
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td width="200" valign="top">&nbsp;</td>
                                                </tr>
                                                <tr height="25">
                                                    <td width="200">&nbsp;</td>
                                                    <td width="200">&nbsp;</td>
                                                    <td width="200">&nbsp;</td>
                                                </tr>
                                            </table>
                                        </div>

                                        <div class='movableContent'>
                                            <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" class="container">
                                                <tr>
                                                    <td width="100%" colspan="3" align="center" style="padding-bottom:10px;padding-top:25px;">
                                                        <div class="contentEditableContainer contentTextEditable">
                                                            <div class="contentEditable" align='center'>
                                                                <h2></h2>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td width="100">&nbsp;</td>
                                                    <td width="400" align="center">
                                                        <div class="contentEditableContainer contentTextEditable">
                                                            <div class="contentEditable" align='left'>
                                                                <p>Dear , %s
                                                                    <br/>
                                                                    <br/> 
                                                                    Thank you for being with us.<br/>
                                                                    This is your registration Information.<br/>
                                                                    login - <br/>
                                                                    Password - <br/>
                                                                    Click on the link below to sign-in your profile. If you're no longer interested in hearing from us, simply click on unsubscribe below (or ignore this message) and we won't send you any more newsletters.
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td width="100">&nbsp;</td>
                                                </tr>
                                            </table>
                                            <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" class="container">
                                                <tr>
                                                    <td width="200">&nbsp;</td>
                                                    <td width="200" align="center" style="padding-top:25px;">
                                                        <table cellpadding="0" cellspacing="0" border="0" align="center" width="200" height="50">
                                                            <tr>
                                                                <td bgcolor="#ED006F" align="center" style="border-radius:4px;" width="200" height="50">
                                                                    <div class="contentEditableContainer contentTextEditable">
                                                                        <div class="contentEditable" align='center'>
                                                                            <a target='_blank' href="" class='link2'>Sign-In</a>
                                                                        </div>
                                                                    </div>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                    <td width="200">&nbsp;</td>
                                                </tr>
                                            </table>
                                        </div>
                                        <div class='movableContent'>
                                            <table cellpadding="0" cellspacing="0" border="0" align="center" width="600" class="container">
                                                <tr>
                                                    <td width="100%" colspan="2" style="padding-top:65px;">
                                                        <hr style="height:1px;border:none;color:#333;background-color:#ddd;" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td width="60%" height="70" valign="middle" style="padding-bottom:20px;">
                                                        <div class="contentEditableContainer contentTextEditable">
                                                            <div class="contentEditable" align='left'>
                                                                <span style="font-size:13px;color:#181818;font-family:Helvetica, Arial, sans-serif;line-height:200%;">Sent to  by LinkedInterests.in</span>
                                                                <br/>
                                                                <span style="font-size:13px;color:#181818;font-family:Helvetica, Arial, sans-serif;line-height:200%;">
<!--                                                             <a target='_blank' href="#" style="text-decoration:none;color:#555">Forward to a friend</a>-->
                                                                </span>
                                                                <br/>
                                                                <span style="font-size:13px;color:#181818;font-family:Helvetica, Arial, sans-serif;line-height:200%;">
                                                                    <a href="#" style="text-decoration:none;color:#555">click here to unsubscribe</a>
                                                               </span>
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td width="40%" height="70" align="right" valign="top" align='right' style="padding-bottom:20px;">
                                                        <table width="100%" border="0" cellspacing="0" cellpadding="0" align='right'>
                                                            <tr>
                                                                <td width='57%'></td>
                                                                <td valign="top" width='34'>
                                                                    <div class="contentEditableContainer contentFacebookEditable" style='display:inline;'>
                                                                        <div class="contentEditable">
                                                                            <img src="" data-default="placeholder" data-max-width='30' data-customIcon="true" width='30' height='30' alt='facebook' style='margin-right:40x;'>
                                                                        </div>
                                                                    </div>
                                                                </td>
                                                                <td valign="top" width='34'>
                                                                    <div class="contentEditableContainer contentTwitterEditable" style='display:inline;'>
                                                                        <div class="contentEditable">
                                                                            <img src="" data-default="placeholder" data-max-width='30' data-customIcon="true" width='30' height='30' alt='twitter' style='margin-right:40x;'>
                                                                        </div>
                                                                    </div>
                                                                </td>
                                                                <td valign="top" width='34'>
                                                                    <div class="contentEditableContainer contentImageEditable" style='display:inline;'>
                                                                        <div class="contentEditable">
                                                                            <a target='_blank' href="#" data-default="placeholder" style="text-decoration:none;">
                                                                                <img src="" width="30" height="30" data-max-width="30" alt='pinterest' style='margin-right:40x;' />
                                                                            </a>
                                                                        </div>
                                                                    </div>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>

            </td>
        </tr>
    </table>
    <!-- End of wrapper table -->
</body>

</html>

%s is only in line 260

and my php code

$to = $validatedData['email'];
$name = $validatedData['name'];

$subject = 'Thank\'s for being with Us';

$message = file_get_contents(ROOT . '/views/newsletters/sign-up.php');
$message = sprintf($message, $name);

$headers  = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

$headers[] = 'To: ' . $validatedData['name'] . '<' . $to . '>';
$headers[] = 'From: LinkedInterests Support <support@LinkedInterests.in>';                

mail($to, $subject, $message, implode("
", $headers));
</div>
  • 写回答

4条回答 默认 最新

  • douxia9826 2017-07-11 11:15
    关注
    $body = file_get_contents('email-template.html');
    $patterns = array(
            '/{name}/',
            '/{date}/',
            '/{company}/',
            '/{contact}/',
            '/{email}/',
        );
        $replacements = array(
            $post['name'],
            $post['date'],
            $post['company'],
            $post['contact'],
            $post['email'],
        );
    $body = preg_replace($patterns, $replacements, $body);
    

    And Email Template Should be like this

    <!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Email</title>
    </head>
    
    <body>
            <p>
                <b>Hi {name},</b>
                <b>Your {date},</b>
                <b>Your {company},</b>
                <b>Your {contact},</b>
                <b>Your {email},</b>
            </p>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c#里的静态变量为什么是错误的?
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误