Rednaxela Anim Rednaxela Anim - 1 month ago 14
PHP Question

PHPMailer: Conditional Statement on Email Template

Not sure if I am doing this correctly or if it is even possible. Basically, I just need to change the background color of a table

<td>
depending on the value of variable $cascadeType.

The form which accepts user input:

<form action="CascadeFunction.php" method="post" name="cascader" id="cascader" enctype="multipart/form-data">
<label><span class="required">*</span>CASCADE TYPE</label><br /><br />
<label for="Release" class="radioitem">RELEASE
<input type="radio" name="cascadeType" value="Release" id="CascadeType_1" /></label>
<label for="Update" class="radioitem">UPDATE
<input type="radio" name="cascadeType" value="Update" id="CascadeType_2" />
</label>
<label for="Reminder" class="radioitem">REMINDER
<input type="radio" name="cascadeType" value="Reminder" id="CascadeType_3" /></label>
<label for="Information" class="radioitem">INFORMATION
<input type="radio" name="cascadeType" value="Information" id="CascadeType_4" /></label>
<input class="btn btn-primary" name="Submit" type="submit" id="submit" value="POST &amp; CASCADE" />
</form>


The submitted data from the above form then gets processed by the file CascadeFunction.php. Where its PHPMailer code to send email is:

$mail->IsHTML(true); // This tells the PhPMailer that the messages uses HTML.
$mail->MsgHTML(str_replace('%cascadeType%', $CascadeType, file_get_contents('emailtemplates/emailtemplate.php')), dirname(__FILE__));


In an attempt to store the variable $cascadeType, I tried using $_SESSIONS as seen on the following code:

<?php
// starting the session
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['cascadeType'] = $_POST['cascadeType'];
}
?>


Then access the session within the email template (emailtemplate.php) using the code:

<?php
// starting the session
session_start();
?>


Then, within the email template (emailtemplate.php), used the conditional statement below.

<tr>
<?php if ($_SESSION['cascadeType'] = "Information") : ?>
<td style="text-align:center; color:#FFF; background-color:#093; margin:10px; padding:10px 5px;" width="20%">
<h4 style="font-weight:bold;">%cascadeType%</h4>
</td>
<?php elseif ($_SESSION['cascadeType'] = "Update") : ?>
<td style="text-align:center; color:#FFF; background-color:#09F; margin:10px; padding:10px 5px;" width="20%">
<h4 style="font-weight:bold;">%cascadeType%</h4>
</td>
<?php elseif ($_SESSION['cascadeType'] = "Reminder") : ?>
<td style="text-align:center; color:#FFF; background-color:#F00; margin:10px; padding:10px 5px;" width="20%">
<h4 style="font-weight:bold;">%cascadeType%</h4>
</td>
<?php else : ?>
<td style="text-align:center; color:#FFF; background-color:#6C3; margin:10px; padding:10px 5px;" width="20%">
<h4 style="font-weight:bold;">%cascadeType%</h4>
</td>
<?php endif ; ?>
<td style="color:#337AB7; text-align:right; padding-right: 20px;" width="80%">
<h3 style="margin-bottom:2px;">TARGET AUDIENCE</h3>
<p style="margin-top:2px;">%targetAudience%</p>
</td>




The email output just displays all table
<td>
and apparently does not work. Please advise on what is the best way to approach the situation if conditional statements will really not work within an email template

Answer

You're using method MsgHTML which seem to expect a plain HTML. But you're giving it some PHP code with conditions. This PHP code is never evaluated, therefore conditions don't work.

You may try to run the template content using eval and grab its output, but really you should consider using a templater if you need something a little more than trivial replacement in the templates. Look at Twig templater, for example - http://twig.sensiolabs.org/

Update:

Re using eval. First of all, I have to say that this may be unsafe, if you're not very careful about what exactly code is executed.

The idea is to read template content (essentially some HTML+PHP code) into a PHP variable, define some other variables that are used in the template and then use PHP function eval to run that code.

Something like this:

$template = file_get_contents('emailtemplates/emailtemplate.php');
$html = eval('ob_start(); ?>' . $template . '<?php return ob_get_clean();');

Or even this

ob_start();
require('emailtemplates/emailtemplate.php');
$html = ob_get_clean();

Then use the $html variable in MsgHTML

The important trick in both examples is ob_* functions. ob_start() starts capturing the output of PHP code and ob_get_clean() stops it and returns everything that was returned. Check the documentation on these functions. Also note in example with eval() it's important to put ?> and <?php around the template content as unlike include, eval starts in PHP context, but not in HTML.

Comments