//////////////////////////////////////////////////////////////////
FUNCTION f_MailStart
//////////////////////////////////////////////////////////////////
// This function will initiate a CC:Mail session.
//
// Arguments:  a_userid
// Returns:  Boolean
//
//    TRUE  = Successful 
//    FALSE = Unable to initiate session
//////////////////////////////////////////////////////////////////
long    handle
integer return_code
string  s_email_path, s_email_node

handle = OpenChannel( g_str_user_info.s_email_handle, "SendMail" )

IF handle > 0 THEN
   CloseChannel(handle)
   RETURN TRUE   //  EMail already started
ELSE
   //
   IF g_str_user_info.s_email_handle = "NOTES" THEN
      return_code = Run( g_str_user_info.s_email_path, Minimized!)
   ELSE
      return_code = Run( g_str_user_info.s_email_path + ' ' + a_userid + '" PASSWORD', Minimized!)
   END IF

   IF return_code < 0 THEN
      RETURN FALSE
   ELSE
      RETURN TRUE
   END IF

END IF




//////////////////////////////////////////////////////////////////
FUNCTION f_MailSendMessage
//////////////////////////////////////////////////////////////////
// This function will create and send a CC:Mail message.
//
//	Arguments:  a_str_mail = s_subject, s_to[], s_cc[], s_bcc[], s_attachfile
//	Returns:  Integer
//
//		-1 = Couldn't start CC:Mail at all
//		-2 = CC:Mail Busy
//		 0 = Successful
//////////////////////////////////////////////////////////////////
boolean b_retry = TRUE
long    handle, num_recipients, idx
integer return_code, num_tries = 0
string  s_sendto

/* Get handle for CC:Mail */

DO WHILE b_retry

   num_tries ++
   /* Need to give user enough time to type in password */
   IF num_tries > 300 THEN   
      RETURN -1
   END IF

   handle = OpenChannel( "wMail", "SendMail")
   IF handle < 0 THEN
      IF NOT f_mail_start( g_str_user_info.s_userid) THEN
         RETURN -1
      END IF
   ELSE
      b_retry = FALSE
   END IF	

LOOP

// Initiate New Message
IF handle < 0 THEN
   RETURN -1
END IF

return_code = ExecRemote("NewMessage", handle)

IF return_code < 0 THEN
   // CC:Mail is busy
   CloseChannel( handle)
   RETURN -2
END IF

// Address the Message
num_recipients = UpperBound( a_str_mail.s_to )
FOR idx = 1 to num_recipients

// Convert network ID to e-mail equivalent.
   s_sendto = f_get_email( a_str_mail.s_to[idx], sqlca)	
   return_code = ExecRemote( "To " + s_sendto, handle)

   IF return_code < 0 THEN
      // User not found -- continue
   END IF

NEXT

num_recipients = UpperBound( a_str_mail.s_cc )

FOR idx = 1 to num_recipients

// Convert network ID to e-mail equivalent
   s_sendto = f_get_email( a_str_mail.s_cc[idx], sqlca)	
   return_code = ExecRemote( "CC " + s_sendto, handle)

   IF return_code < 0 THEN
      // User not found -- continue
   END IF

NEXT

num_recipients = UpperBound( a_str_mail.s_bcc )

FOR idx = 1 to num_recipients

   // Convert network ID to e-mail equivalent
   s_sendto = f_get_email( a_str_mail.s_bcc[idx], sqlca)	
   return_code = ExecRemote( "BCC " + s_sendto, handle)

   IF return_code < 0 THEN
      // User not found -- continue
   END IF

NEXT

// Subject
return_code = ExecRemote( "Subject " + a_str_mail.s_subject, handle)

IF return_code < 0 THEN
   // CC:Mail is busy
   CloseChannel( handle)
   RETURN -2
END IF

return_code = ExecRemote( "AttachFile " + a_str_mail.s_attachfile, handle)

IF return_code < 0 THEN
   // CC:Mail is busy
   CloseChannel( handle)
   RETURN -2
END IF

return_code = ExecRemote( "Send", handle)

IF return_code < 0 THEN
   // CC:Mail is busy
   CloseChannel( handle)
   RETURN -2
END IF

RETURN 1
