Forums

Load email templates from database

MichielStr 12 Apr, 2013
Hi all,

I am facing a(nother) challenge. I have to send emails to users who are not the logged in users. These emails can be in languages different from that of the user in logged in. So I cannot use the Multi-Language action to define the content of the email (that would send an email in the language of the logged in user).

So I have set up a table with email templates in different languages which get retrieved based on the (not logged in) user's language preferences.

Retrieving the correct template works well and is in the following array-format:

[email] => Array
(
[email_subject] => Email about item {cf_id}
[email_text] => Dear {user.firstname} {user.lastname},...
)

Setting the subject and body in the Email [GH] action works fine when using {email.email_subject} and {email.email_text} respectively. The values between curly brackets within the subject (cf_id) and body (user.firstname and user.lastname) are not replaced by the correct values, however. So an email is sent with curly brackets and not with the values they represent.

This must have something to do with the order of processing or the data type of the values retrieved, since the action should replace the values between curly brackets.

Is there a way to get this working?
GreyHead 13 Apr, 2013
Hi MichielStr,

I think that my Email [GH] action only handles the basic curly replacement in the top level $form->data array, I'll have a look at adding the extra parsing to handle sub-arrays (or Model IDs).

If you want a workaround then I think that this should work OK:
<?php
$form->data['user_name'] = $form->data['user']['firstname'].' '.$form->data['user']['lastname'];
?>
. . .
[email_text] => Dear {user_name},... 

Bob
MichielStr 13 Apr, 2013
Hi Bob,

That is not the issue. Your action handles sub-arrays perfectly well. If I put {email.email_text} in the email body field of your action, the correct sub-array value (the email text) is shown.

Where it goes wrong, however, is with the curly bracket values within the template stored in the database. These are not correctly translated to their array-values.

To be more precise, I have three arrays:

[email] => Array
(
[email_subject] => Email about item {cf_id}
[email_text] => Dear {user.firstname} {user.lastname},...
)

[user] => Array
(
[firstname] => John
[lastname] => Doe
)

[cf_id] => 20

The email text is sent by your Email [GH] action. I want the {cf_id}, {user.firstname} and {user.lastname} to be replaced by their respective values from the other arrays. However, the email that arrives at the recipient still contains the curly brackets. So the correct template is sent, but the translation of curly brackets values inside the template fails.

Hope this makes things clearer.
GreyHead 13 Apr, 2013
Hi MichielStr,

Sorry, I misunderstood. How are you loading the saved template into the Email [GH] action? Are you putting that in curly brackets as {email.email_text} ?

Bob
MichielStr 13 Apr, 2013
Yes, indeed. I also tried putting in EMAILBODY and then EMAILBODY={email.email_text} in the Multi-language action. To no avail...
GreyHead 13 Apr, 2013
Hi MichielStr,

Ah, neither of the actions will handle a curly bracket inside a curly bracket which is what you end up with :-(

Try adding this in the Email template (the Rich Text Editor must be off):
<?php
echo $form->data['email']['email_text'];
?>

Or I think you could skip a step and include a text file something like this.
<?php
$user =& JFactory::getUser($user_id);
$language = $user->getParam('language', 'en_GB');
include ('/some_path/'.$language.'email_text.php');
?>

I haven't tested but I think either of these would then allow the {email.firstname} values to be inserted.

Bob
MichielStr 13 Apr, 2013
No luck with the first solution. Still the values between curly brackets within the template aren't resolved. Tried doing it the other way around (replacing the curly brackets in the template by 'real' variables), but then part of the variable code gets stripped. $form->data[user][firstname] become data[user][firstname] in the final output.

I will try the php solution. This could also be a solution for an earlier problem I posted about, where I had to send a unique ID to each recipient within a list of recipients.
MichielStr 14 Apr, 2013
Hi Bob,

Your first solution does work. I was using HTML Entity Code for the curly brackets ({ and}) because I thought the CF form I had created to create emails with and save them to the database was not correctly processing curly brackets. I also have an edit option in the form and when I reopen an email for editing, the curly brackets values are missing completely. In fact, they are stored in the database, but the textarea in the form filters the values between curly brackets out. Probably because it considers these items to be variables.

Do you know if there is a way to allow the display of (variables with) curly brackets within the textarea element? I have tried commenting out the curly brackets, to no avail.

Thanks,

Michiel
This topic is locked and no more replies can be posted.