Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

php mailer and html includes with php variables

Hello I am trying to send html emails using php mailer class. The problem is i would like to incllude php variables in my email while using includes as to keep things organized. Heres my php mailer....

 $place = $data['place'];
 $start_time = $data['start_time'];

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = file_get_contents('../emails/event.html');
$mail->Send(); // send message

my question is, is it possible to have php variables in event.html ? i tried this with no luck (below is event.html)..

<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 style=''>This email is to remind you that you have an upcoming meeting at $place on $start_time.</p>
<p>Thanks</p>
</div>
</td></tr>
</table>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, very easily with include and a short helper function:

function get_include_contents($filename, $variablesToMakeLocal) {
    extract($variablesToMakeLocal);
    if (is_file($filename)) {
        ob_start();
        include $filename;
        return ob_get_clean();
    }
    return false;
}

$mail->IsHTML(true);    // set email format to HTML
$mail->Subject = "You have an event today";
$mail->Body = get_include_contents('../emails/event.php', $data); // HTML -> PHP!
$mail->Send(); // send message
  • The get_include_contents function is courtesy of the PHP include documentation, modified slightly to include an array of variables.

  • Important: Since your include is processing within a function, the scope of execution of the PHP template file (/emails/event.php) is in that function's scope (no variables immediately available besides super globals

  • That is why I have added extract($variablesToMakeLocal) — it extracts all array keys from $variablesToMakeLocal as variables in the function's scope, which in turn means they are within scope of the file being included.

    Since you already had place and start_time in the $data array, I simply passed that straight into the function. You may want to be aware that this will extract all keys within $data — you may or may not want that.

  • Note that now your template file is processing as a PHP file, so all the same caveats and syntax rules apply. You should not expose it to be edited by the outside world, and you must use <?php echo $place ?> to output variables, as in any PHP file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...