user1411837
user1411837

Reputation: 1564

php mail - include a html file in the mail

I have a mailer template in html format. It is a large html file with inline css that will hold dynamic data.

Now this is my code.

$subject = "V-Link Logistics Booking Update";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $msg = "mails/airbill.php";
    mail($res['0']['user_email'],$subject,$msg,$headers);

This does not seem to work and really dont get the problem. Any advice would be very helpful.

Upvotes: 4

Views: 3577

Answers (1)

Lix
Lix

Reputation: 47966

You can use the PHP function file_get_contents() to simply extract the contents of a file.

So your $msg variable would be built like this:

$msg = file_get_contents("mails/airbill.php");

If airbill.php needs to be passed through the PHP interpreter, you could use the include() function's return value (which is the evaluated code). To use this method, the airbill.php file needs to actually return it's content as a string (using a simple return statement). So you could do something like this:

airbill.php :

<?php

$message = <<<message
<strong>This</strong> is the content of the <span style="...">email</span>
message;
return $message;

?>

Your other file would look like this:

$interpreted_content = include("mails/airbill.php")
$msg = $interpreted_content;

Upvotes: 4

Related Questions