Administer > Status and notifications > Service Manager email solutions > Smart Email > Set up outbound email > Support of cc and bcc fields for outbound emails

Support of the cc and bcc fields for outbound emails

The Service Manager Server now supports the use of the cc, bcc, and reply.to fields when sending email out through the eventout queue.

In email eventout, the evsysopt field is used to determine whether you want to use these fields:

  • If this field is set to 0 or empty, the format for the evfields field is user.to^user.from^user.array^subject^text.
  • If this field is set to 1, the format for the evfields field is user.to^user.from^user.cc^user.bcc^reply.to^subjext^text.

Sample scripts

The following are two sample scripts for your reference.

Sample 1

var eventout = new SCFile("eventout");
eventout.evtype = "email";
eventout.evtime = system.functions.tod();
eventout.evexpire = system.functions.tod();
eventout.evsysseq = String(Number(new Date()));
eventout.evsepchar = "^";
eventout.evfields = "tim@microfocus.com^smith@microfocus.com^John@microfocus.com^marry@microfocus.com^smith@microfocus.com ;John@microfocus.com ^hello^hello all"; eventout.evsysopt = "1"; eventout.doSave();

Sample 2

var GroupTypes = ['assignment', 'cm3groups', 'ocmgroups', 'oncall'];
var GroupInformations = {
  'assignment': {'file': 'assignment', 'nameField': 'name', 'memberField': 'operators'},
  'cm3groups':  {'file': 'cm3groups',  'nameField': 'name', 'memberField': 'members'},
  'ocmgroups':  {'file': 'ocmgroups',  'nameField': 'name', 'memberField': 'members'},
  'oncall':     {'file': 'oncall',     'nameField': 'name', 'memberField': 'schedule', 'subMemberField': 'contact'}
};


function testSendEmail()
{
  var from = 'falcon';
  var toUsers = ['falcon'];
  var toGroups = {'assignment': ['Application', 'ASSET MANAGEMENT'], 'cm3groups': 'Application'}; //2 group types
  var ccUsers = ['Adam.Cailat','Admin.General']; //2 operators
  var ccGroups = {'ocmgroups': ['ASSET MANAGEMENT'], 'oncall': 'HP Admin'}; //2 group types. 1 group for each type
  var bccUsers = ['Adrian.Baxt'];
  var bccGroups = {'assignment': ['Application', 'ASSET MANAGEMENT']}; //2 assignment groups 
  var replyTo = 'falcon';
  var title = 'this is a test email';
  var content = 'this is the email content';

  sendEmail(from, toUsers, toGroups, ccUsers, ccGroups, bccUsers, bccGroups, replyTo, title, content);
}

testSendEmail();


/**
 * Send email by creating a record in the eventout file
 * 
 * toUsers, ccUsers, bccUsers, replyTo can be an JS array of operator/contact/email names or one operator/contact/email name.
 * toGroups, ccGroups, bccGroups should be an JS object. The key of the object is the group type ('assignment' or 'cm3groups' or 'ocmgroups' or 'oncall').
 * The value is an array of groups or one group of that group type.
 */
function sendEmail(from, toUsers, toGroups, ccUsers, ccGroups, bccUsers, bccGroups, replyTo, title, content)
{
  if(from == null)
  {
    from = "";
  }
  
  if( title == null)
  {
    title = "";
  }
  
  if(content == null)
  {
    content = "";
  }
  
  var to = generateEmailAdress(toUsers, toGroups);
  var cc = generateEmailAdress(ccUsers, ccGroups);
  var bcc = generateEmailAdress(bccUsers, bccGroups);
  replyTo = generateEmailAdress(replyTo);
  
  var sep = '^';
  
  var eventout = new SCFile("eventout");
  eventout.evtype = "email";
  eventout.evtime = system.functions.tod();
  eventout.evexpire = system.functions.tod();
  eventout.evsysseq = String(Number(new Date()));
  eventout.evsepchar = sep;
  eventout.evfields = to + sep + from + sep + cc + sep + bcc + sep + replyTo + sep + title + sep + content;
  eventout.evsysopt = "1";
  eventout.doSave();
}


