How to Parse Email template variable in SugarCRM by custom script

sugarcrmdevelopers —  July 24, 2013
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

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.