我有这样的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>
这只是整个内容的三分之一。只是不想把所有东西都粘贴在这里。正如您所看到的,有些部分是用php动态填充的。我的问题是什么是最好的方式来获取这个并发送为html。
我正在使用phpmailer:
$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!";
}