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.
Response.Write(myMailObject.Execute());
if (myMailObject.mSentOK)
{
Response.Write("Email Sent")
}
else
{
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.
Parents
  • 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)

    {

    AddBlock(myMailObject);

    }

    CRM.AddContent(CRM.Mode);

    CRM.AddContent(myMailObject.Execute());

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

    {

    Response.Redirect(CRM.URL(184));

    }

    else

    {

    CRM.AddContent(myMailObject.mErrorMessage);

    }

    Response.Write(CRM.GetPage());

Comment
  • 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)

    {

    AddBlock(myMailObject);

    }

    CRM.AddContent(CRM.Mode);

    CRM.AddContent(myMailObject.Execute());

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

    {

    Response.Redirect(CRM.URL(184));

    }

    else

    {

    CRM.AddContent(myMailObject.mErrorMessage);

    }

    Response.Write(CRM.GetPage());

Children
No Data