Wednesday, 17 July 2013

How to Parse Email template variable in SugarCRM by custom script

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 :
Hello $contact_name,
Greetings from Urdhva-tech.
Here we have example of some variable.
First Name : $contact_first_name
Last Name : $contact_last_name
Email : $contact_email1



any custom script.php


$template_name = 'Greetings from Urdhva-tech';
$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>";

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 :

Chris Olliver

Follow us on twitter: @Urdhvatech

Contact us at for any services related to SugarCRM.


  1. Hi,

    Thanks 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.

  2. Here 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.


    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 ) {

    $template_name = 'My Template Based On Contact Module A';

    $send_email = 1;

    if ($row['acustomfield_c'] > 0 && $row['acustomfield_c'] != 1 ) {

    $template_name = 'My Template Based On Contact Module B';

    $send_email = 1;

    if ($send_email == 1) {
    $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);

    $mail = new SugarPHPMailer();

    $emailObj = new Email();
    $defaults = $emailObj->getSystemDefaultEmail();

    $mail->From = $defaults['email'];
    $mail->FromName = $defaults['name'];


    $mail->AddAddress('', '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

    //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;



    1. Hello,

      In 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.

  3. 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.

    I am still having the same result. :(

    I wonder if there is a known bug with that parse_template_bean method?

  4. 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.

    1. Its good to know that you got it working. We always welcome discussions! Have a happy end of the year, and happy new year! :)


Urdhva Tech Pvt. Ltd. Powered by Blogger.

Urdhva Tech

My Photo
SugarCRM Consultants Contact us at Skype : urdhvatech

Search This Blog


Stay connected Socially


Total Pageviews

Popular Posts