1. Script for sending an email

    Article: AN0001919Updated: 28.03.2020

    The simplest way of sending email from ObjectGears is use of notifications, e.g. in rules in which you can set conditions when the rule (sending an notification) should occur. At the same time you can define in rules also to whom the notification should be sent (distribution group, users stated in a record column...).

    You can also use a script and objekt OG.Email for a common solution of sending email notifications. Examples below demonstrate some of many possibilities that this object provides.

    Sending an email compiled in a script

    var m = OG.Email.CreateMessage();


    m.Subject = 'Hello word';
    m.Body = 'body {{var.prom1}}';

    m.AddVariable('prom1', 'PRO-1');//adding variable that is used in the email body
    m.SendEmailAsOne = true;   //sending as a single email, not  separately to each recipient


    Using notification in a script

    We do not need to define the email body and subject directly in the script. We can refer to an existing notification that has Body and Subject already defined. In such a case we add in the script variables that the notification works withs.

    Unlike in the previous case we will not use method OG.Email.Send, but OG.Email.SendNotification.

    This method is sending a notification that can have defined various language versions and users then receive email in the language according to their settings.

    var m = OG.Email.CreateMessage();

    //Subject and body does not need to be defined in the script. Instead we can use an existing notification that already contains Subject and body.
    m.Notification = OG.Notification.GetAll().GetDefaultByCode( model.Id, 'timewriting_reject');

    //email template s ID = 1
    m.EmailTemplateId = 1;   

    //Adding variables that are used in the notification
    m.AddVariable('solver', 'my solver');
    m.AddVariable('pro_code', 'my project code');
    m.AddVariable('pro_name', 'my project name');
    m.AddVariable('pro_ict_man', 'my project manager');
    m.AddVariable('datarow.url', OG.GetWebUrl() + '' + OGActualDataRow.DetailUrl);


    Notification with distribution group, variables and URL link in the email

    This command sends a notification 'request_created' from model 'it' to distribution group 'service_desk'. You can use command {{var.calcfee}}, which contains a text compiled in the below stated example, in the notification 'request_created'. You can use whatever number of variables like variable 'abc' utilizing method OG.CreateHashtable and parameter ht in this example. You can use also e.g. command {{var.DataRow.Url}}, that inserts URL to the data record with which the notification is associated via parameter OGActualDataRow.

    var ht = OG.CreateHashtable();

    ht['calcfee'] = 'Calculated fee: 78,00 USD.';

    OG.Email.SendNotification('it', 'request_created', 'service_desk', OGActualDataRow, ht, Com.ObjectGears.Common.Entities.EmailOwnerType.ClassDef, OGActualDataRow.ParentId);

    Attaching files

    We can also attach files to the email. Below example shows adding three attachments. The first one is a text file with a name and content defined in the script. The second one is a picture stored on disk in a relative path and the third one is file referred with an absolute path. Both file have to be accessible by ObjectGears technical account (account under which Windows service or web are running, depending on which component secures email sending). According to the file type state the corresponding Mime type.

    var m = OG.Email.CreateMessage();

    m.SendEmailAsOne = true; //send as a single email
    m.Subject = 'Subj';
    m.Body = 'Body';

    // Adding a simple text file compiled in the script
    m.Attachements.AddFromText('This is a text that will be in the file', 'File name.txt', 'ct');

    // Adding file with a relative path
    var file = OG.Utils.MapServerPath('~/ImagesData/system/user_use.png');
    var bytes = OG.Utils.ReadFileToBytes(file);
    m.Attachements.AddFromBytes(bytes, 'user.png', 'image/x-png');

    // Adding file with an absolute path
    var file = 'C:\\R5\\Export\\Server_status.csv';
    var bytes = OG.Utils.ReadFileToBytes(file);
    m.Attachements.AddFromBytes(bytes, 'Přehled serverů.csv', OG.Email.MimeType_Text_Plain);


    Assigning email to a record

    If we want to assign the email to an object which it relates to, we will use property m.AssignEmailForDataRow. Email will then be displayed also in the detailed archive of the given record. In the example below we are using actual record (for use e.g. in the rule after record saving). Another possibility is to determine the record, to which the email should be assigned, in a common way by means of record reading.

    var m = OG.Email.CreateMessage();

    m.SendEmailAsOne = true; //odeslat jako jeden email
    m.Subject = 'Subj';
    m.Body = 'Body';

    m.AssignEmailForDataRow = OGActualDataRow


    Sending notification to a person stated in a referred class (e.g. email about task to a project manager)

    In some cases we want to send a notification about a record to a person stated in another task, to which our record referring o. A good example is a notification about a task to the manage of project, to which our task belongs.

    Relationship of both classes is described by the following scheme.

    In the class task we will create rules of type Script - After saving a new record and After saving an existing record.

    /*The script will send a notification 'task' to the distribution group 'task', to the user from colum 'solver' of he class 'task' and to the user 'project_manager' from the referred class 'project'. It will also link he email to the given record in order it is displayed in the record audit-trail.

    var model = OGDataParent.Model;
    var m = OG.Email.CreateMessage();

    m.Notification = OG.Notification.GetAll().GetDefaultByCode( model.Id, 'task');
    //adding recipents from the distribution grou 'task' - links both to users and email addresses stated as text
    var ds = OG.DistributionGroup.GetAll().GetByModelId(model.Id, false).GetByCode('task');

    //add solver of the task

    //add project manager in email cc

    //attach email to the record in order the sent email is displazed in the record audit-trailu
    m.AssignEmailForDataRow = OGActualDataRow;