iTP Active Transaction Pages (iTP ATP) Programmer's Guide
ATP Web Page and Script Structure
iTP Active Transaction Pages (iTP ATP) Programmer’s Guide—522292-002
3-17
Using TCP/IP Sockets from JavaScript
The script in Example 3-10 consists of a call to a function called sendMail, which in 
turn calls other functions defined earlier in the script. The order of operations is typical 
for a sockets application:
Example 3-10. Code for TCP/IP Sockets Script - Function Call
// Send a mail message ...
function sendmail (smtphost, from, fromHost, to, toHost, subject, 
message) {
 var rc;
 // Create a socket connection .....
 var s=new atp.socket (smtphost, 25);
  handleErrors (s, 'constructor');
 // Get started ....
 s.write ("HELLO " + fromHost + "\n");
  handleErrors (s, 'HELLO write');
  rc = handleReply (s, 'HELLO reply'); 
 // Write the sender ....
 s.write ("MAIL FROM: " + from + '@' + fromHost+ "\n");
  handleErrors (s, 'MAIL FROM write');
 rc = handleReply (s, 'MAIL FROM reply'); 
 // Write the recipient ....
 s.write ("RCPT TO: " + to + '@' + toHost+ "\n");
  handleErrors (s, 'RCPT TO write');
 rc = handleReply (s, 'RCPT TO reply'); 
 // Write the data header ...
 s.write ("DATA\n");
  handleErrors (s, 'DATA write');
 rc = handleReply (s, 'DATA reply', 354); 
  //  Write the subject ....
  s.write ("SUBJECT: " + subject + "\n");
  handleErrors (s, 'SUBJECT write');
 // Write the message body ...
 if (message.indexOf('\n',message.length-1) == message.length-1)
  message += '.\n';
 else
  message += '\n.\n';
 s.write (message);
  handleErrors (s, 'message body write');
  rc = handleReply (s, 'message body reply'); 
 // Finish off  ...
 s.write ("QUIT\n");
  handleErrors (s, 'QUIT write'); 
 rc = handleReply (s, 'QUIT reply'); 
 // Close the TCP/IP socket
 s.close ();
 atp.print ('<br>Message sent.<br>\n');
 return;
 }










