0

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

3 Answers 3

2

You should change your code in sample.php from

<p>Variable one is <?php echo $variable['one']; ?>.</p>
<p>Variable two is <?php echo $variable['two']; ?>.</p>

into

<p>Variable one is <?php echo $one; ?>.</p>
<p>Variable two is <?php echo $two; ?>.</p>

because you used extract function

Sign up to request clarification or add additional context in comments.

Comments

1

Answer is in the comments, but an alternative way to tackle this is to change the code to:

$mail->Body = include('sample.php'); 

And in sample.php instead of echoing everything, return it. So you can either place ob_start() at the top, then ob_get_clean at the bottom of sample.php , or you can store the html in a multi-line string.

<?php
return <<<EOT
<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 $variable[one].</p>
<p>Variable two is $variable[two].</p>
<p>Thanks</p>
</div>
</td></tr>
</table>
EOT
?>

1 Comment

this is not working. could you please explain this. curious to know this code too !!!
0

If you use extract() the keys of the array will be the variable names. So, in your example the variables you'll have to use in your template are $one and $two

Check the manual for more info: https://www.php.net/manual/en/function.extract.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.