Reputation: 3361
I am working on a project where I need to add a email template that uses variables for name, address and phone number. In my database i have 2 tables
on front end i have a textarea to add templates in database.
My template contains tokens like {name}, {address}, {phone} that are replaced with respective user detail when I send emails using that template.
Now i am able to fetch all details for users and email templates but not able to replace the tokens with values with php. i tried str_replace to replace {name} and other tokens with variables like $user->name.
Upvotes: 0
Views: 47
Reputation: 36
You can use below code to replace tokens :
$template_body = file_get_contents('Email template file path');
$email_values= array(
'name'=>$user->name,
'address'=>$user->address,
'phone'=>$user->phone,
);
if(count($email_values)>0)
{
foreach($email_values as $key=>$value)
{
$template_body = str_replace('{'.$key.'}',$value,$template_body);
}
}
return $template_body;
Upvotes: 1