Pages

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:


&record=9940c799-359b-f2fb-2943-4c619dfc696f  


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:



<?php


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
include('modules/EmailTemplates/EmailTemplate.php');


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

//This is where we use the ID value of the email template record
$emailTemp->retrieve('9940c799-359b-f2fb-2943-4c619dfc696f'); 

require_once('include/SugarPHPMailer.php');
$mail = new SugarPHPMailer();


require_once('modules/Emails/Email.php');
$emailObj = new Email();
$defaults = $emailObj->getSystemDefaultEmail();


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


$mail->ClearAllRecipients();
$mail->ClearReplyTos();


$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
$mail->prepForOutbound();
$mail->setMailerForSystem();


//Send the message, log if error occurs
if (!$mail->Send());
{
$GLOBALS['log']->fatal('ERROR: Message Send Failed');
}
}
}


}


?>

Now, put it to use.

17 comments:

  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?

    ReplyDelete
  2. @Mark:

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

    ReplyDelete
    Replies
    1. Can we add multiple email addresses in to field

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

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

    Email Templates

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

    Example:
    When I receive the email it has
    :{::future::Case::rdy_no_c::}

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

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

    ReplyDelete
  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 ?

    ReplyDelete
    Replies
    1. Just create a Email Bean and save:
      global $current_user;
      global $timedate;
      require_once('modules/Emails/Email.php');
      $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);

      $emailObj->save();

      Delete
  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

    ReplyDelete
    Replies
    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.

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

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

    Please help how to achieve this ?

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

    needed add this 2 things -

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

    ReplyDelete
  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?

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

    ReplyDelete
    Replies
    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.

      Delete

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