Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Email

Send an email to selected users

Scroll Prev Next More

 

Sometimes you need to send an email to selected users. Let's say one of the fields on a page, EmailField contains email addresses. The key field on the page is KeyColumn, table name - TableName.

 

Let's consider two situations:

Subject and text of the email determined directly in the event

 

1. Proceed to the Page Designer screen.

 

2. Create a custom button (e.g., "Email selected") and add the following code to the Server tab:

 

var emails = new StringBuilder();
 
XVar data;
while(data = button.getNextSelectedRecord())
{
  if(data["EmailField"])
  {
    if(emails.Length > 0)
        emails.Append(", ");
    if(data["EmailField"] != "")
        emails.Append(data["EmailField"].ToString());
  }
}
 
// send the email
XVar rmail = XVar.Array();
rmail["to"] = emails.ToString();
rmail["subject"] = "Sample subject";
rmail["body"] = "Your email message here";
var res = MVCFunctions.runner_mail(rmail);
result["txt"] = "Emails were sent.";
 
if (!res["mailed"])
{
  result["txt"] = "Error happened: " + res["message"].ToString();
}

 

and to the Client After tab:

 

var message = result["txt"];
ctrl.setMessage(message);

 

Note: The Client Before tab should be blank (delete sample code there if any).

User enters the subject, email body, "from" email address

 

1. Proceed to the Page Designer screen and select the List page.

 

2. Create a custom button (e.g., "Email selected").

 

Add the following code to the Client Before tab:

 

if ( proxy["emailUsers"] === true ) {  
   params["emailFrom"] = proxy["emailFrom"];
   params["emailBody"] = proxy["emailBody"];
   params["emailSubject"] = proxy["emailSubject"];
 
   proxy["emailUsers"] = false;
  return true;
}  
 
var selBoxes = pageObj.getSelBoxes( pageid ),
   args = {
       modal: true,
       header: "Input from, subject and body",
       html: '<div>'
          + '<div>From: <input type="text" id="emailFrom"s style="margin: 5px 0"></div>'
          + '<div>Subject: <input type="text" id="emailSubject"s style="margin: 5px 0"></div>'
          + '<div>Body: <textarea id="emailBody"></textarea></div>'
          + '</div>',
       footer: '<a href="#" id="emailUsersSave" class="btn btn-primary">' + Runner.lang.constants.TEXT_SAVE + '</a>'
           + '<a href="#" id="emailUsersCancel" class="btn btn-default">' + Runner.lang.constants.TEXT_CANCEL + '</a>',
       afterCreate: function( win ) {
          $("#emailUsersSave").on("click", function( e ) {
              var context = win.body();
 
               proxy["emailUsers"] = true;
               proxy["emailFrom"] = $("#emailFrom", context).val();
               proxy["emailBody"] = $("#emailBody", context).val();
               proxy["emailSubject"] = $("#emailSubject", context).val();
              $('[id="' + ctrl.id + '"]').click();
 
               e.preventDefault();              
               win.destroy();
           });
 
          $("#emailUsersCancel").on("click", function( e ) {
               e.preventDefault();
               win.destroy();                        
           });        
       }
   };
 
if ( selBoxes.length == 0 || !confirm('Do you really want to email these records?') ) {
  return false;
}
 
Runner.displayPopup( args );
return false;

 
Server tab:

 

if(parameters["emailBody"] || parameters["emailSubject"] || parameters["emailFrom"] )
{
  var emails = new StringBuilder();
 
  XVar data;
  while(data = button.getNextSelectedRecord())
  {
    if(data["EmailField"])
    {
        if(emails.Length > 0)
          emails.Append(", ");
        if(data["EmailField"] != "")
          emails.Append(data["EmailField"].ToString());
    }
  }
 
  // send the email
  var rmail = XVar.Array();
  rmail["from"] = parameters["emailFrom"];
  rmail["to"] = emails.ToString();
  rmail["body"] = parameters["emailBody"];
  rmail["subject"] = parameters["emailSubject"];
  var res = MVCFunctions.runner_mail(rmail);
   
  result["txt"] = "Emails were sent.";
 
  if (!res["mailed"])
  {
    result["txt"] = "Error happened: " + res["message"].ToString();
  }
}

 

Client After tab:

 

var message = result["txt"] + " !!!";
ctrl.setMessage(message);

 

Note: After selecting record(s) on the List page and clicking the "Email selected" button, a popup appears where you can enter From, Subject and Body parameters of your letter.

Click the "Save" button to send an email to selected user(s).

See also:

Grid Row Javascript API: row.getKeys()

JavaScript API:getSelectedRecordKeys()

Button object: getNextSelectedRecord()

Send an email to all users

Send simple email

runner_mail function

Email selected records

About Tabs/Sections API

JavaScript API: RunnerPage object

AJAX helper object: setMessage()

About Dialog API