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:

 

dim rmail, data, emails
set rmail=CreateDictionary()
emails = ""
do while bValue(DoAssignment(data,button.getNextSelectedRecord()))
  if bValue(data("EmailField")) then
    if emails<>"" then
        emails = emails & ", "
    end if
     emails = emails & data("EmailField")
  end if
loop
 
' send the email
rmail("to")=emails
rmail("subject")="Sample subject"
rmail("body")="Your email text here"
set ret = runner_mail(rmail)
result("txt") = "Emails were sent."
 
' if error happened print a message on the web page
if not ret("mailed") then
  errmsg = "Error happened: <br>"
  errmsg = errmsg & "File: " & ret("source") & "<br>"
  errmsg = errmsg & "Line: " & ret("number") & "<br>"
  errmsg = errmsg & "Description: " & ret("description") & "<br>"
  result("txt") = errmsg
end if

 

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 bValue(params("emailBody")) or bValue(params("emailSubject")) or bValue(params("emailFrom")) then
  dim emails, data, rmail,errmsg
  set rmail = CreateDictionary()
  do while bValue(DoAssignment(data,button.getNextSelectedRecord()))
  if bValue(data("EmailField")) then
    if emails<>"" then
        emails = emails & ", "
    end if
  emails = emails & data("EmailField")
  end if
  loop
 
  ' send the email
  rmail("to")=emails
  rmail("from") = params("emailFrom")
  rmail("subject")=params("emailSubject")
  rmail("body")=params("emailBody")
  set ret = runner_mail(rmail)
  result("txt") = "Emails were sent."
 
  ' if error happened print a message on the web page
  if not ret("mailed") then
     errmsg = "Error happened: <br>"
     errmsg = errmsg & "File: " & ret("source") & "<br>"
     errmsg = errmsg & "Line: " & ret("number") & "<br>"
     errmsg = errmsg & "Description: " & ret("description") & "<br>"
     result("txt") = errmsg
  end if
end if

 

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