Library: SendMail

Tested with opsi 4.0.2
required WInst Version: 4.11.3
By Frank Thommen, 2014/01/10 11:13

This script requires blat which can be downloaded from http://www.blat.net/.

Usage instructions see below in UseSendMail.sub

Tree:

%SCRIPTDRIVE%\lib\
  ├ UseSendMail.sub
  ├ SendMail_Variables.opsiinc
  ├ SendMail_Functions.opsiinc
  └ blat311\
     ├ docs\
     │  └ [...] 
     └ bin\
        ├ blatdll.h
        ├ blat.lib
        ├ blat.dll
        └ blat.exe

UseSendMail.sub

;
; -----------------------------------------------------------
;  UseSendMail.sub - send file, string or stringlist as mail
; -----------------------------------------------------------
;
; AUTHOR: Frank Thommen, EMBL Heidelberg
; SHORTNAME (for variable names): mail
;
; PREREQUISITE: blat from http://www.blat.net
;
;
; PURPOSE: Send a mail using "blat".  The content of the mail
;          can be taken from
;
;            * a stringlist (each element will become a line
;              of the mailbody
;            * a string (this will result in a one-line mail)
;            * a textfile (the content will become the mailbody)
;
;          The mail subject is user-defined, while sender and
;          destination address are harcoded in this version.
;          Therefore this version rather qualifies for sending
;          status mails to a fixed address rather than being a
;          freely configurable mailer.  However this could easily
;          be achieved by adding more user-definable variables.
;
;
; USAGE:
;          Sub "%SCRIPTDRIVE%\lib\UseSendMail.sub"
;          Set $_mail.subject = "Your Mail Subject"
;
;          Set $_mail.msgtype    = "stringlist"
;          Set $_mail.msgstrlist = <some stringlist cmd>
;             or
;          Set $_mail.msgtype    = "string"
;          Set $_mail.msgstring  = "Hello, how are you?"
;             or
;          Set $_mail.msgtype    = "file"
;          Set $_mail.msgfile    = "C:\some\file.txt"
;
;          Sub_SendMail
;
;
; HISTORY: 10-JAN-2014: Initial version
;
;
 
include_insert "%SCRIPTDRIVE%\lib\SendMail_Variables.opsiinc"
include_append "%SCRIPTDRIVE%\lib\SendMail_Functions.opsiinc"

SendMail_Variables.opsiinc

DefVar $_mail.msgtype
DefStringList $_mail.msgstrlist
DefVar $_mail.msgstring
DefVar $_mail.msgfile
DefVar $_mail.subject

SendMail_Functions.opsiinc

[sub_SendMail]
if ($_mail.msgtype = "file")
  WinBatch_Mail_SendMailFromFile
else
  if ($_mail.msgtype = "stringlist")
    ; avoid error message in case this function is used more then once
    ; within script
    ScriptErrorMessages = off
      DefVar $__mail.msgfile
    ScriptErrorMessages = on
 
    ; Create temporary filename and encode to get rid of
    ; special characters which might not be allowed in filenames
    Set    $__mail.msgfile = "C:\tmp\" + base64EncodeStr(RandomStr) + ".tmpmail"
 
    if ( FileExists($__mail.msgfile) )
      PAUSE "ERROR: File " + $__mail.msgfile + " already exists.  Please inform <YOUR CONTACT HERE> about this error"
    else
      for %s% in $_mail.msgstrlist do DosInAnIcon_Mail_DumpToFile
      if NOT( FileExists($__mail.msgfile) )
        PAUSE "ERROR: Mailfile " + $__mail.msgfile + " could not be created.  Please inform <YOUR CONTACT HERE> about this error"
      else
        WinBatch_Mail_SendMailFromLocalFile
        Files_Mail_DeleteMsgFile
      endif
    endif
  else
    if ($_mail.msgtype = "string")
      WinBatch_Mail_SendMailFromString
    else
      PAUSE "ERROR: Unknown Messagetype! Please inform <YOUR CONTACT HERE> about this error"
    endif
  endif
endif
 
;
; ----------------------------------------------------------
;
 
[DosInAnIcon_Mail_DumpToFile]
IF NOT "%s%"=="" (
  ECHO %s% >> "$__mail.msgfile"
) ELSE (
  ECHO.    >> "$__mail.msgfile"
)
 
 
[Files_Mail_DeleteMsgFile]
del -f "$__mail.msgfile"
 
 
[WinBatch_Mail_SendMailFromFile]
"%SCRIPTDRIVE%\lib\blat311\bin\blat.exe" "$_mail.msgfile" -serverSMTP smtp.your.domain -f "Name <email@ddress>" -to "Name <email@ddress>" -subject "$_mail.subject"
 
 
[WinBatch_Mail_SendMailFromString]
"%SCRIPTDRIVE%\lib\blat311\bin\blat.exe" - -serverSMTP smtp.your.domain -f "Name <email@ddress>" -to "Name <email@ddress>" -subject "$_mail.subject" -body "$_mail.msgstring"
 
 
[WinBatch_Mail_SendMailFromLocalFile]
"%SCRIPTDRIVE%\lib\blat311\bin\blat.exe" "$__mail.msgfile" -serverSMTP smtp.your.domain -f "Name <email@ddress>" -to "Name <email@ddress>" -subject "$_mail.subject"