Please enable JavaScript to view this site.

Navigation: Advanced topics > Events > Sample events > Email

Email selected records

Scroll Prev Next More

How to send an email to a predefined address

To send an email with several selected records on the List page, you need to create a custom button.

 

1. Proceed to the Page Designer screen.

 

2. Create an Update selected custom button and add the following code snippets into it:

Server tab:

 

 

dim body, data, rmail, ret
set rmail = CreateDictionary()
body=""
do while(bValue(doAssignmentByRef(data, button.getNextSelectedRecord())))
  body=body & "OrderID: " & data("OrderID") & vbcrlf
  body=body & "Customer: " & data("CustomerID") & vbcrlf
  body=body & "Employee: " & data("EmployeeID") & vbcrlf & _
        "-------------------" & vbcrlf & vbcrlf
loop
 
' send the email
rmail("to")="test@test.com"
rmail("subject")="Sample subject"
rmail("body")=body
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

Client After tab:

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

 

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

 

Sample email message:

 

OrderID: 10249

Customer: TRADH

Employee: 6

-------------------

OrderID: 10251

Customer: VICTE

Employee: 3

-------------------

OrderID: 10253

Customer: HANAR

Employee: 3

-------------------

How to send an email to the currently authorized user

Instead of a hardcoded email address, you can send email to the current user.

1. The email address is used as the username

email=Session("UserID")

2. The email address is stored in an individual field

In this case, you need to save the email address to the session variable in the AfterSuccessfulLogin event:

 

Session("email")=data("email")

 

BeforeProcess event code:

 

email=Session("email")

See also:

Inserting custom button

Update selected

Tri-part events

Send an email to all users

Send an email to selected users

Send simple email

How to email selected records as separate PDF files

Grid Row Javascript API: row.getKeys()

JavaScript API:getSelectedRecordKeys()

Button object: getNextSelectedRecord()

runner_mail function

AJAX helper object: setMessage()