0

so I have a table in which I have email templates, and these email templates can be later fetched for sending emails.

The structure is :-

for_status     subject          message
 int             text           text

An example entry is :-

for_status = 1,
subject = Transaction Status Changed,
message = Hi $user->firstname, this is a test message.

Ok, so the problem is, when I send the email with the current message and subject, it displays in the email Hi $user->firstname, this is a test message

Instead of showing, Hi thefirstname here, this is a test message.

I'm fetching user details successfully on the same page in the $user variable. What's going wrong here?

Marc B
  • 356,200
  • 43
  • 426
  • 500
user3369276
  • 71
  • 2
  • 11
  • Just because you have a variable inside some text doesn't mean anything. Unless that string gets executed somehow, that variable is just some randomish characters that LOOK like a variable. – Marc B Jul 18 '14 at 19:36
  • You need to use templates/placeholders as mentioned in the answers. [This](http://stackoverflow.com/questions/3158743/simple-template-var-replacement-but-with-a-twist), [and this](http://stackoverflow.com/questions/477398/php-templating-with-str-replace) question can provide some ideas. – undefined Jul 18 '14 at 19:41

3 Answers3

1

When you say "table of email templates", is the actual text of the email template in some sort of database?

If this is the case and you have $user->firstname in the database, I'm pretty sure its going to spit that out directly.

mmundiff
  • 3,875
  • 7
  • 32
  • 46
1

You need something whats called placeholders. You need to decide what to have as a place holder, I personally use the following:

{%first_name%}, this is a test message.

Then in your PHP code you just have an array of placeholders & values like this:

$arr = array(
   '{%first_name%}' => $user->firstname,
);

//and now replace the body
$body = str_replace(array_keys($arr), $arr, $body);

There is a better way of doing this by using already written libraries or using Regular Expression to correctly parse, but I will leave that up to you to figure it out.

GGio
  • 7,563
  • 11
  • 44
  • 81
  • ok this seems to be working. But this only works for 1 value. If have like 3 values within the array, it doesn't seems to be working. – user3369276 Jul 18 '14 at 20:16
  • you need to have 3 place holders than as well. if you mean 3 same place holders with different values yes it will not work because its an array and its keys are the place holders you cant have same key and different values – GGio Jul 18 '14 at 20:17
0

PHP will only perform the substitution for you on strings you have coded into your own PHP files, not those that have come from a database or any other source. If it did, that would be a massive security risk!

You will need to perform substitution on your template yourself.

// $message is: Hi %{userFirstName}, this is a test message.
$message = str_replace('%{userFirstName}', $user->firstname, $message);
Dave Morrissey
  • 4,371
  • 1
  • 23
  • 31