function generateEmailAdress(users, groups)
{
  if(groups == null)
  {
    groups = {};
  }
  
  var emails = {};
  var members = {};
  
  checkUsers(emails, members, users);
  
  var length = GroupTypes.length;
  
  for(var i = 0; i < length; i++)
  {
    var groupType = GroupTypes[i];
    var groupNames = groups[groupType];
    if(groupNames != null)
    {
      getGroupMembers(members, groupNames, groupType);
    }
  }
  
  queryUserEmail(emails, members);
  
  var res = "";

  for( var email in emails )
  {
    if(emails[email] === true)
    {
      res = res + email + ';';
    }
  }
  
  return res;
}

function queryUserEmail(emails, usersObj)
{
  var users = objToArray(usersObj);
  var length = users.length;
  
  if(length === 0)
  {
    return;
  }
  
  var query;
  
  if( length === 1 )
  {
    query = 'name="' + users[0] + '"';
  }
  else
  {
    query = 'name isin ' + system.functions.str(users);
  }
  
  var operator = new SCFile('operator', SCFILE_READONLY);
  operator.setFields(['name','email']);
  res = operator.doSelect(query);
  for(; res === RC_SUCCESS; res = operator.getNext())
  {
    var email = operator.email;
    if(email != null)
    {
      emails[email] = true;
      delete usersObj[operator.name];
    }
  }
  
  users = objToArray(usersObj);
  length = users.length;
  if( length === 1 )
  {
    query = 'contact.name="' + users[0] + '"';
  }
  else
  {
    query = 'contact.name isin ' + system.functions.str(users);
  }
  var contacts = new SCFile('contacts', SCFILE_READONLY);
  contacts.setFields(['email']);
  var res = contacts.doSelect(query);
  for(; res === RC_SUCCESS; res = contacts.getNext())
  {
    var email = contacts.email;
    if(email != null)
    {
      emails[email] = true;
    }
  }
}

function getGroupMembers(members, groupNames, groupType)
{
  if(groupNames == null)
    return members;
  
  var groupInfo = GroupInformations[groupType];
  if(groupInfo == null)
    return members;
    
  return _getGroupMembers(members, groupNames, groupInfo.file, groupInfo.nameField, groupInfo.memberField, groupInfo.subMemberField);
}



function _getGroupMembers(members, groupNames, fileName, nameField, memberField, subMemberField)
{
  var query;
  if (system.functions.type(groupNames) == 8)
  {
    query = nameField + ' isin ' + system.functions.str(groupNames);
  }
  else
  {
    query = nameField + ' = "' + groupNames + '"';
  }
    
  var file = new SCFile(fileName, SCFILE_READONLY);
  file.setFields([memberField]);
  var res = file.doSelect(query);
  
  for(; res === RC_SUCCESS; res = file.getNext())
  {
    var temp = file[memberField];
    if(temp == null)
    {
      continue;
    }
    
    if(subMemberField != null)
    {
      var length = temp.length();
      for(var i = 0; i < length; i++ )
      {
        var userName = temp[i][subMemberField];
        if(userName != null && userName != '')
        {
          members[userName] = true;
        }
      }
      continue;
    }
    
    temp = temp.toArray();
    var length = temp.length;
    for(var i = 0; i < length; i++ )
    {
      var userName = temp[i];
      if(userName != null && userName != '')
      {
        members[userName] = true;
      }
    }
  }
  
  return members;
}

function isEmailAdress(name)
{
  return name.indexOf('@') != -1;
}

function checkUsers(emails, members, users)
{
  if(users == null)
    return;
  
  if(users instanceof Array)
  {
    var length = users.length;
    for( var i = 0; i < length; i++ )
    {
      var value = users[i];
      if(value != null)
      {
        if(isEmailAdress(value))
	    {
	      emails[value] = true;
	    }
	    else
	    {
	      members[value] = true;
	    }
      }
    }
  }
  else
  {
    if(isEmailAdress(users))
    {
      emails[users] = true;
    }
    else
    {
      members[users] = true;
    }
  }
}

function objToArray(obj)
{
  var res = [];
  for( var name in obj)
  {
    if(obj[name] === true)
    {
      res.push(name);
    }
  }
  
  return res;
}

Support of the CC and BCC fields in HTML Email notifications

Out-of-box, the CC and BCC fields are available when sending an HTML Email notification from a record. For details, see Send an HTML email message using the Notify option.