Greetings from Urdhva tech!
Here is an code snippet how SugarCRM parse an Email Template.
Let's have an example
Here I have created email template from Emails->Create Email Template
Template body :
any custom script.php
<?php
$template_name = 'Greetings from Urdhva-tech';
require_once('modules/EmailTemplates/EmailTemplate.php');
$template = new EmailTemplate();
$template->retrieve_by_string_fields(array('name' => $template_name,'type'=>'email'));
$oContact = new Contact();
$oContact->retrieve("36CharacterContactID"); //Contact ID
//Parse Subject If we used variable in subject
$template->subject = $template->parse_template_bean($template->subject,$oContact->module_dir, $oContact);
//Parse Body HTML
$template->body_html = $template->parse_template_bean($template->body_html,$oContact->module_dir, $oContact);
//Here you will have a result
print "<pre>";
print_r(from_html($template->body_html));
Output of script will be this.
Sallie Elzey is the name of contact which i used at time of contact retrieve.
Hello Sallie Elzey,
Greetings from Urdhva-tech.
Here we have example of some variable.
Thanks
Chris Olliver
Follow us on twitter: @Urdhvatech
Contact us at contact@urdhva-tech.com for any services related to SugarCRM.
Here is an code snippet how SugarCRM parse an Email Template.
Let's have an example
Here I have created email template from Emails->Create Email Template
Template body :
Hello $contact_name,
Greetings from Urdhva-tech.
Here we have example of some variable.
Thanks
$contact_user_full_name
$contact_user_phone_mobile
Greetings from Urdhva-tech.
Here we have example of some variable.
First Name : | $contact_first_name |
Last Name : | $contact_last_name |
Email : | $contact_email1 |
Thanks
$contact_user_full_name
$contact_user_phone_mobile
any custom script.php
<?php
$template_name = 'Greetings from Urdhva-tech';
require_once('modules/EmailTemplates/EmailTemplate.php');
$template = new EmailTemplate();
$template->retrieve_by_string_fields(array('name' => $template_name,'type'=>'email'));
$oContact = new Contact();
$oContact->retrieve("36CharacterContactID"); //Contact ID
//Parse Subject If we used variable in subject
$template->subject = $template->parse_template_bean($template->subject,$oContact->module_dir, $oContact);
//Parse Body HTML
$template->body_html = $template->parse_template_bean($template->body_html,$oContact->module_dir, $oContact);
//Here you will have a result
print "<pre>";
print_r(from_html($template->body_html));
Output of script will be this.
Sallie Elzey is the name of contact which i used at time of contact retrieve.
Hello Sallie Elzey,
Greetings from Urdhva-tech.
Here we have example of some variable.
First Name : | Sallie |
Last Name : | Elzey |
Email : | example-sugarcrm@gmail.com |
Thanks
Chris Olliver
Follow us on twitter: @Urdhvatech
Contact us at contact@urdhva-tech.com for any services related to SugarCRM.
Hi,
ReplyDeleteThanks for this great article. I am so close to getting it working, except that for me the $template->parse_template_bean() call isn't replacing my template variables with data.
I've debugged the $oContact object and it's successfully retrieving data into it.
Can you post the code snippet?
DeleteHere is the full function with a few things changed for annonymity's sake. All debug lines work - I see them in my sugar log. Its just when I get the email (for either template A or template B) It contains all the template variables, - the data I want to see is not showing int he template. e.g. I see {::future::Contacts::first_name::} {::future::Contacts::last_name::} instead of Anandra Umagum in the email I receive.
ReplyDelete---------------------------------------
debug('*** Entering MyFunction function ***');
// database object
global $db;
//define sql query - do something
$sql = "call AStoredProcedure";
//run the query
$rs = $db->query($sql);
$sql = "select * from contacts_cstm";
$rs = $db->query($sql);
while ( ($row = $db->fetchByAssoc($rs)) ) {
$GLOBALS['log']->debug(' * I got a row! * ');
if ($row['acustomfield_c'] <= 0 && $row['acustomfield_c'] != 1 ) {
$GLOBALS['log']->debug($row['anothercustomfield_c']);
$template_name = 'My Template Based On Contact Module A';
$send_email = 1;
}
if ($row['acustomfield_c'] > 0 && $row['acustomfield_c'] != 1 ) {
$GLOBALS['log']->debug($row['anothercustomfield_c']);
$template_name = 'My Template Based On Contact Module B';
$send_email = 1;
}
if ($send_email == 1) {
require_once('/home/homeuser/www/sugarfolder/modules/EmailTemplates/EmailTemplate.php');
$template = new EmailTemplate();
$template->retrieve_by_string_fields(array('name' => $template_name,'type'=>'Workflow'));
$GLOBALS['log']->debug('*** email template is ' . print_r($template, true));
$oContact = new Contact();
$oContact->retrieve($row['id_c']); //Contact ID
$GLOBALS['log']->debug('*** Contact is ' . print_r($oContact, true));
//Parse Body HTML
$template->body_html = $template->parse_template_bean($template->body_html,$oContact->module_name, $oContact);
require_once('/home/homeuser/www/sugarfolder/include/SugarPHPMailer.php');
$mail = new SugarPHPMailer();
require_once('/home/homeuser/www/sugarfolder/modules/Emails/Email.php');
$emailObj = new Email();
$defaults = $emailObj->getSystemDefaultEmail();
$mail->From = $defaults['email'];
$mail->FromName = $defaults['name'];
$mail->ClearAllRecipients();
$mail->ClearReplyTos();
$mail->AddAddress('anandra@umagum.com.xx', 'Anandra Umagum');
$mail->Subject = from_html($template->subject);
$mail->Body_html = from_html($template->body_html);
$mail->Body = wordwrap($template->body_html, 900);
$mail->IsHTML(true); //Omit or comment out this line if plain text
$mail->prepForOutbound();
$mail->setMailerForSystem();
//Send the message, log if error occurs
if (!$mail->Send());
{
$GLOBALS['log']->fatal('*** ERROR: Message Send Failed');
}
}
}
$GLOBALS['log']->debug('*** Exiting MyFunction function ***');
return true;
}
?>
----------------------------------------
Hello,
DeleteIn your code, you have
$template->subject = $template->parse_template_bean($template->subject,$oContact->module_name, $oContact);
Please change $oContact->module_name to $oContact->module_dir and it should fix it for you.
Thanks! I actually saw this difference before contacting you. $oContact->module_name and $oContact->module_dir both return 'Contacts' so there isn't a difference which one you use.
ReplyDeleteI am still having the same result. :(
I wonder if there is a known bug with that parse_template_bean method?
I have now found the cause! the parse_template_bean method only knows how to read variables from an EMAIL-type template, not a WORKFLOW-type template. All is working now, but thanks for your facility and allowing discussion of these issues.
ReplyDeleteIts good to know that you got it working. We always welcome discussions! Have a happy end of the year, and happy new year! :)
Delete