I have a code like the below. I'm not able to pass the variables to the php file and send a mail.
In other words, I need to send some variable to php file, then include the php file in my body content and send it as a mail.
How to resolve it. The problem I'm getting is - The php variables defined in sample.php is not displaying in my body content.
index.php
<?php
require("class.phpmailer.php");
function get_include_contents($filename, $variablesToMakeLocal) {
extract($variablesToMakeLocal);
if (is_file($filename)) {
ob_start();
include $filename;
return ob_get_clean();
}
return false;
}
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom("[email protected]");
$mail->AddAddress("[email protected]");
$mail->Subject = "Test Subject";
$variable['one'] = 'my variable one';
$variable['two'] = 'my variable two';
$mail->IsHTML(true);
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('sample.php', $variable);
$mail->Send();
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
sample.php
<table width='600px' cellpadding='0' cellspacing='0'>
<tr><td bgcolor='#eeeeee'><img src='logo.png' /></td></tr>
<tr><td bgcolor='#ffffff' bordercolor='#eeeeee'>
<div style='border:1px solid #eeeeee;font-family:Segoe UI,Tahoma,Verdana,Arial,sans-serif;padding:20px 10px;'>
<p>Variable one is <?php echo $variable['one']; ?>.</p>
<p>Variable two is <?php echo $variable['two']; ?>.</p>
<p>Thanks</p>
</div>
</td></tr>
</table>
Please help me out.
Thanks, Kimz