--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
-- ftp Transfer Test
title = "SMTP Test"
-- load the smtp support and its friends
local smtp = require("socket.smtp")
local mime = require("mime")
local ltn12 = require("ltn12")

function filter(s)
    if s then io.write(s) end
    return s
end

-- main program
function setup() 
print("SMTP test");
--print(arg[0])
print(debug.getinfo(1).source)

source = smtp.message {
    headers = {
	-- Remember that headers are *ignored* by smtp.send. 
     from = "Mark DiVecchio <RTC@silogic.com>",
     to = "No One <RTC@silogic.com>",
     subject = "Hello",
	['content-type'] = 'multipart/alternative' },
    body = {
        [1] = {
            headers = { ['Content-type'] = 'text/html' },
            body = "<html> <body> Hi, <b>there</b>...</body> </html>"
        },
        [2] = {
            headers = { ['content-type'] = 'text/plain' },
            body = "Hi, there..."
        }
    }
}

r, e = smtp.send{
    rcpt = {"<RTC@silogic.com>",
            "<RTC@silogic.com>" },
    from = "<RTC@silogic.com>",
    source = ltn12.source.chain(source, filter),
	user = "RTC@silogic.com",
	password = "mM888888gr42b",
    server = "mail.silogic.com",
	port = 2525
}

print(r, e)

-- creates a source to send a message with two parts. The first part is 
-- plain text, the second part is a PNG image, encoded as base64.
source = smtp.message{
  headers = {
     -- Remember that headers are *ignored* by smtp.send. 
     from = "Marco Old <markd@silogic.com>",
     to = "No One <noone@silogic.com>",
     subject = "Here is a message with attachments"
  },
  body = {
    preamble = "If your client doesn't understand attachments, \r\n" ..
               "it will still display the preamble and the epilogue.\r\n" ..
               "Preamble might show up even in a MIME enabled client.",
    -- first part: No headers means plain text, us-ascii.
    -- The mime.eol low-level filter normalizes end-of-line markers.
    [1] = { 
      body = mime.eol(0, [[
        Lines in a message body should always end with CRLF. 
        The smtp module will *NOT* perform translation. It will
        perform necessary stuffing, though.
      ]])
    },
    -- second part: Headers describe content the to be an image, 
    -- sent under the base64 transfer content encoding.
    -- Notice that nothing happens until the message is sent. Small 
    -- chunks are loaded into memory and translation happens on the fly.
    [2] = { 
      headers = {
        ["ConTenT-tYpE"] = 'image/png; name="defines.out"',
        ["content-disposition"] = 'attachment; filename="defines.out"',
        ["content-description"] = 'Binary File',
        ["content-transfer-encoding"] = "BASE64"
      },
      body = ltn12.source.chain(
		-- file must be in the same folder as this script
        ltn12.source.file(io.open("defines.out", "rb")),
        ltn12.filter.chain(
          mime.encode("base64"),
          mime.wrap()
        )
      )
    },
    epilogue = "This might also show up, but after the attachments"
  }
}


r, e = smtp.send{
    rcpt = {"<markd@silogic.com>",
            "<noone@silogic.com>" },
    from = "<markd@silogic.com>",
    source = ltn12.source.chain(source, filter),
	user = "markd@silogic.com",
	password = "mM888gr42b",
    server = "mail.silogic.com",
    port = 2525
}

print(r, e)
print("Done");

return false;
end

