1. Script for sending an email

    Article: AN0001919 Updated: 04.11.2018 Displayed: 1125x

    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');


    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


  2.    ---
    Your rating:
    Rated by users: 0x
  3. Top