2

Possible Duplicate:
Problem when loading php file into variable (Load result of php code instead of the code as a string)

I am bit confused about following...

message alert function

public static function MsgAlert($email, $name, $subject, $message)
{
    $msg = include('mailer.php');

    $subject = 'New Message';

    if ($email != '' && validate_email($email))
    {
        $mailer = new PHPMailer();

        $mailer->SetFrom(ADMIN_EMAIL, SITE_NAME);
        $mailer->AddAddress($email);
        $mailer->Subject = $subject;
        $mailer->Body = Postman::nl2br($msg);
        $mailer->AltBody = $msg;
        $mailer->Send();
    }
}

I have put my newsletter design into mailer.php, and please check markup for that...

mailer.php

<div>
<h1><?php $subject; ?><span>27 September 2011 at 6:26 pm</span></h1>
<div>
<p>Hello <?php $name; ?>,</p>
<?php $message; ?>
</div> <!--message container -->
</div>

Am i using correct approach or should i do something more better as its not working. please suggest. thanks.

Community
  • 1
  • 1
seoppc
  • 2,766
  • 7
  • 44
  • 76

2 Answers2

5

You cannot include to a variable, but what you can do is use php's output buffering functions to achieve the desired outcome, e.g.:

ob_start();
include 'mailer.php';
$msg = ob_get_clean();

This will render the template and store the result in $msg.

middus
  • 9,103
  • 1
  • 31
  • 33
5

include() does not RETURN whatever you're including. It'll return true for success (file was included/executed) or false (file couldn't be included). With your current structure, you're need this:

ob_start();
include('mailer.php');
$msg = ob_get_clean();

to capture the output of the mailer.php script as it's included.

Marc B
  • 356,200
  • 43
  • 426
  • 500