# Library functions for the scripting library (scrib)
#
# File: scrib/lib/mail.tcl
#
# $Revision$ on $Date$
# 
# Copyright (c) 1993-2003 ICEM CFD Engineering
#
#---------------------------------------------------------------------

## Send a message to <em>recipients</em> with <em>subject</em> and <em>message</em>

proc scrib_lib_mail_send_message {recipients subject message} {
    
    # try to use some tempfile stuff from guibase
    set rpt [cmd_tempfile "bugrpt"]
    set fp [open $rpt a]
    puts $fp "$message\n"
    close $fp
    return [scrib_lib_mail_send_filebody $recipients $subject $rpt]
}

## Send a message to <em>recipients</em> with <em>subject</em> and 
## with the body being the contents of the given <em>bodyfile</em>

proc scrib_lib_mail_send_filebody {recipients subject bodyfile} {
    
    if ![file exists $bodyfile] {
	mess "Error: cannot find body file $bodyfile to send mail\n" red
	return 0
    }
    
    set valid_recipients ""
    foreach rcpt $recipients {
	if {0 > [lsearch $rcpt "@"]} {
	    lappend valid_recipients "${rcpt}@icemcfd.com"
	} else {
	    lappend valid_recipients $rcpt
	}
    }
    
    # fixme for windows; fix it so it's bg
    
    mess "Sending mail to \"$valid_recipients\" with subject \"$subject\"\n"
    set cmd "Mail -s {$subject} $valid_recipients < $bodyfile"
    set stat [eval exec $cmd &]
    
    return $stat
}
