Pacuraru Daniel
Pacuraru Daniel

Reputation: 1205

load html mail file inside php

I implemented a html email inside a html file. And i have a php file which uses the PHPMailer class to send emails. What i want to achieve is, i have some text inside the html that should change depends who i send the email.

This is the php file that sends the emails

<?php
    // get variables
    $contact_name = addslashes($_GET['contact_name']);
    $contact_phone = addslashes($_GET['contact_phone']);
    $contact_email = addslashes($_GET['contact_email']);
    $contact_message = addslashes($_GET['contact_message']);

    // send mail
    require("class.phpmailer.php");
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = 'ssl://smtp.gmail.com:465';
    $mailer->SMTPAuth = TRUE;
    $mailer->Username = '[email protected]';
    $mailer->Password = 'mypass';
    $mailer->From = 'contact_email';
    $mailer->FromName = 'PS Contact';
    $mailer->Subject = $contact_name;
    $mailer->AddAddress('[email protected]');
    $mailer->Body = $contact_message;
    if($mailer->Send())
        echo 'ok';
?>

And the html file containing a simple html mail implemented with tables and all that standard it need.

I want to ask braver minds than mine which is the best approach to accomplish this. :)

Thank you in advance, Daniel!

EDIT: right now, in the $mailer->Body i have the $contact_message variable as a text email.. but i want in that body to load an html file containing an html email and i want to somehow change the body of the html email with the text inside this $contact_message variable.

Upvotes: 1

Views: 1691

Answers (4)

pankar
pankar

Reputation: 1701

One simple way to go is to have special tokens in your html files that will be replaced by the caller. For example assuming that you have two variables that might dynamically change content, name, surname then put something like this in your html: %%NAME%%, %%SURNAME%% and then in the calling script simply:

$html = str_replace("%%NAME%%", $name, $html);
$html = str_replace("%%SURNAME%%", $surname, $html);

or by nesting the two above:

$html = str_replace("%%NAME%%", $name, str_replace("%%SURNAME%%", $surname, $html));



EDIT A more elegant solution in case you have many variables: Define an associative array that will hold your needles and replacements for them:

$myReplacements = array ( "%%NAME%%"    => $name,
                          "%%SURNAME%%" => $surname
);

and use a loop to make it happen:

foreach ($myReplacements as $needle => $replacement)
   $html = str_replace($needle, $replacement, $html);

Upvotes: 1

tigrang
tigrang

Reputation: 6767

To answer your edit:

function renderHtmlEmail($body) {
    ob_start();
    include ('my_html_email.php');
    return ob_get_clean();
}

In your my_html_email.php file you would have something like so:

<html>
    <body>
        <p>....<p>
        <!-- the body -->
        <?php echo $body; ?>
    </body>
</html>

And

 $mailer->Body = renderHtmlEmail($contact_message);

If you need other variables to pass to the layout/template file, add params to that method, or pass an associative array like so function renderHtmlEmail($viewVars) and inside the function extract($viewVars);

You will then be able to use those vars inside the template, for ex. Dear <?php echo $to; ?>,

You will probably have to change your html file from .html to .php if it isn't already.

That is, if I understood the issue correctly.

Upvotes: 0

bkwint
bkwint

Reputation: 626

If I build a website I usually use a templating engine, something like Smarty... You could write your html mail in a smarty template file. Then you could add the wanted texts based on criteria automatically. Just assign the correct values to the templating engine.

Upvotes: 0

mlishn
mlishn

Reputation: 1667

Create conditional statements based on the email you want to see to. Then include that in the tempalted php html email text.

You can also pass the changing values to a function which will implement the above.

Upvotes: 0

Related Questions