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

1 minute read time.
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.AddRecipient("[email protected]","Training","TO");
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.
  • 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.

  • FormerMember

    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;

    AddRecipient("[email protected]","[email protected]","TO");



    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
    An attempt was made to reference a token that does not exist

    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
    The handle is invalid

    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?



  • 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;

    AddRecipient("[email protected]","Training","TO");

    AddRecipient("[email protected]","Support","CC");


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

    with (myBlockContainer)






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









  • 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!