Dear All

I have 2 Pages
smtpAuth.php
contents.php

When I use this code

$body             = file_get_contents('contents.php');
      $body             = eregi_replace("[\]",'',$body);

It cannot show PHP variables values in Send email.
Content.php file attached as contentsphpfile

Whole HTML working fine but PHP DataBase values are not showing in Email.

Dani AI

Generated

As correctly pointed out, reading a PHP file as plain text will give you the source, not the executed HTML — so any echoed variables inside that file won’t be expanded. Two practical approaches will produce the executed output you want.

Capture the executed output by including the script and buffering its output (runs in the same process and is simple to use):

<?php
ob_start();
include __DIR__ . '/contents.php';
$body = ob_get_clean();
?>

Or fetch the page over HTTP so the server executes it (works if allow_url_fopen is enabled, or use curl as suggested). That returns the rendered HTML rather than the raw PHP source.

The image problem you reported when moving the HTML into smtpauth.php is usually a path/embedding issue: email clients can’t resolve local or relative filesystem paths. Either use an absolute URL () or embed the image into the message as an inline attachment (Content-ID). Example with PHPMailer:

$mail->addEmbeddedImage('/full/server/path/images/phpmailer.gif', 'logo_cid', 'phpmailer.gif');
$body = '<img src="cid:logo_cid" alt="Logo">';

Other quick tips:

  • If contents.php needs a DB connection or certain variables, set those up before you include it or refactor the template to accept data rather than open its own DB.
  • Avoid deprecated functions like eregi_replace; use str_replace('\\', '', $body) or preg_replace() for safer replacements.
  • Ensure the mail is sent as HTML (Content-Type header or $mail->isHTML(true)), then inspect the final $body (echo or save to file) to debug missing values or broken URLs.

These steps will make the variables render and resolve image display issues.

Recommended Answers

All 8 Replies

What output you are getting

I am getting everything of HTML tags, but not getting echo Var a; value in my email or even echo this code

$body             = file_get_contents('contents.php');

When I open the contents.php it shows value of a in browser.

Try with php_curl
You must enable php_curl on your server.

then write following function in your smtpauth.php

<?php
function my_curl($url,$rethdr=0)
{
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_HEADER, $rethdr);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$res=curl_exec($ch);	
	return $res;
}
?>

$body=my_curl('http://localhost/yourfolder/contents.php',1);

I hope this will work

but i want to know why its not working

Try with php_curl
You must enable php_curl on your server.

then write following function in your smtpauth.php

<?php
function my_curl($url,$rethdr=0)
{
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_HEADER, $rethdr);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$res=curl_exec($ch);	
	return $res;
}
?>

$body=my_curl('http://localhost/yourfolder/contents.php',1);

I hope this will work

curl is difficult but my problem is simple, if i use html and php code on smtpauth.php its work fine, but if i get it using get content why its not working

file_get_contents('contents.php'); will only read php file it will not execute php code.

Ok, but if I put my whole contents.php code in the smtpauth.php then it will work fine. but only one problem , its not including image, all other code is working fine
here is code

$body			= "<html><body style='margin: 10px;'>
<div style='width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;'>
<div align='center'><img src='images/phpmailer.gif' style='height: 90px; width: 340px'></div><br>
<table  style='border-color: #666;' cellpadding='10'>
  	<tr>
    <td width='42%' style='background: #eee;'><strong>Total Days</strong></td>
    <td width='58%' style='background: #eee;'><strong>No. of Payments</strong></td>
	</tr>
	
	 	<tr>
    <td width='42%' style='background: #eee;'>15-21</td>
    <td width='58%'>$num_rows</td>
	</tr>
	
		 	<tr>
    <td width='42%' style='background: #eee;'>21-30</td>
    <td width='58%'>$num_rows</td>
	</tr>
	
		 	<tr>
    <td width='42%' style='background: #eee;'>30+</td>
    <td width='58%'>$num_rows</td>
	</tr>

</table>
<br />
Engineering Database:<br />
Generated Automatically by Engineering Databse<br />
Please, DO NOT Reply to this System Generated Email.<br /></div></body></html>";

<?php
<img src='<?php echo "http://".$_SERVER["HTTP_HOST"]."/yourfolder/images/phpmailer.gif" ?>' style='height: 90px; width: 340px'>

?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.