0

why my php variables are not initializing, what is the workaround to send html email with initialized php variable values before sending email?. Im sure I must be missing something.

$query=mysqli_query($conDB,"SELECT * FROM client_profile WHERE client_id='1'") or die(mysql_error());
    while($row = mysqli_fetch_array($query)) {
    $client_name = $row['client_name'];
    }

I fetched the client name in $client_name from above query.

$query2=mysqli_query($conDB,"SELECT * FROM emails WHERE emailfor='1'") or die(mysql_error());
while($row2 = mysqli_fetch_array($query2)) {

$from = $row2['fromEmail'];
$sendTO = $row2['to_Email'];
$template = $row2['template'];  //this database column holds this variable $client_name with table template
$subject = $row2['subject'];

}

$template variable holds the below structure

    <div style="color:#333;font-size: 14px;">
    Dear, '. $client_name .'<br /><br /><br />
    <table border="1" style="border-collapse:collapse; border:thin dotted #CCC; padding:5px; color:#333; font-size:13px;">
      <tr>
        <td><h3>Client Information</h3></td>    
      </tr>

      <tr><td>Client Name</td>    <td>'. $client_name .'</td></tr>
    </table>
    </div>

now im sending email as below ---

$to = $sendTO;
$emailSubject="$subject";
$from = $fromTO;    

$message = '<html><body bgcolor="#FFFFFF">';
$message .= '<head>';
$message .= '</head>';

$message .= "$template";    
$message .= "</body></html>";    

$headers = "From: $from \r\n" . "CC: $from";
$headers .= "Reply-To: $from \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";

mail($to, $emailSubject, $message,$headers);

Im receiving email with the variable name as Dear, $client_name Any help will be much appreciated.

2 Answers 2

0

Write $message .= "{$template}";

Or use str_replace function =>

str_replace($arr , $arr1 , $string)

http://php.net/manual/ru/function.str-replace.php

0
-1

If you are working with PHP 5.2.x or higher you might want to checkout Mandrill and maybe used with MailChimp for your template generation.

The sandbox mode they have is pretty good.

Mandrill - https://mandrillapp.com/api/docs/index.php.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.