My supervisor had a message she needed to send to a lot of people today. Given a list of email addresses, and a message, how do you send the message to everyone?
Scheme, of course!
(require (lib "smtp.ss" "net")
(lib "head.ss" "net"))
(define the-mailserver "some.url")
(define the-from-address "some@email")
(define the-subject "Some email subject")
(define (send-to email-address the-message)
(let ([the-header empty-header])
(set! the-header
(insert-field "To" email-address the-header))
(set! the-header
(insert-field "From" the-from-address the-header))
(set! the-header
(insert-field "Subject" the-subject the-header))
(fprintf (current-error-port) "~a" email-address)
(smtp-send-message
the-mailserver
the-from-address
(list email-address)
the-header
the-message)
))
(define (read-file-to-los file)
(let ([ip (open-input-file file)]
[lines '()])
(let loop ([line (read-line ip)])
(unless (eof-object? line)
(set! lines (cons line lines))
(loop (read-line ip))))
(set! lines (reverse lines))
lines
))
(define (send-to-all address-file file)
(let ([message (read-file-to-los file)]
[addresses (read-file-to-los address-file)])
(for-each (lambda (addr)
(send-to addr message))
addresses)))
To send the messages, load the code into MzScheme:
mzscheme -gf whatever-you-saved-this-as.scm
and then call
(send-to-all "name-of-address-file" "name-of-message-file")
The address file should contain one email address per line.
I must admit, read-file-to-los could be more functional, and perhaps more concise… but that’s not what I did today. Eh.
I don’t claim there is anything about this piece of code that is interesting, or exciting, or otherwise “good”. It was just nice that I was able to write it in 10 minutes and save someone else an hour or so when they would have otherwise engaged in a tedious, repetitive task better suited for a language in the LISP family.
I have switched to PLT Scheme for so many of my scripting and programming tasks that these kinds of things simply become non-issues for me. I wonder what people who don’t know Scheme do? I guess they program in Python, or *shudder* Perl.