lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 29 July 2013 18:50, Chris Datfung <> wrote:
I want to write a script to log into my gmail account via imap, search for a specific message (based on the subject and date) and download and untar the attachment from that message (there will only be one for each day). Can this be easily done in lua? Can someone point me to an example?

Hi, it certainly can be done. There are some IMAP libraries for Lua, search for "Lua IMAP", for example pointing to here [1].

It is not very hard to write your own Lua IMAP parser, which is what I did for fun (and to parse fail2ban messages about "attacks" on my server to draw them on map). Take a look at my small IMAP module [2], which provides direct access to the IMAP protocol, saving the responses in a table for further processing. Keep an IMAP reference or examples [3] at hand to get an idea how to get to a certain message.

I suggest creating a GMail filter to collect message of your interest into a "folder" for easier searching.

Example of use:

    local imap = require "imap"
    local gmail = imap.connect("", 993, true)
    gmail:login("your_username", "your_password")
    gmail:select "CustomFilter" -- open a folder

    -- fetch all message headers in the folder into an array
    local all = gmail:fetch "1:* FULL"

    -- fetch the text of the message 593 into a table, each element being one line
    local message = gmail:fetch "593 BODY[TEXT]"

You have to handle the MIME parsing of the message to extract the attachment, or look for other libraries which can handle it.