[imapfilter-devel] Archive mail based on sender or sender domain

Lefteris Chatzibarbas lefcha at hellug.gr
Sun Jul 25 14:13:45 EEST 2004


On Fri, Jul 23, 2004 at 09:28:26AM -0500, Ezsra McDonald wrote:
> Here is my current block of code:
> 
> -- Get the year for the archive name
> Year = os.date("%Y")
> 
> -- Archive filtered mail
> results = match(myaccount, 'ztmp', myfilter)
> headers = fetchheaders(myaccount, 'INBOX', { 'from' }, results)
> 
> if (headers ~= nil) then
> for msgid, msghdrs in pairs(headers) do
>     -- check for "From: foo at bar"
>    _, _, user, domain = string.find(msghdrs, 'From:
> ([%w.-]+)@([%w.-]+)')
>    if (user ~= nil or domain ~= nil) then
>       -- Replace '.' with '_'
>       domain = string.gsub ( domain, "%p", "_" )
>       user = string.gsub ( user, "%p", "_" )
>       dest = Year .. "/" .. domain .. "/" .. user
>       -- Copy message to archive
>       copy(myaccount, 'INBOX', myaccount, dest, { msgid })
>       -- Move message to mail box
>       move(myaccount, 'INBOX', myaccount, 'mail', { msgid })
>       print(msgid, user, domain, dest)
>    end
>    -- check for "From: Foo Bar <foo at bar>"
>    _, _, user, domain = string.find(msghdrs, 'From:
> .+<([%w.-]+)@([%w.-]+)>')
>    if (user ~= nil and domain ~= nil) then
>       -- Replace '.' with '_'
>       domain = string.gsub ( domain, "%p", "_" )
>       user = string.gsub ( user, "%p", "_" )
>       dest = Year .. "/" .. domain .. "/" .. user
>       -- Copy message to archive
>       copy(myaccount, 'INBOX', myaccount, dest, { msgid })
>       -- Move message to mail box
>       move(myaccount, 'INBOX', myaccount, 'mail', { msgid })
>       print(msgid, user, domain, dest)
>    end
> end
> end
> 
> 
> The problem is that not all the mail is being filed as it should. It
> appears to only file the first match in the archive and mail box. Any
> other messages that match the filter are not filed. I have also found
> some messages misfiled in the wrong folders. Any idea why?

I believe that the "first match" problem can be solved either by
enabling UID (options.uid = true), or by doing two copy actions (instead
of a copy and then a move) and in the end of the for loop delete the
messages ("results" variable) from the INBOX.

It is difficult to say what goes wrong with the matching problems from
here; your best bet is putting some "print" commands in places that will
help you track down the problem...




More information about the Imapfilter-devel mailing list