Vikram
Vikram

Reputation: 3361

Create and use tokens in texarea to add templates

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

  1. Users - with name, address, phone number and category
  2. Email_templates - for different email templates like Christmas, New year etc

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

Answers (1)

Bimal Patel
Bimal Patel

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

Related Questions