set m [open "|mail $email" w]
puts $m "Subject: $subj\n"
puts $m $text
close $m
Does anybody have a script for attaching a file?
--
Martin
> Does anybody have a script for attaching a file?
I have the following:
# sendmail_SMTP --
#
# Creates and sends a mail to a user using SMTP.
# RFC 821 governs SMTP.
#
# Arguments:
# SMTP_host SMTP host to use (assumes socket 25)
# to list of users to send mail to
# cc list of users to CC: send mail to
# from return email address
# subject subject for the mail
# text body of the mail
# fields optional extra fields to place in mail headers
# Results:
# sends a mail to the specified user.
#
proc sendmail_SMTP {SMTP_host to cc from subject text {fields {}}} {
set SMTP_socket 25
set socket [socket $SMTP_host $SMTP_socket]
puts $socket "HELO icn.siemens.de"
flush $socket
gets $socket; #drain the channel
puts $socket "MAIL FROM: <$from>"
flush $socket
gets $socket; #drain the channel
foreach name [concat $to $cc] {
puts $socket "RCPT TO: <[string trim $name]>"
flush $socket
}
gets $socket; #drain the channel
puts $socket DATA
flush $socket
gets $socket; #drain the channel
puts $socket "From: <$from>"
foreach name $to {
puts $socket "To: <[string trim $name]>"
}
foreach name $cc {
puts $socket "Cc: <[string trim $name]>"
}
puts $socket "Subject: $subject"
if {[string compare $fields {}]} {
puts $socket "$fields"
}
puts "\n"
# Uniformize newlines.
foreach line [split $text \n] {
if {[string match .* $line]} {
puts $socket .$line
} else {
puts $socket $line
}
}
puts $socket .\nQUIT
flush $socket
gets $socket; #drain the channel
close $socket
}
# sendmail --
#
# Creates and sends a mail to a user.
# Expects /usr/lib/sendmail to be available.
#
# Arguments:
# -to str user to send mail to (multiple users comma separated)
# -from str return email address
# -subject str subject for the mail
# -body str body of the mail
# -fields str extra fields to place in mail headers
# -attach file file to include as MIME attachment
# Results:
# sends a mail to the specified user.
#
proc sendmail {args} {
# cheap getopts, no error handling
array set opts {
-to {} -from {} -body {} -fields {} -subject {} -attach {} -cc {}
}
foreach {key val} $args {
if {[info exists opts($key)]} { set opts($key) $val }
}
if {[string match {} $opts(-to)] || [string match {} $opts(-from)]}
{
cgi_die "Error: -to and -from must be specified to sendmail\
\n[info level 0]"
}
if {[string length $opts(-attach)]} {
if {[catch {open $opts(-attach) r} atfid]} {
cgi_die "Error: couldn't find file to attach \"$opts(-attach)\""
}
fconfigure $atfid -translation binary
}
if {0} {
##
## SMTP version
##
## This hack sends directly through an SMTP host instead of
## relying on a local sendmail
if {[string length $opts(-attach)] && [info exists atfid]} {
set fullbody {}
set opts(-attach) [file tail $opts(-attach)]
set bound "----NEXT_PART_[clock seconds].[pid]"
append fullbody "Content-Type: multipart/mixed;
boundary=\"$bound\"\n"
append pre "This message is in MIME format. " \
"Since your mail reader does not understand" \
"\nthis format, some or all of this message may " \
"not be legible.\n\n"
append body "Content-Type: text/plain; charset=\"iso-8859-1\"\n" \
"Content-Transfer-Encoding: base64\n\n"
set mainbody [Base64_Encode $opts(-body)]
set data [read $atfid]
close $atfid
if {[info tclversion]<8.1 || [string first \0 $data]>=0} {
append attach "Content-Type: application/octet-stream;\
name=\"$opts(-attach)\"\n" \
"Content-Disposition: attachment;\
filename=\"$opts(-attach)\"\n\n"
} else {
set data [Base64_Encode $data]
append attach "Content-Type: text-plain; charset=\"iso-8859-1\";\
name=\"$opts(-attach)\"\n" \
"Content-Transfer-Encoding: base64\n" \
"Content-Disposition: attachment;\
filename=\"$opts(-attach)\"\n\n"
}
set len [string length
"$pre--$bound\n$body$mainbody\n--$bound\n$attach$data\n--$bound--"]
append fullbody "Content-Length: $len\n\n"
append fullbody
"$pre--$bound\n$body$mainbody\n--$bound\n$attach$data\n--$bound--\n"
} else {
set fullbody "\n$opts(-body)"
}
if {[catch {
sendmail_SMTP $::DIR(MAILHOST) \
[split $opts(-to) ,] [split $opts(-cc) ,] \
$opts(-from) "AutoSSI $opts(-subject)" $fullbody \
"X-Ident-From: SSI AutoMailer"
} err]} {
cgi_die $err
}
} else {
##
## SENDMAIL version
##
set mailprog "/usr/lib/sendmail -t -oi"
if {[catch {open "|$mailprog" r+} mailfid]} {
cgi_die "Error: couldn't execute \"$mailprog\"" $mailfid
}
puts $mailfid "X-Ident-From: SSI AutoMailer"
puts $mailfid "From: $opts(-from)"
puts $mailfid "Subject: AutoSSI $opts(-subject)"
puts $mailfid "To: $opts(-to)"
puts $mailfid "Cc: $opts(-cc)"
if {[string length $opts(-fields)]} {
puts $mailfid $opts(-fields)
}
if {[string length $opts(-attach)] && [info exists atfid]} {
set opts(-attach) [file tail $opts(-attach)]
set bound "----NEXT_PART_[clock seconds].[pid]"
puts $mailfid "Content-Type: multipart/mixed; boundary=\"$bound\""
append pre "This message is in MIME format. " \
"Since your mail reader does not understand" \
"\nthis format, some or all of this message may " \
"not be legible.\n\n"
append body "Content-Type: text/plain; charset=\"iso-8859-1\"\n" \
"Content-Transfer-Encoding: base64\n\n"
set mainbody [Base64_Encode $opts(-body)]
set data [read $atfid]
close $atfid
if {[info tclversion]<8.1 || [string first \0 $data]>=0} {
append attach "Content-Type: application/octet-stream;\
name=\"$opts(-attach)\"\n" \
"Content-Disposition: attachment;\
filename=\"$opts(-attach)\"\n\n"
} else {
set data [Base64_Encode $data]
append attach "Content-Type: text-plain; charset=\"iso-8859-1\";\
name=\"$opts(-attach)\"\n" \
"Content-Transfer-Encoding: base64\n" \
"Content-Disposition: attachment;\
filename=\"$opts(-attach)\"\n\n"
}
set len [string length
"$pre--$bound\n$body$mainbody\n--$bound\n$attach$data\n--$bound--"]
puts $mailfid "Content-Length: $len\n"
puts $mailfid
"$pre--$bound\n$body$mainbody\n--$bound\n$attach$data\n--$bound--"
} else {
puts $mailfid "\n$opts(-body)"
}
if {[catch {close $mailfid} err]} {
cgi_error "sendmail error: $err"
}
}
}; #end proc sendmail
# Base64_Encode --
#
# base 64 encoding routine, from Brent Welch
#
# Arguments:
# string string to base64 encode
# Results:
# base64 encoded string
#
proc Base64_Encode {string} {
global base64_enc
if {![info exists base64_enc(A)]} {
set i 0
foreach char {
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 + /
} {
set base64_enc($i) $char
incr i
}
}
set result {}
set state 0
set length 0
foreach c [split $string {}] {
scan $c %c x
switch [incr state] {
1 { append result $base64_enc([expr {($x >>2) & 0x3F}]) }
2 { append result $base64_enc([expr {(($old << 4) & 0x30) | (($x >>
4) & 0xF)}]) }
3 {
append result $base64_enc([expr {(($old << 2) & 0x3C) | (($x >> 6) &
0x3)}])
append result $base64_enc([expr {($x & 0x3F)}])
set state 0
}
}
set old $x
incr length
if {$length >= 72} {
append result \n
set length 0
}
}
set x 0
switch $state {
0 { # OK }
1 { append result $base64_enc([expr {(($old << 4) & 0x30)}])== }
2 { append result $base64_enc([expr {(($old << 2) &
0x3C)}])= }
}
return $result
}
here's what the calling sequence would look like assuming that you wanted to
include a plaintext file in the message:
smtp::sendmessage \
[mime::initialize -canonical text/plain -param {charset us-ascii} \
-param [list name $file] -file $file] \
-header [list To $address] \
-header [list Subject $subject]
i'm looking for alpha testers. if anyone's interested, reply privately.
/mtr
Michalowski, Martin [KAN:0Q64:EXCH] <stud...@americasm01.nt.com> wrote in
message news:379F47B5...@americasm01.nt.com...
> I'm using the following for sending email:
>
> set m [open "|mail $email" w]
> puts $m "Subject: $subj\n"
> puts $m $text
> close $m
>
> Does anybody have a script for attaching a file?
>
> --
> Martin