Thursday, June 9, 2011

SugarCRM Customization: Workflow Emails with Templates

Some time ago I posted an article describing the manner in which one can send custom email messages via a logic hook. Since then, several folks have asked if it is possible to leverage the email template system built into SugarCRM for generating the actual message. 

It is certainly possible and that is exactly what we will cover in this post.

To save ourselves some time, we will simply modify the script from the previous post and change the relevant portions so as to cause it to use an e-mail template instead of the file based template it previously used. But before that, we need to create the e-mail template.

Create your e-mail template as you would any other, following these steps:

1. Select Emails > Create Email Template within SugarCRM 
2. Compose your message and click Save when done 

Next, we need the ID of the email template you just created. This will allow us to leverage it within the script, giving us our desired result. 

To retrieve the ID:

1. Select Emails > View Email Templates  
2. Click on the email template you just created
3. Copy the content from the address field in your web browser to your clipboard
4. Paste the content to Notepad or other format where it can be easily inspected

Once pasted, you should find that the address field contains something similar to the following:


The portion to the right of the equal sign is what we will need. 

With the preliminary work out of the way, we can proceed to modify the script that sends the message. If you want to see the original script, take a look at the original post

Here is the modified version, using the email template we just created:


class Welcome {

function sendMessage(&$bean, $event, $arguments)
//Grab the Lead's Status (only send if Status = New)
$status = $bean->status;

//Get the TO name and e-mail address for the message
$rcpt_name = $bean->first_name . ' ' . $bean->last_name;
$rcpt_email = $bean->email1;

//Check the status on Lead record
if ($status == 'New')
//Send welcome msg to Lead

$emailTemp = new EmailTemplate();
$emailTemp->disable_row_level_security = true;

//This is where we use the ID value of the email template record

$mail = new SugarPHPMailer();

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

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


$mail->AddAddress($rcpt_email, $rcpt_name);
$mail->Subject = from_html($emailTemp->subject);

$mail->Body_html = from_html($emailTemp->body_html);
$mail->Body = wordwrap($emailTemp->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');



Now, put it to use.


  1. Hi Angel,

    Using templates will certainly help our business manage the content of their own outgoing mail.

    Question for you: Does the SugarPHPMailer class handle file attachments?

    How would you enhance this code to attach, for example, the head revision of a related Document?

  2. @Mark:

    Indeed, the AddAttachment() method in SugarPHPMailer handles attachments.

    1. Can we add multiple email addresses in to field

    2. I've read some messages on this particular topic and it seems like the capability is not built-in. I haven't tried it myself to know for sure, but that seems to be the conclusion.

  3. Great stuff! very interesting to read this post..Thanks!

    Email Templates

  4. Hi Angel,

    When I receive an email using an email template it emails the variables inside the email instead of the values of the variables.

    When I receive the email it has

    I tried to comment out your convert to html line, but it didn't work... any ideas/hints?

  5. @Unknown, yes that would make sense as the code I've given doesn't handle the part responsible for parsing out the variables and putting in their respective values.

    I am not entirely sure where that is handled, but I would take a look at the code in SugarPHPMailer.php.

  6. Hi,
    The email sent like this does not appear in "Sent Mails" and hence in the history of email exchanges between the default email id and the recipient.

    Please help how to achieve this ?

    1. Just create a Email Bean and save:
      global $current_user;
      global $timedate;
      $emailObj = new Email();

      $emailObj->assigned_user_id = $current_user->id;
      $emailObj->created_by = $current_user->id;
      $emailObj->date_sent = $timedate->now();
      $emailObj->type = 'out';
      $emailObj->status = 'sent';
      $emailObj->from_addr_name = $current_user->user_name." <".$current_user->email1.">";
      $emailObj->to_addrs_names = $rcpt_email." <".$rcpt_name.">";
      $emailObj->name = from_html($emailTemp->subject);
      $emailObj->description = from_html($emailTemp->body_html);
      $emailObj->description_html = wordwrap($emailTemp->body_html, 900);


  7. hello...

    I followed the tutorial and at the end, when I send the email, it comes with variables (eg $name) instead of the values ​​... I'm using version 6.5.7 ...

    I hope you can help me with this problem ...

    thanks in advance! ... sorry for my bad English

    1. The tutorial doesn't address that particular need. There is function for parsing those variables, but it is not part of this example. I think it is in SugarPHPMailer.php, but I am not sure.

      Another option is to perform a string replace via PHP.

  8. Your message help me!
    Very Thank's!!!

  9. Hi,
    The email sent like this does not appear in "History" with associated contact.

    Please help how to achieve this ?

  10. Hi, I found why it is no saved to history!

    needed add this 2 things -

    $emailObj->parent_type = 'Contacts';
    $emailObj->parent_id = $idContact;

  11. Hi is it possible to combine this php with a query i found in this website's posts to create something like this:

    Hi Everyone,

    I am creating an automated Email system for a shipping Company.

    I have no knowledge of programming but I want to send an email every time I "create a new Load" on my "Load Board" custom module I did with Module Builder. The Information of the new Load I created have to be sent after saved to a list of Curriers I have on a "Curriers" Module I created with Module Builder. The Email have to be sent only if the status field of the Currier is "registered". My SuiteCRM is 7.9.7.

    I read I need to create a Logic Hooks for that, so I am searching the Code to make it works. I found I need to create two files: "logic_hooks.php" and "send _email.php" and put it on modules/te_lb_load_board folder.

    As I understand the system has to take the emails list from database mysql, check if the staus is "registered", take the custom email template I created with the "Email Template" module and send it with SugarPHPMailer.php

    Can someone please tell me if I am right?

  12. How many editions are available in suitecrm apart from community version

    1. Apologies, I don't dabble in SuiteCRM so I am not familiar with the nuances of their products. I would recommend you contact them directly.


Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.