[AOLSERVER] ns_sendmail is wrong for non-ascii strings

23 views
Skip to first unread message

Alexey Pechnikov

unread,
May 26, 2009, 2:00:52 PM5/26/09
to AOLS...@listserv.aol.com
Hello!

ns_sendmail can't work for non-ascii subject and body.
I wrote the simple wrapper. is it possible to fix upstream version
or add like wrapper?

============================
# note: _ns_sendmail is exists!
if {[info commands orig_ns_sendmail] eq {}} {
rename ns_sendmail orig_ns_sendmail
package require base64
# headers are ignored!
proc ns_sendmail {to from subject body args} {
set headerSet [ns_set create]
ns_set put $headerSet Content-Type {text/plain; charset=UTF-8}
ns_set put $headerSet Content-Transfer-Encoding base64
ns_set put $headerSet MIME-Version 1.0

set subject [base64::encode [encoding convertto utf-8 $subject]]
set subject "=?UTF-8?B?${subject}?="
set body [base64::encode [encoding convertto utf-8 $body]]
orig_ns_sendmail $to $from $subject $body $headerSet [lindex $args 1]
}
}
============================

Best regards, Alexey Pechnikov.
http://pechnikov.tel/


--
AOLserver - http://www.aolserver.com/

To Remove yourself from this list, simply send an email to <list...@listserv.aol.com> with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: field of your email blank.

Dossy Shiobara

unread,
May 26, 2009, 3:16:05 PM5/26/09
to AOLS...@listserv.aol.com
On 5/26/09 2:00 PM, Alexey Pechnikov wrote:
> ns_sendmail can't work for non-ascii subject and body.
> I wrote the simple wrapper. is it possible to fix upstream version
> or add like wrapper?

Nice wrapper - this assumes the receipient can understand MIME-encoded
mail, which is probably the case but would break existing code that
expects outgoing mail having the old behavior.

Perhaps only define the wrapper if a ns_param is set in the
configuration, and by default not apply the wrapper? Or, better yet:
integrate your change into ns_sendmail directly and again, activate it
using an optional proc argument or ns_param config setting.

Ideally, it's probably best to work to define a reasonable "ns_mail"
interface and implement it. ns_sendmail is an ugly wart that needs to
go ...

--
Dossy Shiobara | do...@panoptic.com | http://dossy.org/
Panoptic Computer Network | http://panoptic.com/
"He realized the fastest way to change is to laugh at your own
folly -- then you can let go and quickly move on." (p. 70)

Bernhard van Woerden

unread,
May 26, 2009, 4:33:59 PM5/26/09
to AOLS...@listserv.aol.com
If the subject is long then it should really go on multiple lines using header folding.
checkout mime::word_encode in tcllib
The From and To headers may also contain non ASCII characters.

I use the following proc to fold and encode header values

proc email_header_fold {string} {
    # Fold header into lines starting with a space as per rfc2822
    set width 78

    set start 0
    set list {}
    while { $start<[string length $string] \
      && [regexp -indices -start $start --  {(\"[^\"]+\")|(\([^\)]+\))|([^ ]+)} $string match] } {
    set atom [string range $string [lindex $match 0] [lindex $match 1]]
    # If characters above 127 then encode
    if { [regexp {([\u007F-\u00FF])} $atom] } {
        set list [concat $list [split [mime::word_encode utf-8 quoted-printable $atom] \r\n]]
    } else {
        lappend list $atom
    }
    set start [expr {[lindex $match 1]+1}]
    }
    set result {}
    set line [lindex $list 0]
    foreach string [lrange $list 1 end] {
    if { [string length "$line $string"]<=78 } {
        append line " $string"
    } else {
        lappend result $line
        set line $string
    }
    }
    lappend result $line

    return [string map {\r\n "\r\n "} [join $result \r\n]]
}


-- Bernhard

2009/5/26 Alexey Pechnikov <pech...@mobigroup.ru>

Alexey Pechnikov

unread,
May 27, 2009, 3:22:43 AM5/27/09
to AOLS...@listserv.aol.com
Hello!

On Wednesday 27 May 2009 00:33:59 Bernhard van Woerden wrote:
> If the subject is long then it should really go on multiple lines using
> header folding.

Well, it's right comment.

===============
# note: _ns_sendmail is exists!
if {[info commands orig_ns_sendmail] eq {}} {
rename ns_sendmail orig_ns_sendmail
package require base64

package require mime

# headers are ignored!
proc ns_sendmail {to from subject body args} {
set headerSet [ns_set create]
ns_set put $headerSet Content-Type {text/plain; charset=UTF-8}
ns_set put $headerSet Content-Transfer-Encoding base64
ns_set put $headerSet MIME-Version 1.0

orig_ns_sendmail $to \
$from \
[mime::word_encode utf-8 quoted-printable [encoding convertto utf-8 $subject]] \
[base64::encode [encoding convertto utf-8 $body]] \
$headerSet [lindex $args 1]
}
}
===============

> The From and To headers may also contain non ASCII characters.

This code may be simple too, I must to think about...

Reply all
Reply to author
Forward
0 new messages