Sending mails in Common Lisp

Just a quickie that might be useful for people new to CL.

Install CL-SMTP if you don’t have it yet:

(asdf-install:install 'cl-smtp)

You’re now able to load it into your Lisp core at any time:

(asdf:oos 'asdf:load-op 'cl-smtp)

And use it, either interactively (HOST is your SMTP server, e.g. localhost):

(cl-smtp:send-email "HOST" "from@foo.com" "recipient@bar.com"
                    "SUBJECT" "BODY")

or non-interactively:

(handler-case
  (prog1 t
    (cl-smtp:send-email "HOST" "from@foo.com" "recipient@bar.com"
                        "SUBJECT" "BODY"))
  (error (msg) (format t "Could not send mail: ~A~%" msg) nil))

We need the prog1 because send-email always returns nil, even on success.
With the above, the whole expression will return true on success, which lets us decide afterwards whether we were able to hand our message to the mail server.

Comments

  1. Raja r
    February 8th, 2008 | 9:59 pm

    Did you run into this error at all:

    couldn’t read from #: Transport endpoint is not connected

    sendmail works from the bash command prompt, so I’m having a hard time figuring out what the problem is.

    Appreciate any help!

    Thanks
    Raja

  2. February 9th, 2008 | 11:11 am

    Note that CL-SMTP doesn’t use sendmail at all but connects directly to the target mail server.

    I’d look it up in your mail server’s manual or on your favorite search engine.

    Or try strace-ing your Lisp image so you can look a bit more at the guts of the messages.

  3. Raja r
    February 11th, 2008 | 6:44 pm

    That explains it then – I was confusing it with another package, mel-base.

    This works fine now.

    Thank you.
    Raja

Leave a reply