Reading emails using IMAP protocol in racket

I am using the following code to get an IMAP connection. I want to read emails. I have read this documentation and couldn't come from here.

my code:

#lang racket

(define imap-server "*****")
(define imap-port-no ***)
(define username "*****")
(define pw "*****")
(define mailbox-name "INBOX") 

(require openssl/mzssl

(define (test-connect)
  (let ([c (ssl-make-client-context)])
    (let-values ([(in out) (ssl-connect imap-server imap-port-no c)])
      (imap-connect* in out username pw mailbox-name))))

(define-values (imap cnt recent) (test-connect))


I am getting the number of emails and the number of recent emails from this. How to proceed from here. what functions should I call to read emails. Thanks in advance.


source to share

1 answer

Try something like this:

(imap-get-messages imap '(1) '(uid flags header body))


This should return a list containing the "fields" described by flags, which header

gives you the full header part and the body

email body. (This is just a quick experiment to see that everything works, you need to know which messages to retrieve, etc. - everything is described in the documentation .)

Here is a complete program that returns a list of headers you want for each message in the INBOX, where each message gets a list of headers and their values ​​as strings. But note that email is not very reliable for To:

this kind of thing - you can get the message no matter what appears in the field and there are many other headers with similar semantics (e.g. Resent-To:

similar to To:

, sometimes there is a header Sender:

that might be more reliable than From:

, etc.).

#lang racket/base

(define imap-server "")
(define imap-port-no 1234)
(define username "----")
(define pw "----")
(define mailbox-name "INBOX")

(require racket/list openssl/mzssl net/imap net/head)

(define (test-connect)
  (let ([c (ssl-make-client-context)])
    (let-values ([(in out) (ssl-connect imap-server imap-port-no c)])
      (imap-connect* in out username pw mailbox-name))))

(define-values [imap messages recent] (test-connect))

(define (get-interesting-headers ns)
  (for/list ([x (imap-get-messages imap ns '(header))])
     (λ (x)
       (define s
         (string->symbol (string-downcase (bytes->string/utf-8 (car x)))))
       (and (memq s '(from to date subject))
            (cons s (bytes->string/utf-8 (cdr x)))))
     (extract-all-fields (car x)))))
(get-interesting-headers (for/list ([i messages]) (add1 i)))




All Articles