Some thoughts on accessing the eMail Templates in the COM API.

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Some thoughts on accessing the eMail Templates in the COM API.

  • Comments 9
  • Likes
Sage CRM allows you to build email Templates and it offers 3 options for sending out emails. But only one of the options allows an obvious way of using the templates.
  1. We can use the internal email client which offers us access to the email templates. These are only available via the interface and are tied to the internal email client.
  2. Send an email using the eWareMessage block, which is typically used in TableLevel scripts and ASP pages. But this can't use the templates
  3. In the Mail Manager we have the specialised Mail Manager objects but these do not use the template.
The email templates are held in the database in a table called emailtemplates. The internal mailmanager will load the template from the database and then merge the contextual information with the #merge# fields. So if the template contains #case_description# and #user_lastname# the internal client uses the information from the context (eWare.GetContextInfo()) to swap in the correct information.

This feature of using the templates, on the face of it, is only available to the Internal Mail Client. But if we consider this code:

var myMailObject = eWare.GetBlock("messageblock");
myMailObject.DisplayForm = false;
myMailObject.mSubject = "Subject Here";
myMailObject.mBody = "Message Goes here";
myMailObject.Mode = 2; //Set Mode to send(2) message is sent immediately.
if (myMailObject.mSentOK)
Response.Write("Email Sent")
Response.Write("There has been a problem: "+myMailObject.mErrorMessage);

The property myMailObject.mBody could get its text from anywhere. As the template is stored in the database there would be nothing to stop us retrieving the text of the template.

For example

var templateRecord = eWare.FindRecord("emailtemplates","emte_id=1");
myMailObject.mBody = templateRecord.emte_comm_email;

And if we wanted to then we could get clever with searching and replacing the merge characters with contextual information.
  • Can you please guide on how to search and replace the merge fields in the email template when using it on COM ASP or Tablescript? I am successful till the email sending, but to merge the contextual information is what I am struggling. "This post says that we get clever with searching and replacing the merge characters with contextual Information". How can I do this? Any help much appreciated.

  • Jeff,

    This is working fine.Thanks!!

    but how to attach documents?I'm using Sage CRM 7.1 SP1.1

  • See the Forum thread:

  • Hi Jeff,

    I know this article is two years old but I'm having an issue  in that the body of the mail is being rendered as HTML not formatted text i.e. I see "..." in the message body although I'm loading in the template as you suggest.


    Record recTemplate = FindRecord("EmailTemplates", "emte_id=7");

    //now we check we've loaded in the template

                       if (recTemplate.RecordCount > 0)



                           //populate with the template

                           //MAIL SUBJECT

                           MyMailSubject = recTemplate.GetFieldAsString("EmTe_Comm_Note").Trim();

                           //UPDATE SUBJECT

                           MyMailSubject = MyMailSubject.Replace("#quot_description#", recQuote.GetFieldAsString("Quot_Description").Trim());

                           MyMailSubject = MyMailSubject.Replace("#quot_poref#", recQuote.GetFieldAsString("Quot_reference").Trim());

                           //MAIL BODY

                           MyMailBody = recTemplate.GetFieldAsString("emte_comm_email").Trim();

                           //Update Mail Body With Quote Data...

                           MyMailBody = MyMailBody.Replace("#quot_contactid#", strPersonGreeting.Trim());

                           MyMailBody = MyMailBody.Replace("#quot_description#",recQuote.GetFieldAsString("Quot_Description").Trim());

                           MyMailBody = MyMailBody.Replace("#quot_opened#", recQuote.GetFieldAsString("Quot_TimeStamp").Trim());

                           MyMailBody = MyMailBody.Replace("#quot_shipaddress#", recQuote.GetFieldAsString("Quot_shipaddress").Trim());

                           MyMailBody = MyMailBody.Replace("#quot_billaddress#", recQuote.GetFieldAsString("Quot_billaddress").Trim());

                          myMailObject = new MessageBlock();

                          myMailObject.DisplayForm = true;.

                          myMailObject.Subject = MyMailSubject;

                           myMailObject.Body = MyMailBody;




  • Sorry that should read I see " <HTML ><HEAD>..."

  • message block still an option to use in version 7.2?

    I tried the recommended code in your post and it appears to be getting hung up on the following line...


    Thank you for your assistance!

  • This is still working fine in Sage CRM 7.3 - I can't swear for 7.2 but I hadn't heard there was an issue.

    Below is the test code that I used.

    var myMailObject = CRM.GetBlock("messageblock");

    with (myMailObject)


     DisplayForm = true;

     mSubject = "Subject Here 2";

     mBody = "Message Goes here";

     mShowCC = true;

     mShowBCC = true;




    var myBlockContainer = CRM.GetBlock("Container");

    with (myBlockContainer)






    if (myMailObject.mSentOK && CRM.Mode==2)









  • Hi Jeff,  I have a js script that sends emails on a scheduled task.  It was working fine in older version on old server.

    Now I have migrated everything to a new server (Server 2012 R2 Standard 64 bit) and upgraded to 7.3 SP2.1.

    The email send script is no longer working.

    I tried taking your example below and stripping it down to run as a stand-alone js script, but it is not working either.

    Here is what I have -

    var username = "test";

    var password = "test";

    var CRM = new ActiveXObject("eWare.CRM");

    CRM.FastLogon = 3; //this prevents the meta data from loading.


    var myMailObject = CRM.GetBlock("messageblock");

    with (myMailObject)


    DisplayForm = false;

    mSubject = "Subject Here 2";

    mBody = "Message Goes here";

    mShowCC = false;

    mShowBCC = false;




    I have this script in a file called emailtest.js and I launch it like this -

    C:\WINDOWS\SysWOW64\wscript.exe emailtest.js

    The logs show errors referencing impersonated user, but I have checked those settings and they look correct.

    Jul 25 2016 9:46:12.602 1592 4532 1 startup,version,dllsize,dlldate,exehost,imagebase,reference Sage CRM 7.3 SP2.1 11630080 May 18 2016 8:44:38.000 C:\WINDOWS\SysWOW64\wscript.exe 1A8C8A50 #line#

    Jul 25 2016 9:46:15.556 1592 4532 1 Metadata 2843

    Jul 25 2016 9:46:15.634 1592 4532 5 LoadUserSession: RemoteAddress not found

    Jul 25 2016 9:46:15.556 1592 4532 1 Metadata 2843

    Jul 25 2016 9:46:15.634 1592 4532 5 LoadUserSession: RemoteAddress not found

    Jul 25 2016 9:46:15.649 1592 4532 1 PrepImpersonatefail,Error OpenThreadToken failed<br />An attempt was made to reference a token that does not exist<br />

    Jul 25 2016 9:46:15.649 1592 4532 1 ** Created global user activity thread **

    Jul 25 2016 9:46:15.649 1592 9208 1 ImpersonateThread.Impersonate,Error ImpersonateLoggedOnUser failed<br />The handle is invalid<br />

    Jul 25 2016 9:46:15.806 1592 4532 3 CoeWareBase,Time 3203

    Jul 25 2016 9:46:15.806 1592 4532 1 shutdown

    Currently impersonated user is domain admin.

    Any suggestions?



  • Kevin

    I am not in a position to test this code on my machine, but the code that you've posted looks reasonable.  

    The Impersonate error is one that occurs around emails.  I would look at the settings again.  I would check that emails work elsewhere in the system.