Sending Emails in ASP 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.

Sending Emails in ASP COM API

  • Comments 9
  • Likes
 
 


Sage CRM offers a very simple MessageBlock object that you can use for your own pages. This object can either be used automatically to send a message without an interface prompt or it can create a form allowing the details of the email message to be completed.

An example use of the MessageBlock that sends the email directly without prompting the users is shown below:


[code language="javascript"]
var myMailObject = CRM.GetBlock("messageblock");
with (myMailObject)
{
DisplayForm = false;
mSubject = "Subject Here";
mBody = "Message Goes here";

AddRecipient("training@sagecrm.com","Training","TO");
Mode = 2;
Response.Write(Execute());
if (mSentOK)
{
Response.Write("Email Sent")
}
else
{
Response.Write("There has been a problem: "+mErrorMessage);
}
}
[/code]

This can be changed to work within Tablelevel and Validation scripts.

To create a form that allows the user to complete the message then this code should give you an idea:

[code language="javascript"]
//Create a Visible Message Block and Set Properties
var myMailObject = CRM.GetBlock("messageblock");
with (myMailObject)
{
//works with setting made within CRM administration screens
//hide email form;
DisplayForm = true;
mSubject = "Subject Here";
//mBody is Automatically truncated to 160 chars for SMS
mBody = "Message Goes here";
mShowCC = true;
mShowBCC = true;
//TO|CC|BCC are valid recipients
AddRecipient("somebody@sagecrm.com","Training","TO");
AddRecipient("another@sagecrm.com","Support","CC");
AddRecipient("person@sagecrm.com","Manager","BCC");
Response.Write(Execute());
}
[/code]


Note: The MessageBlock object however does not offer the ability to add attachments.

To work with attachments in an ASP page then you may want to consider something like the CDONTS object. This will offer the ability to add attachments. 

[code language="javascript"]
var MyCDONTSMail = CreateObject("CDONTS.NewMail");
MyCDONTSMail.From= "somebody@nowhere.com";
MyCDONTSMail.To= "nobody@nowhere.com";
MyCDONTSMail.Subject="This is a Test";
MyBody = "Thank you for ordering that stuff";
MyCDONTSMail.Body= MyBody;
MyCDONTSMail.Send();
[/code]

The only problem is that CDONTS or any other object won't create the e-mail form fields on the ASP page for you. These would have to be created manually by you. I have covered how to write screen not linked to a table in previous posts. 

Note: You will see in the documentation that there are other objects related to emails, e.g. the MsgHandler Object. The MsgHandler, Email Object, AddressList Object, MailAddress, Attachment, and AttachmentList objects are all only appropriate to the Mail Manager script templates.



If you really want a sophisticated email for then you could try using the internal action that is responsible for the Internal Email Client.
 
Comments
  • I am using the following code which is outlined above.  It works great.  The only issue is that when the email is sent the To and CC line contain each recipients name twice.  For example, if I used the exact code below for the recipients the To and CC line would read:

    To: Training; Training

    CC: Support; Support

    Any ideas how to stop the duplication of the recipients?  They only receive 1 email, but it's annoying to have the names duplicated.

    //Create a Visible Message Block and Set Properties

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

    with (myMailObject)

    {

    //works with setting made within CRM administration screens

    //hide email form;

    DisplayForm = true;

    mSubject = "Subject Here";

    //mBody is Automatically truncated to 160 chars for SMS

    mBody = "Message Goes here";

    mShowCC = true;

    mShowBCC = true;

    //TO|CC|BCC are valid recipients

    AddRecipient("somebody@sagecrm.com","Training","TO");

    AddRecipient("another@sagecrm.com","Support","CC");

    AddRecipient("person@sagecrm.com","Manager","BCC");

    Response.Write(Execute());

    }

  • Danny

    If the code is adding a mysterious repeat then it may be a bug and is worth logging this.

  • Hey Jeff, this was awesome thanks, my only issue now is how to make mBody = "Message Goes here";  equal the text thats in a particular field at the time

  • If you are handling a submitted form from and ASP page you could access the data using

    mBody = Request.Form("fieldname");

  • You Sir, are a legend!!!!

  • Do we need to add any library file to create CDONTS object? As we are not able to create that object.

    Please suggest.

  • Deepak

    Have a look at this Microsoft support article that explains about CDO libraries.  support.microsoft.com/.../171440

  • hello, i tried same script but it did not work. any suggestions. thanks

  • This article is quite old, and I have not review it.  It maybe that you will need to do more research on creating emails based on the appropriate email objects available within your server.