如何使用 php 通过电子邮件发送动态填充的 HTML?

How to send dynamically filled HTML via email with php?

我有这样的html需要通过邮件发送:

<!DOCTYPE html>
<html>
    <head>
        <link href='https://fonts.googleapis.com/css?family=Open+Sans&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
        <style>
            #mainWrapper,body,html{height:100%;width:100%}body,html{margin:0;padding:0;font-family:'Open Sans',sans-serif;font-size:15px;line-height:15px}#mainWrapper{position:relative;background-color:#fff}#mainWrapper .logo{position:absolute;top:24px;left:0;right:0;margin:0 auto}#mainWrapper .checkMeOut{position:absolute;top:20px;left:49.5%;right:0;margin:0 auto}#formWrapper{position:absolute;top:99px;left:0;background-color:#f5f5f5;border-top:1px solid #dde0e6;width:100%}.tableWrapper{position:relative;max-width:804px;background-color:#fff;border-radius:5px;padding:20px 38px;margin:71px auto 20px;border-left:1px solid #cfcfcf;border-right:1px solid #cfcfcf}.tableWrapper:after{content:"";position:absolute;bottom:-8px;left:0;right:0;margin:0 auto;width:880px;height:8px}#tableDecoration{display:block;margin:-112px auto 0}#dateField{color:#4b4b4b;font-size:15px;line-height:34px;display:inline-block;border:1px solid rgba(164,164,164,.53);border-radius:18px;padding:0 15px;position:absolute;top:41px;left:91px}.title{text-align:center;padding:31px 0 40px;font-size:21px;line-height:21px}table td,table th{border-bottom:1px solid #dde0e6;vertical-align:middle;padding:25px 35px 19px 34px}table{width:100%;border-top:1px dashed #dde0e6}table th{width:25%;text-align:right}table td{border-left:1px solid #dde0e6}
        </style>
    </head>
    <body>
        <div id="mainWrapper">
            <div id="formWrapper">
                <div class="tableWrapper">
                    <div id="dateField">6/12/2015</div>
                    <table cellspacing="0" cellpading="0">
                        <tr>
                            <th>Campaign ID:</th>
                            <td><?php echo $campaignID ?></td>
                        </tr>
                        <tr>
                            <th>Campaign Title:</th>
                            <td><?php echo $campaignName ?></td>
                        </tr>
                        <tr>
                            <th>Email:</th>
                            <td><?php $campaignName ?></td>
                        </tr>
                        <tr>
                            <th>Start/ End:</th>
                            <td><?php echo $startDate . - . $endDate ?></td>
                        </tr>

                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

这只是全部内容的1/3。只是不想在这里粘贴所有内容。如您所见,某些部分动态填充了 php。我的问题是获取此信息并以 html.

形式发送的最佳方式是什么

我正在使用 php邮件程序:

$mail = new PHPMailer();
$mail->Body= $body;
$mail->IsHTML(true); 

一个选项可能是逐行抓取所有内容并应用于一个变量,但这在我看来不太方便。

另一种选择可能是有一个单独的文件:

$body = file_get_contents(dirname(__FILE__) . '/template/index.html');

但不确定如何处理这些动态值。

也许可以使用某种模板系统?但也不确定如何处理。

您可以仅基于 php str_replace() 函数

构建带有动态值的模板逻辑

1) 在此语法中创建包含变量的电子邮件模板 {variable_name}

 ....
 <div> Hello {name} </div>

2) 创建 getEmailTemplate($tempalte, $variablesArr) 函数,您将在其中阅读电子邮件 $tempalte 并使用 str_replace

进行处理

3)getEmailTemplate 中循环 $variablesArr 并用 templateHTML

中的值替换所有变量
// Example for above html $variablesArr = array('name' => "Your user name")
foreach($variablesArr as $key => $value) {
   $templateHTML = str_replace("{".$key."}", $value, $templateHTML);
}

我会给你一个你可以使用它的例子:

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via mail(), basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; 
// optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAttachment("images/phpmailer.gif");      // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;

} else {
    echo "Message sent!";

}