sendmail.com
Jerry Alan Braga, Friday December 11 2009 @ 05:32PM EST
$ sendmail_save_verify := 'f$verify(0)'
$ if ("''sendmail_verify'" .nes. "") then set verify
$!
$! Send Mail with Mime
$!
$! Requirements:
$!	MC MIME utility for attachment rendering
$!	TCPIP send from File utility if using SMTP
$!
$! Logicals if Defined
$!	define/system sendmail_domain	! Overrides the default domain
$!	define/system sendmail_sff	! Send from file image to use for smtp
$!
$! Usage:
$!	@sendmail "to_address,..." "Subject" "Body" "Attachment,..." "delete" "cnvformat" "from_address"
$!
$ Main:
$!-----
$ Gosub Initialize
$ Gosub Header
$ Gosub Body
$ Gosub Attachments
$ Gosub Send
$ Gosub Finish
$!
$ sendmail_save_verify := 'f$verify(sendmail_save_verify)'
$ Exit
$!
$ Initialize:
$!-----------
$!
$ proc     = f$edit(f$parse(f$enviornment("PROCEDURE"),,,"NAME"),"LOWERCASE,COLLAPSE")
$ username = f$edit(f$getjpi("","USERNAME"),"LOWERCASE,COLLAPSE")
$ pid      = f$getjpi("","PID")
$ tmp_inp  = "sys$scratch:''proc'''pid'.tmp"
$ tmp_eml  = "sys$scratch:''proc'''pid'.eml"
$ tmp_tmp  = "sys$scratch:''proc'''pid'.tmp"
$ dq       = """
$ disdir   = "mail_list:"
$ pdfcnv   = "cool:txt2pdf.com"
$!
$! -- Get the Send From File Image --
$!
$ sff = f$trnlnm("''proc'_sff")
$ if (sff .eqs. "")
$ then 	sff = "$sys$system:tcpip$smtp_sff.exe"
$ else	sff = "$''sff'"
$ endif
$!
$! -- Get Email Domain --
$!
$ email_domain = f$edit(f$trnlnm("''proc'_domain"),"LOWERCASE,COLLAPSE")
$ if (email_domain .eqs. "") then email_domain = f$edit(f$trnlnm("TCPIP$SMTP_DOMAIN"),"LOWERCASE,COLLAPSE")
$ if (email_domain .eqs. "") then email_domain = f$edit(f$trnlnm("TCPIP$INET_DOMAIN"),"LOWERCASE,COLLAPSE")
$!
$! -- RFC Message ID --
$!
$ message_id = f$string(f$cvtime(,,"HUNDREDTH")) + f$string(f$cvtime(,,"SECONDOFYEAR")) + -
f$string(f$cvtime(,,"MINUTEOFYEAR")) + f$string(f$cvtime(,,"HOUROFYEAR")) + -
f$string(f$cvtime(,,"DAYOFYEAR")) + f$string(f$cvtime(,,"YEAR")) + -
f$string(f$cvtime(,,"HUNDREDTH"))
$!
$! -- RFC Date time Day, DD Mon YYYY HH:MM:SS offset --
$!
$ timezone_sign = ""
$ timezone_hh = 0
$ timezone_mm = 0
$ timezone_differential = f$trnlnm("SYS$TIMEZONE_DIFFERENTIAL")
$ if (timezone_differential .lt. 0)
$ then
$	timezone_sign = "-"
$	timezone_differential = timezone_differential * -1
$ endif
$ timezone_hh:
$ 	if (timezone_differential .lt. 3600) then goto timezone_done
$ 	timezone_differential = timezone_differential - 3600
$ 	timezone_hh = timezone_hh + 1
$ goto timezone_hh
$ timezone_done:
$ timezone_mm = timezone_differential / 60
$ timezone_rfc =  "''timezone_sign'''f$extract(1,2,f$string(100 + timezone_hh))'''f$extract(1,2,f$string(100 + timezone_mm))'"
$!
$ datetime    = -
f$edit(f$extract(0,1,f$cvtime(,"ABSOLUTE","WEEKDAY")),"UPCASE") + -
f$edit(f$extract(1,2,f$cvtime(,"ABSOLUTE","WEEKDAY")),"LOWERCASE") + ", " + -
f$cvtime(,"ABSOLUTE","DAY") + " " + -
f$edit(f$extract(0,1,f$cvtime(,"ABSOLUTE","MONTH")),"UPCASE") + -
f$edit(f$extract(1,2,f$cvtime(,"ABSOLUTE","MONTH")),"LOWERCASE") + " " + -
f$cvtime(,"ABSOLUTE","YEAR") + " " + -
f$cvtime(,"ABSOLUTE","HOUR") + ":" + -
f$cvtime(,"ABSOLUTE","MINUTE") + ":" + -
f$cvtime(,"ABSOLUTE","SECOND") + " " + -
timezone_rfc
$!
$! *** Start Parameters ***
$!
$ p_address 	= f$edit("''p1'","COLLAPSE")		! Email address(s)
$ p_subject 	= f$edit("''p2'","TRIM")		! Subject
$ p_body 	= f$edit("''p3'","TRIM")		! Can be A Filename or message
$ p_attach	= f$edit("''p4'","COLLAPSE")		! File(s)
$ p_delete	= f$edit("''p5'","UPCASE,COLLAPSE")	! DELETE Body/Attach File after
$ p_cnvfmt	= f$edit("''p6'","UPCASE,COLLAPSE")	! Convert to Format ("PDF","HTML","PRE")
$ p_from 	= f$edit("''p7'","COLLAPSE")		! Override From address
$!
$! *** End Parameters ***
$!
$! -- Check Log Logical --
$!
$ sff_log_level = 0
$ sff_log_level = f$edit(f$trnlnm("''proc'_log_level"),"UPCASE,COLLAPSE")
$ if (sff_log_level .eqs. "") then sff_log_level = 0
$!
$! -- Address Must be passed ---
$!
$ if (p_address .eqs. "?")
$ then
$	p_address = f$edit(f$getjpi("","USERNAME"),"LOWERCASE,COLLAPSE")
$ endif
$ if (p_address .eqs. "")
$ then
$	write sys$output ""
$	write sys$output "Email Address Required"
$	exit
$ endif
$!
$! Check for the Image Required
$!
$ if (f$search(f$extract(1,f$length(sff),sff)) .eqs. "")
$ then
$	write sys$output ""
$	write sys$output "Cannot Find ''sff'"
$	exit
$ endif
$!
$! Check to See if From Address has been overriden
$! If this is true assume the same domain as sender
$! This emulates VMS mail based
$! This is required as the mail/for will not work without @domain.com
$!
$ from_address = username
$ if (p_from .nes. "")
$ then
$ 	from_address = p_from
$ endif
$!
$ if (f$locate("@",from_address) .eq. f$length(from_address))
$ then
$	frdis = 0
$	open/error=nofrdis frid 'disdir''from_address'.dis
$	read/end_of_file=froef frid from_address
$	frdis = 1
$	freof:close frid
$	nofrdis: if (.not. frdis) then from_address = "''from_address'@''email_domain'"
$ endif
$ from_domain = f$extract(f$locate("@",from_address),f$length(from_address),from_address)
$ from_name = from_address
$ reply_address = from_address
$!
$ Header:
$!-------
$!
$ open/write f 'tmp_eml'
$!
$! If No Attachments are to be Used then Must Write Text Header
$! As MC MIME will not be used to Create it
$!
$ if (p_attach .eqs. "")
$ then
$	boundary = "------=_OpenVMS/MIME.''message_id'"
$ 	write f "Message-ID: <''message_id'@OpenVMS>"
$ 	write f "Mime-version: 1.0"
$ endif
$!
$! Need to determine if body has HTML based tags to replace content type
$!
$ eml_body_stxt = ""
$ eml_body_etxt = ""
$ eml_body_type = "text/plain"
$ eml_body_file = f$search("''p_body'")
$ eml_body_msg = ""
$ if (eml_body_file .nes. "")
$ then
$	open/read/error=b_eof b_id 'eml_body_file'
$	read/end_of_file=b_eof b_id eml_body_msg
$	close b_id
$	b_eof:
$ else
$ 	eml_body_msg = "''p_body'"
$ endif
$ if (f$locate("", f$edit(eml_body_msg,"UPCASE")) .ne. f$length(eml_body_msg))
$ then
$	eml_body_type = "text/html"
$ else
$	if (p_cnvfmt .eqs. "PRE")
$	then
$		eml_body_type = "text/html"
$		eml_body_stxt = "
"
$		eml_body_etxt = "
" $ endif $ endif $! $ write f "Content-Type: ''eml_body_type'; charset=ISO-8859-1" $ write f "Content-Transfer-Encoding: 7bit" $ write f "Content-Disposition: inline" $ write f "" $! $ close f $! $ Return $! $ Body: $!----- $! $! Body Of Message Can be an input file or a Text String $! $ if (eml_body_file .nes. "") $ then $ if ("''eml_body_stxt'" .nes. "") $ then $ open/append f 'tmp_eml' $ write f "''eml_body_stxt'" $ close f $ endif $! $ convert/append 'eml_body_file' 'tmp_eml' $! $ if ("''eml_body_etxt'" .nes. "") $ then $ open/append f 'tmp_eml' $ write f "''eml_body_etxt'" $ close f $ endif $ else $ open/append f 'tmp_eml' $ write f "''eml_body_stxt'''eml_body_msg'''eml_body_etxt'" $ close f $ endif $! $ Return $! $ Attachments: $!------------ $! $! Parse Multiple attachments and add according to file format $! using the MIME utility $! $ files = p_attach $ del_attach = "" $ afnd = 0 $ aloop: $ if (files .eqs. "") then goto aeof $ comma = f$locate(",",files) $ if (comma .ne. f$length(files)) $ then file = f$extract(0,comma,files) $ else file = files $ endif $! $ fspec = f$search(file) $ if (fspec .eqs. "") then goto adone $ del_file = fspec $ $ if (.not. afnd) $ then $ open/write f 'tmp_inp' $ write f "open/draft ''tmp_eml'" $ close f $ afnd = 1 $ endif $! $ if (p_cnvfmt .eqs. "PDF" .and. f$search(pdfcnv) .nes. "") $ then $ pdfspec = "sys$scratch:" + f$parse(fspec,,,"NAME") + ".pdf" $ @'pdfcnv' "''fspec'" "''pdfspec'" $ fspec = f$search(pdfspec) $ endif $! $ open/append f 'tmp_inp' $ write f "add ''fspec'" $ close f $! $ if (p_cnvfmt .eqs. "PDF") $ then $ if (del_attach .nes. "") then del_attach = del_attach + "," $ del_attach = del_attach + fspec $ endif $ if (p_delete .nes. "") $ then $ if (del_attach .nes. "") then del_attach = del_attach + "," $ del_attach = del_attach + del_file $ endif $! $ adone: $ if (comma .eq. f$length(files)) $ then files = "" $ else files = f$extract(comma+1,f$length(files),files) $ endif $ goto aloop $ aeof: if (.not. afnd) then return $ open/append f 'tmp_inp' $ write f "save" $ write f "exit" $ close f $! $! Run MIME utility with the imput file above $! $ define/user sys$input 'tmp_inp' $ define/user sys$output nl: $! $ mc mime $! $ delete/nolog/noconfirm 'tmp_inp';* $ if (del_attach .nes. "") then delete/nolog/noconfirm 'del_attach' $! $ Return $! $ Send: $!----- $! $! Parse thru all addresses and send out a email for each $! $ address = p_address - "<" - ">" $ fspec = f$search(tmp_eml) $ eloop: $ if (address .eqs. "") then goto eeof $ comma = f$locate(",",address) $ if (comma .ne. f$length(address)) $ then taddress = f$extract(0,comma,address) $ else taddress = address $ endif $! $! Look for a SMTP extended formated message Name Address $! and Rewrite Address as "Name"
$! $ to_address = taddress $ if (f$locate("@",taddress) .eq. f$length(taddress)) $ then $ todis = 0 $ open/error=notodis toid 'disdir''to_address'.dis $ read/end_of_file=toeof toid to_address $ todis = 1 $ toeof: close toid $ notodis: if (.not. todis) then to_address = "''taddress'@''email_domain'" $ endif $ to_domain = f$extract(f$locate("@",to_address),f$length(to_address),to_address) $ to_address = "<" + f$edit(to_address,"LOWERCASE") + ">" $ to_name = to_address $! $! If Email is Based on Using SMTP must write full Envelope Header $! Have to Write the Envelope Header for Each Destination $! $ open/write f 'tmp_eml' $ write f "MAIL FROM:<''from_address'>" $ write f "RCPT TO:",to_address $ write f "DATA" $ write f "Reply-To: <''reply_address'>" $ write f "From: ",dq,from_name,dq," <''from_address'>" $ write f "To: ",to_name $ write f "Subject: ",p_subject $ write f "Date: ",datetime $ close f $! $! Append the MIME rendered Version to This $! $ convert/append 'fspec' 'tmp_eml' $! $! Use the TCPIP SMTP Send From File Utility $! $ sff 'tmp_eml' -loglevel 'sff_log_level' $! $ if (comma .eq. f$length(address)) $ then address = "" $ else address = f$extract(comma+1,f$length(address),address) $ endif $ goto eloop $ eeof: $! $ delete/nolog/noconfirm 'tmp_eml';* $! $ return $! $ Finish: $!------- $! $ if (eml_body_file .nes. "" .and. p_delete .nes. "") $ then $ delete/nolog 'eml_body_file' $ endif $! $ exit

< GETLIB.COM | txt2pdf.com >