How To Parse Email Template Variable In SugarCRM /SuiteCRM By Custom Script

Greetings from TechCRM!

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 TechCRM.
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 any_name.php

<?php

$template_name = 'Greetings from TechCRM';
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.
Michael Servetnick is the name of contact which i used at time of contact retrieve.


Hello Sallie Elzey,
Greetings from TechCRM.
Here we have example of some variable.
First Name :	Michael 
Last Name :	Servetnick 
Email :	example-sugarcrm@techcrm.in
Thanks
Navin Rakhonde

Note :

1. Here, <modulename> means the module name you see in the URL, for example, Contacts, Leads, Accounts, etc.
2. <any_name> means anything as per your requirements

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at info@techcrm.in

Thank you.

Spread the love

2 thoughts on “How To Parse Email Template Variable In SugarCRM /SuiteCRM By Custom Script

Leave a Reply

Your email address will not be published. Required fields are marked *