proc EmailSendMessage {args} { # Verify and set the command line arguments. set requiredArgs {-recipient -subject -body} set smtpServer 10.10.125.10 set smtpPort 25 set originator someone@somewhere.com set token [mime::initialize -canonical text/plain -string $body] mime::setheader $token Subject $subject smtp::sendmessage $token -recipients $recipient -servers $smtpServer \ -ports $smtpPort -originator $originator mime::finalize $token }
E-mail attachments constitute a surprisingly large and even unwieldy subject. The first clarification is to distinguish emission of e-mail with attachments, from its reception and interpretation. The Wiki page on the MIME package focuses mostly on the latter.
2002-10-31: Roberto Mello provides the following example:
package require mime package require smtp set rcpts [open email-list.txt] set files [lsort [glob *.JPG]] set text {Ooops! Some of the pictures got mixed up in the last e-mail.} append text \n {Let's try this again.} \ \n \n {Happy Halloween!} \ \n {Your friendly MASA Villages Staff} set i 0 while {[gets $rcpts rcpt] != -1} { set this_image [lindex $files $i] # create an image and text set imageT [mime::initialize -canonical \ "image/jpg; name=\"$this_image\"" -file $this_image] set textT [mime::initialize -canonical text/plain -string $text] # create a multipart containing both, and a timestamp set multiT [ mime::initialize -canonical multipart/mixed -parts [ list $imageT $textT]] # send it to some friends puts "Sending to $rcpt: $this_image" smtp::sendmessage $multiT \ -header [list From {Juliane Mello <jmello@usu>}] \ -header [list To $rcpt] \ -header [list Subject Ooops!] incr i }
The variety of ways to transmit e-mail attachments with Tcl is large. CL keeps a few models in [1].One of the reasons for active interest in the range of possibilities is typical for Tcl: one wants to compensate for various constraints. mime had severe flaws before tcllib 0.8 (and still has other errors--more on that, later) and is essentially unreliable with Tcl before 8.2.CL frequently needs to set up quick e-mail operations on hosts equipped with old interpreters; 7.6, for example, in the case of a client met in January 2002. For older interpreters, it's best to enlist outside help, as with
set tmpfile /tmp/abc[pid] set subject {This is the subject line.} exec /usr/local/bin/mpack -s $subject -o $tmpfile $standard_name set To {The distribution list} set recipients {first@host.com second@somewhere.com nemo@sea.org} set recipients [join $recipients ,] exec $sendmail -t << \ "To: ($To) Bcc: $recipients [exec cat $tmpfile]"
Mutt: Someone had contributed a mutt example that was not correct (mutt tossed back a "bad usage" message). CL's taken the liberty of simplifying the example from four lines to one, in the process of correcting it:
exec mutt -s $subject -a $attachment $to << $body
Other useful external Unix utilities include pine and uuencode [give examples].
CMM: Can anyone enlighten on the proper way to unencode incoming binary attachments for output to disk?CMM: Note for the unwise (like me), properly used the mime package will automatically unencode base64 encoding by default. My issue was that I wasn't outputting in binary mode. To fix this: fconfigure $fileid -translation binary
FP: Another useful Unix utility for this job is UUDeview
uuenview -b -m nemo@sea.org -s {The Subject} filename