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 = 'danyel.p@gmail.com';
$mailer->Password = 'mypass';
$mailer->From = 'contact_email';
$mailer->FromName = 'PS Contact';
$mailer->Subject = $contact_name;
$mailer->AddAddress('pdaniel@gmail.com');
$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.
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,surnamethen put something like this in your html:%%NAME%%,%%SURNAME%%and then in the calling script simply:or by nesting the two above:
EDIT
A more elegant solution in case you have many variables: Define an associative array that will hold your needles and replacements for them:
and use a loop to make it happen: