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:

 

$emails = array();
 
while( $data = $button->getNextSelectedRecord() )
{
  if( $data["EmailField"] )
      $emails[] = $data["EmailField"];
}
 
// send the email
$email = implode(", ", $emails);
$subject = "Sample subject";
$body = "Your email text here";
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
 
$result["txt"] = "Emails were sent.";
 
// if error happened print a message on the web page
if( !$arr["mailed"] )
{
  $errmsg = "Error happened: <br>";
  $errmsg.= "File: " . $arr["errors"][0]["file"] . "<br>";
  $errmsg.= "Line: " . $arr["errors"][0]["line"] . "<br>";
  $errmsg.= "Description: " . $arr["errors"][0]["description"] . "<br>";
  $result["txt"] = $errmsg;
}

 

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( $params["emailBody"] || $params["emailSubject"] || $params["emailFrom"] )
{
  $emails = array();
  while ( $data = $button->getNextSelectedRecord() )
  {
    if ( $data["EmailField"] )
        $emails[] = $data["EmailField"];
  }
 
  // send the email
  $from = $params["emailFrom"];
  $email = implode(",", $emails);
  $body = $params["emailBody"];
  $subject = $params["emailSubject"];
  $arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body, 'from' => $from));
 
  $result["txt"] = "Emails were sent.";
  if (!$arr["mailed"])
  {
    $errmsg = "Error happened: <br>";
    $errmsg.= "File: " . $arr["errors"][0]["file"] . "<br>";
    $errmsg.= "Line: " . $arr["errors"][0]["line"] . "<br>";
    $errmsg.= "Description: " . $arr["errors"][0]["description"] . "<br>";
    $result["txt"] = $errmsg;
  }
}

 

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

 

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software