Forums

[SOLVED] Is $emails[0]->template still working in 3.1 RC5.5?

shemzone 18 Sep, 2010
Hello people!
After doing many tests, searching the forum and verifying every setting in Chronoforms, I can't find a solution.

I was using ChronoForms 3.1 RC1 with no problem and made changes on the fly in the email template by using OnSubmit Event before sending email.
This kind of code worked like a charm:
$emails[0]->template = str_replace('##myoldstring##','my new string',$emails[0]->template);

where ##myoldstring## was in my email template.

I installed the last version ChronoForms 3.1 RC5.5 and the replacement doesn't work at all.

I tried different things:
- echo something before sending email => OK
- replace string with something else => OK
- I even rebuilt the form from scratch
Every thing is ok (thanks message, email sent, database records, except the $emails[0]->template = str_replace(...). I can't modify email message on the fly as I did with success with RC1.

Is there something changed since the RC1? Does $emails[0]->template still work?

Thanks a lot for your answers if any.

My installation:
Joomla 1.5.20
PHP 5.2.13
MySql Server:5.0.90
Chronoforms:3.1 RC5.5 <- current version downloaded from Chronoengine website
Chronoforms content plugin: 3.1 RC5.2 <- current version downloaded from Chronoengine website

Regards

Shemzone
nml375 18 Sep, 2010
Hi Shemzone,
The email API has changed since 3.1RC1. If you check the #31 FAQ you'll see samples on how to interact with the email templates in both older and current versions of ChronoForms.

Roughly though, the code you need should look something like this:
<?php
$Form =& CFChronoForm::getInstance('form_name_here');
$Emails =& CFEMails::getInstance($Form->formrow->id);
$template = $Emails->getEmailData(1, 'template');
$template = str_replace('##oldstring##', 'newstring', $template);
$Emails->setEmailData(1, 'template', $template);
?>


/Fredrik
shemzone 18 Sep, 2010
That's it.
Many thanks Fredrik for your quick and accurate support.

Regards
Shemzone (Frédéric 🙂 )
shemzone 18 Sep, 2010
Last question before close as solved definitively this thread.
If I have two different email templates, say one for the customer, one other for the admin.
How can I str_replace for both templates?

What does meant the parameter 1 in class->function $Emails->setEmailData(1,...
I figured it was the number of template, but too easy...
Is there an documentation for the API?
Thanks
Shemzone
nml375 18 Sep, 2010
Hi Shemzone,
The 1 means the first email template of the form, 2 would be the second email template of the form, and so on... (pretty much the same as the number in the old $emails[]-array, just increased by one).

Unfortunately, the best "documentation" I've seen sofar, is the sourcecode of the CFEmails-class (libraries/mails.php).

/Fredrik
shemzone 18 Sep, 2010
So, does this make sense?
1 being the template for customer
2 being the template for admin

$Form =& CFChronoForm::getInstance('ContactM4S');
$Emails =& CFEMails::getInstance($Form->formrow->id);
$template = $Emails->getEmailData(1, 'template');
$templateAdmin = $Emails->getEmailData(2, 'templateAdmin');
$template = str_replace('##sentDate##',strftime('%d/%m/%y'),$template);
$templateAdmin = str_replace('##sentDate##',strftime('%d/%m/%y'),$templateAdmin);
if (JRequest::getVar('Extrainfo') !=''){
	$template = str_replace('##ExtraInfo##','<li>Vous avez écrit le message suivant : '.JRequest::getVar('Extrainfo').'</li>',$template);
	$templateAdmin = str_replace('##ExtraInfoAdmin##','<li>Message : '.JRequest::getVar('Extrainfo').'</li>',$templateAdmin);
}else{
	$template = str_replace('##ExtraInfo##','',$template);
	$templateAdmin = str_replace('##ExtraInfoAdmin##','',$templateAdmin);
}

Shemzone
nml375 18 Sep, 2010
Hi Shemzone,
There is no property named 'templateAdmin'. The exact list of available properties are the column names of the #__chrono_contact_emails database table. The template is always stored in the 'template' property.
Also, you did not restore the updated template text into the CFEmails object. You'll have to use the setEmailData() method for this.

That said, the Extrainfo replacement is not needed, as ChronoForm will do it's own substitution of {fieldname} with the submitted value of that fieldname. So, all you need to do is insert {Extrainfo} into your email templates. Further, there's a fairly simple tweak to do the same with other information, such as your "sentDate":
Add {sentDate} to wherever you'd like the date in your email template, and then add the following into your "on submit - before email" code:
<?php
$time = strftime('%d/%m/%y');
JRequest::setVar('sentDate', 'post', $time);
?>

That would create a fictive form field value named 'sentDate', with the current date as the value. This would then be used by the email template to substitute {sentDate}

/Fredrik
shemzone 19 Sep, 2010
Hi Fredrik
That seems to not work...
Here what I've done (on basicDemo):

In email template, I add {sentDate}
Then in "OnSubmit before" box
<?php
$time = strftime('%d/%m/%y');
JRequest::setVar('sentDate', 'post', $time);
?>


I even tried
<?php
$time = strftime('%d/%m/%y');
JRequest::setVar('sentDate', 'post', $time);
$Form =& CFChronoForm::getInstance('basicDemo');
$Emails =& CFEMails::getInstance($Form->formrow->id);
$template = $Emails->getEmailData(1, 'template');
$Emails->setEmailData(1, 'sentDate', JRequest::getVar('sentDate'));
?>


My var stay as {sentDate}
Are JRequest::setVar and JRequest::getVar really working?
Shemzone
nml375 19 Sep, 2010
Hi Shemzone,
Oh, sorry, I mixed up the order of the parameters. The proper code should look like this:
<?php
$time = strftime('%d/%m/%y');
JRequest::setVar('sentDate', $time, 'post');
?>


Also, you cannot use the setEmailData() method to add values for substitution in your email template. You can only use it to alter the actual properties of the email template(s), such as "Sender email addres", "Sender name", "Recipient", "Subject", email template, and so on (the actual names of these settings correspond to the names of the database table column names, as mentioned earlier).

/Fredrik
shemzone 19 Sep, 2010
Thanks a lot Fredrik
That works
Shemzone
This topic is locked and no more replies can be posted.