SMTP send email with attachment, using LuaSocket

Can someone tell me why this code works fine with a non Corona version of Lua eg. Lua for Windows, but fails when I try it with Corona ?

It gives an error "cannot find file or directory smtp-test.lua , which is the file I’m attaching, which is located in the same folder as main.lua ?

[code]
smtp = require(“socket.smtp”)
mime = require(“mime”)
ltn12 = require(“ltn12”)

mailserv = “XXX”
mailport = “25”

from = ""
rcpt = { “”, “” }

mesgt = smtp.message{
headers = {
to = “XXX”,
subject = “Test Lua Email”
},
body = {
preable = “This email contains attachments.”,
[1] = {
body = mime.eol(0, [[
“With any luck, we are able to see a Lua-sent
email here.”
]])
},
[2] = {
headers = {
[“content-type”] = ‘text/plain; name=“smtp-test.lua”’,
[“content-disposition”] = ‘attachment; filename=“smtp-test.lua”’,
[“content-description”] = ‘the send script’s source’,
[“content-transfer-encoding”] = “BASE64”
},
body = ltn12.source.chain(
ltn12.source.file(io.open(“smtp-test.lua”, “r”)),
ltn12.filter.chain(
mime.encode(“base64”),
mime.wrap()
)
)
},
epilogue = “A few words at the end…”
}
}

r, e = smtp.send {
from = from,
rcpt = rcpt,
source = mesgt,
server = mailserv,
port = mailport
}

if (e) then
io.stderr:write("Could not send email: ", e, “\n”)
end
[/code] [import]uid: 97524 topic_id: 19703 reply_id: 319703[/import]

I’ve got it - it’s simply a path issue. Corona can’t find the file as we haven’t setup the correct path to it.

Here’s the new code which works and successfully sends an email with attachment (note: encrypted smtp not supported)

[code]
smtp = require(“socket.smtp”)
mime = require(“mime”)
ltn12 = require(“ltn12”)

local path = system.pathForFile( “smtp-test.lua”, system.ResourceDirectory )
mailserv = “XXX”
mailport = “25”

from = ""
rcpt = { “”, “” }

mesgt = smtp.message{
headers = {
to = “XXX”,
subject = “Test Lua Email”
},
body = {
preable = “This email contains attachments.”,
[1] = {
body = mime.eol(0, [[
“With any luck, we are able to see a Lua-sent
email here.”
]])
},
[2] = {
headers = {
[“content-type”] = ‘text/plain; name=“smtp-test.lua”’,
[“content-disposition”] = ‘attachment; filename=“smtp-test.lua”’,
[“content-description”] = ‘the send script’s source’,
[“content-transfer-encoding”] = “BASE64”
},
body = ltn12.source.chain(
ltn12.source.file(io.open(path, “r”)),
ltn12.filter.chain(
mime.encode(“base64”),
mime.wrap()
)
)
},
epilogue = “A few words at the end…”
}
}

r, e = smtp.send {
from = from,
rcpt = rcpt,
source = mesgt,
server = mailserv,
port = mailport
}

if (e) then
io.stderr:write("Could not send email: ", e, “\n”)
end
[/code] [import]uid: 97524 topic_id: 19703 reply_id: 76216[/import]

Was just about to post to say about setting the path!

Anyway, glad you solved it and thanks for sharing the code :slight_smile: [import]uid: 84637 topic_id: 19703 reply_id: 80084[/import]