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

Ezsra McDonald ezsra_mcdonald at yahoo.com
Fri Jul 23 17:28:26 EEST 2004


Thanks for the example code. I took a stab at modifying it but I am
having some problems.

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?

Thanks again for your help.

--Ezsra


On Sun, 2004-06-13 at 03:38, Lefteris Chatzibarbas wrote:
> On Thu, Jun 10, 2004 at 07:37:09PM -0700, Ezsra McDonald wrote:
> > I have been looking over the new IMAPfilter. Its very
> > nice. 
> > 
> > I am not a programmer so I am not sure how to extend
> > it. As you know I asked a while back how I can make
> > archive folders based on the sender name or domain. 
> > 
> > Do you have any recommendations or examples of how to
> > do this?
> 
> Here is an example:
> 
>   results = match(myaccount, 'INBOX', myfilter)
>   headers = fetchheaders(myaccount, 'INBOX', { 'from' }, results)
> 
>   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
> 		  print(msgid, user, domain)
> 		  -- move(myaccount, 'INBOX', myaccount, user, { msgid })
> 	  end
> 	  -- check for "From: Foo Bar <foo at bar>"
> 	  _, _, user, domain = string.find(msghdrs, 'From: .+<([%w.-]+)@([%w.-]+)>')
> 	  if (user ~= nil or domain ~= nil) then
> 		  print(msgid, user, domain)
> 		  -- move(myaccount, 'INBOX', myaccount, user, { msgid })
> 	  end
>   end
> 
> Basically, it fetches the "From" headers of the messages that match the filter,
> tries to find the user and domain of the message and then moves the messages to
> a mailbox named after the user (actually it just prints the user and domain,
> for testing purposes).
> 
> 
-- 
Ezsra McDonald

......................................................................

Linux is like a wigwam -- no Gates, no Windows, and an Apache inside.

CONFIDENTIALITY NOTICE:
This E-mail and any attachments are confidential.  If you are not the
intended recipient, you do not have permission to disclose, copy,
distribute, or open any attachments.  If you have received this E-mail
in error, please notify us immediately by returning it to the sender 
and delete this copy from your system.




More information about the Imapfilter-devel mailing list