> On Dec 27, 2016, at 2:32 AM, Tim Hanson <tbh...@gmail.com> wrote:
> 
> Hi, I think I'd like to try using racket as a way to explore relatively 
> simple ways of getting a grip on some mail accounts that have gotten out of 
> hand. Specifically I'd like to be able to
> - compute some statistics, such as number of mails from various addresses
> - reorganize / clean-up, say by moving mails into folders according to rules 
> or deleting mails from specific senders older than a given amount
> 
> I have done similar things in the past using built-in functions in mail 
> clients such as outlook or entourage. (If thunderbird supports such functions 
> I could use that now, but I haven't found clues that it does; I even looked 
> at AppleScript and began using it to explore, but quickly got pretty 
> frustrated with the language and its documentation.)
> 
> I like racket, though, and it would seem to be great to be able to try out 
> functions interactively in the REPL.
> 
> My impression on first glance though, was that the built-in IMAP support 
> would require quite a bit of assembly and additions. I also found SirMail, 
> which looks like more than what I need, though presumably I could make use of 
> its foundation parts if I could figure out which and how.
> 
> Might it be appropriate to build something between the IMAP basics and 
> SirMail? Alternatively, is there a brief how-to for SirMail? I found the 
> sources on github and they seemed to work immediately, but I have some 
> questions about set-up. Perhaps I could even help write a brief "how to" with 
> input from the authors?
> 
> (Maybe there are more appropriate, "easier" ways of doing what I want to do 
> that others think of immediately, in which case clues would be appreciated.)


I recently had to download, parse, and pre-grade IMAP folders. Other than 
fiddling with a bunch of flags, it turned out to be a relatively 
straightforward scripting task. What you’re asking for is a scrip that will 
sooner or later turn into an application. Once you have that, I bet it will be 
easy to factor out a library that you can package up and deposit at 
pkgs.racket-lang.org <http://pkgs.racket-lang.org/>. 

I am appending the module that connected my script to the IMAP server. 

(The script was used by a number of colleagues here, which uncovered a weakness 
in the encoding step of IMAP. If you are using ‘unusual’ passwords, work with 
GIT HEAD instead of 6.7.) 



#lang racket

(provide
 ;; type Message = (list String String String [Listof String])
 
 ;; String String String -> [Listof Message]
 ;; retrieves messages from user's Zimbra CCS email at folder
 retrieve-messages)

;; 
---------------------------------------------------------------------------------------------------
;; dependencies

(require net/imap)
(require net/head)

;; 
---------------------------------------------------------------------------------------------------
;; implementation 

(define ZIMBRA “…")
(define TLS-1? #true)

(define (retrieve-messages user password folder)
  (define flags '(header body))
  (define-values (imap-connection messages# nu)
    (imap-connect ZIMBRA user password folder #:try-tls? TLS-1?))

  (define messages (imap-get-messages imap-connection (build-list messages# 
add1) flags))

  (for/list ((s messages))
    (define head (first s))
    (define to            (bytes->string/utf-8 (extract-field #"to" head)))
    (define from          (bytes->string/utf-8 (extract-field #"from" head)))
    (define subject       (bytes->string/utf-8 (extract-field #"subject" head)))
    (list to from subject (bytes->strings (second s)))))

;; [Listof Bytes] -> [Listof String]
(define (bytes->strings b)
  (for/list ((l (port->bytes-lines (open-input-bytes b))))
    (bytes->string/utf-8 l)))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to