Version 18 of smtp

Updated 2005-02-20 08:33:35 by CMCc

Documentation can be found at http://tcllib.sourceforge.net/doc/smtp.html


Here's a minimal useful example:

      proc send_simple_message {recipient email_server subject body} {
          package require smtp
          package require mime

          set token [mime::initialize -canonical text/plain -string $body]
          mime::setheader $token Subject $subject
          smtp::sendmessage $token \
                  -recipients $recipient -servers $email_server
          mime::finalize $token
      }

      send_simple_message [email protected] localhost \
                "This is the subject." "This is the message."

OK, this is more minimal:

         ...
      set token [mime::initialize -canonical text/plain -string $body]
      smtp::sendmessage $token \
              -header [list Subject $subject] \
              -header [list To $recipient]
      mime::finalize $token
         ...

This last one didn't work for me; I kept getting an error from sendmessage about { } not being a valid option. -- WHD.

WHD, CL will make a point of returning in July 2002 to address this error. What's above certainly worked with ... well, *some* version.

See also http://www.magma.ca/~glennj/tcl/mutt_sendmail.tcl.txt


PT 8-July-2004: Here is a rather more complex example that can handle an authenticating SMTP server.

 set tok [mime::initialize -canonical text/plain -string $TEXT]
 smtp::sendmessage $tok \
        -servers [list $SERVER] -ports [list $PORT] \
        -usetls 1 \
        -username $USERNAME \
        -password $PASSWORD \
        -header [list From "$FROM"] \
        -header [list To "$TO"] \
        -header [list Subject "$SUBJECT"] \
        -header [list Date "[clock format [clock seconds]]"]    
 mime::finalize $tok

Note: if you have trouble with sending mail, adding -debug 1 as an option to the [sendmessage] command will log the SMTP conversation and may help diagnose problems.


SV: See also SMTP with attachments


CMcC 050220: I have put together a first cut asynchronous SMTP client here [L1 ] ... I'd be interested to see if it works for people.

The design is moderately interesting. It's a snit object which is mostly composed of methods of the form X-Y where X is the protocol command last issued, and Y is a glob to match the server's response code. This is a finite state machine, and the fileevent bubbles through it driven by responses from the server.

Given this FSA design, it should be easy enough to use the tcllib finite state machine to drive it, too, if that were desired. I have reservations about too great a reliance on FSA as a programming technique, though.


Category Package, subset Tcllib