-- make mailbody -> file mailbody
-- 
-- use "makemail.ini" , "B64.E"
-- 
-- mail with no attachment's
-- i = sendmail(mails,subject,line)
-- mails is a seuquence of eMail {em1,em2, .... }
-- subject is a text line
-- line is the letter in one sequence . 
--                       
-- mail with one or attachment's
-- i = sendfile(mails,subject,line,files)
-- mails is a seuquence of eMail {em1,em2, .... }
-- subject is a text line
-- line is the letter in one sequence .
-- files is sequence of filename to be send . 
-- (should be in the same dir as the program)
--  
-- encode64(sequence in)
include B64.E
sequence afz,mailer  
integer   mailbody
       
function mailhead()
  return mailer&"\nhelo ike\nmail from:<"&afz&">\n"
end function

function is_error(sequence filename)
integer error=0,file=open(filename,"r")
sequence text
if file>2
then  text=gets(file)
      while sequence(text) do 
        error=match("Error ",text)
        if error then exit end if 
        text=gets(file)
      end while
else  error = -1
end if       
return error
end function

global function Logon()
return 0
end function

global function Logoff()
return 0
end function
global function sendmail(sequence email_to;subject;line)
integer error=0,mailbody = open("mailbody","w")
if not mailbody
then error = -1
else
     puts(mailbody,mailhead())
     --**if only 1 address brace it so we don't set any pointers to an atom. 
     if atom(email_to[1]) then email_to = {email_to}  end if 
     for i = 1 to length(email_to) do 
         puts(mailbody,"rcpt to:<"&email_to[i]&">\n")
     end for
     -- data section : becarefull only last line should have a '.\n'
     puts(mailbody,"data\nfrom:"&afz&"\nto:geadresseerde@alhier\n")
     puts(mailbody,"Subject: "&subject&"\n")
     puts(mailbody,line)
     puts(mailbody,"\n.\n")
     close(mailbody) 
     -- verstuur nu het bestand naar de mailserver      
--     system("peuw mbodyf>mbody.txt",0)
--     system("mbodyf>mbody.txt",0)
     -- was there a 'on error' in mbody.txt
     error = is_error("mbody.txt")
end if
return error     
end function 

global function sendfile(sequence email_to;subject;line;file_path)
constant my_boundery="simple_boundary"
integer filebody,error=0,mailbody = open("mailbody","w")
sequence text
if not mailbody
then error = -1
else
     puts(mailbody,mailhead())
     --if only 1 address brace it so we don't set any pointers to an atom. 
     if atom(email_to[1]) then email_to = {email_to}  end if 
     for i = 1 to length(email_to) do 
         puts(mailbody,"rcpt to:<"&email_to[i]&">\n")
     end for
     -- data section : becarefull only last line should have a '.\n'
     puts(mailbody,"data\nfrom:"&afz&"\nto:geadresseerde@alhier\n")
     puts(mailbody,"Subject: "&subject&"\n")
     -- here Mime declaration
     puts(mailbody,"MIME-Version: 1.0\n")
     puts(mailbody,"Content-Type: multipart/mixed; boundary=\""&my_boundery&"\"\n\n")
     -- not readeble part
     puts(mailbody,"--"&my_boundery&"\n") 
     puts(mailbody,"Content-Type: text/plain\n\n")
     -- here come the letter
     puts(mailbody,line)
     puts(mailbody,"--"&my_boundery&"\n") 
     -- here come's the files
     if length(file_path)      
     --if only 1 address brace it so we don't set any pointers to an atom. 
     then if atom(file_path[1]) then file_path = {file_path}  end if
          for i= 1 to length(file_path) do  
              if i!=1 then puts(mailbody,"\n") end if
              filebody = open(file_path[i],"rb")
              if filebody>2
              then
                puts(mailbody,"Content-Type: application/octet-stream\n")
                puts(mailbody,"Content-Transfer-Encoding: base64\n")
                puts(mailbody,"Content-Disposition: attachment;filename=\""&file_path[i]&"\"\n\n")
                text = get_bytes(filebody,57)
                while sequence(text) and length(text)do
                      puts(mailbody,encode64(text)&'\n')
                      text=get_bytes(filebody,57)
                end while
                close(filebody)
                -- next one
                puts(mailbody,"--"&my_boundery) 
              end if     
          end for 
     -- last file 
     end if   
     puts(mailbody,"--\n") 
     -- end of email
     puts(mailbody,".\n")
     close(mailbody)
     -- verstuur nu het bestand naar de mailserver      
--     system("peuw mbodyf>mbody.txt",0)
--     system("mbodyf>mbody.txt",0)
     -- was there a 'on error' in mbody.txt
     error = is_error("mbody.txt")
end if     
return error
end function

mailbody = open("makemail.ini","r") 
if mailbody>2
then afz=gets(mailbody)
     mailer=gets(mailbody)
     while sequence(mailer) do 
         if match("--",mailer)
         then mailer=gets(mailbody)
         else exit
         end if
     end while
     close(mailbody)
     -- strip \n
     afz = afz[1..-2]
     mailer=mailer[1..-2]
else puts(1,"Error cann't open ini file\n")
     abort(1)
end if