OUTPUT BUFFER:
## 8.4.3 with trf2.1p1 has problems when location ## of Trf package isn't explicitly stated like this ## set ::env(PATH) $::env(PATH)\;H:/tcl/lib/trf ## because Trf can't find crypt.dll package require smtp namespace eval dg_mail { namespace export sendmail variable mailhost 127.0.0.1 variable mailport 25 } proc dg_mail::sendmail {to from subject body {headers ""} {bcc ""} {opt_attcs_type ""} {attcs_array_name ""}} { if {[string length $opt_attcs_type]} { if {$opt_attcs_type != "-file" && $opt_attcs_type != "-string"} { error "unknown option $opt_attcs_type" } if {! [string length $attcs_array_name]} { error "unknown value for option $opt_attcs_type" } upvar 0 $attcs_array_name attcs_array set parts [::mime::initialize -canonical "text/plain" -string $body] foreach {attc_name attc_options} [array get attcs_array] { set part [eval ::mime::initialize $attc_options] lappend parts $part } set messageT [::mime::initialize -canonical "multipart/mixed" -parts $parts] } else { set messageT [::mime::initialize -canonical text/plain -string $body] } set command [list ::smtp::sendmessage $messageT -servers $CMDef(SMPTServer) -ports $CMDef(SMPTPort)] lappend command -header [list From $from] lappend command -header [list To $to] lappend command -header [list Subject $subject] if {[string length $bcc]} { lappend command -header [list Bcc $bcc] } if {[string length $headers]} { foreach {key value} $headers { lappend command -header [list $key $value] } } set err [catch { eval $command } result] ::mime::finalize $messageT -subordinates all if {$err} { error $result } } array set attcs_options {} set attcs [file join [pwd] [info script]] foreach attc $attcs { ## charset is optional set opts [list -canonical "text/plain; charset=ascii; name=\"[file tail $attc]\""] lappend opts -encoding quoted-printable lappend opts -header {Content-Disposition attachment} lappend opts -file $attc set attcs_options($attc) $opts } dg_mail::sendmail sinisa@datagram.dg djoko2000@datagram.dg {Tcl smtp!} Hello! "" "" -file [namespace current]::attcs_options ## exa 1 if {[string length $opt_attcs_type]} { log "Type: $opt_attcs_type" log "Processing: $file" set parts [::mime::initialize -canonical text/plain -string $body] set imageT [::mime::initialize -canonical "image/tif; name=\"[file tail $file]\"" -file $file] lappend parts $imageT set messageT [::mime::initialize -canonical multipart/mixed -parts $parts] } else { set messageT [::mime::initialize -canonical text/plain -string $body] } set opts [list -canonical "text/plain; charset=ascii; name=\"[file tail $attc]\""] # Example of how to use sendmail proc with PDF file types. foreach attc $attcs { ## charset is optional set opts [list -canonical "application/pdf; name=\"[file tail $attc]\""] lappend opts -encoding base64 lappend opts -header {Content-Disposition attachment} lappend opts -file $attc log "Adding $opts to attachment array." set attcs_options($attc) $opts } # Note: without the base64 encoding of the attachment I got errors when opening the attachment sent via email. Bezoar 5/14/2014 - for a general catchall mime type that will send all types of files use application/octet-stream set parts [::mime::initialize -canonical text/plain -string $body] lappend parts [::mime::initialize -canonical "application/pdf; name=\"[file tail $pdfname]\"" -encoding base64 -header {Content-Disposition attachment} -file $pdfname] set token [::mime::initialize -canonical multipart/mixed -parts $parts] set command [list ::smtp::sendmessage $token -servers $mailhost -ports $mailport -username $mailuser -password $mailpass -usetls 1 -header [list From "$from"] -header [list To "$To"] -header [list Subject "$Subject"] -header [list Date "[clock format [clock seconds]]"]] if {[catch {eval $command} err]} { error "Error when sending mail: $err" } catch {::mime::finalize $token -subordinates all}