I have such html which needs to be sent via email:
<!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>
This is just 1/3 of the whole content. Just did not want to paste everything in here. As you can see some parts are dynamically filled with php. My question is what is the best way to grab this and send as html.
I am using phpmailer:
$mail = new PHPMailer();
$mail->Body= $body;
$mail->IsHTML(true);
One of the options might be to grab everything line by line and apply to one variable, but this looks to me not very convenient.
Another option might be to have a separate file:
$body = file_get_contents(dirname(__FILE__) . '/template/index.html');
But not sure how to handle those dynamically values when.
Maybe some kind of templating system can be used? But not sure how to handle this as well.
You can build templateing logic with dynamic values inside based on just php str_replace()
function
1) create email templates with variables inside in this syntax {variable_name}
....
<div> Hello {name} </div>
2) create getEmailTemplate($tempalte, $variablesArr)
function where you will read your email $tempalte and process with str_replace
3) make loop in getEmailTemplate
over $variablesArr
and replace all variables with their values inside templateHTML
// Example for above html $variablesArr = array('name' => "Your user name")
foreach($variablesArr as $key => $value) {
$templateHTML = str_replace("{".$key."}", $value, $templateHTML);
}