I am writing a script which can send E-Mails with LuaSocket SMTP. Now I would like to make my script also be able to read my E-Mails.
I have an E-Mail account at a service which supports IMAP.
IMAP host and port: example.com:143
My E-Mail: doesnotexist@example.com
The password: notsaying
How can I make my script fetch an E-Mail from that service so I can display its contents?
Edit:
I have tried imap4 from https://github.com/vrld/imap4.lua but does not seem to be finished.
I have tried this example:
require 'luarocks.require'
local imap4 = require 'imap4'
local username = "doesnotexist@example.com"
local password = "notsaying"
local connection = imap4('example.com', 143)
print(table.concat(connection:capability(), ', '))
assert(connection:isCapable('IMAP4rev1'))
connection:login(username, password)
for mb, info in pairs(connection:lsub()) do
local stat = connection:status(mb, {'MESSAGES', 'RECENT', 'UNSEEN'})
print(mb, stat.MESSAGES, stat.RECENT, stat.UNSEEN)
end
local info = connection:examine('INBOX')
print(info.exist, info.recent)
for _,v in pairs(connection:fetch('UID BODY.PEEK[HEADER.FIELDS (From Date Subject)]', (info.exist-3)..':*')) do
print(v.id, v.UID, v.BODY.value)
end
connection:logout()
I only added username and password and changed the server and port from the original example and changed the most recent to fetch from 4 to 3. I have also removed the comments, as they are many. Other than those I did not modify anything else, it is basically the same example.
It seems to log in correctly using my credentials and the correct server and port, but it has some kind of problem which I am unable to debug.
The above example outputs:
IMAP4rev1, CHILDREN, NAMESPACE
INBOX.Sent 3 0 0
INBOX 4 0 0
4 0
lua: example.lua:24: attempt to index field 'BODY' (a nil value)
stack traceback:
example.lua:24: in main chunk
[C]: ?
It shows that I have 4 E-Mails in my inbox and 3 in my sent, which is correct.