| Ticket UUID: | f44bcbf0682926a4c7bed9dd45c806bf60d2751d | |||
| Title: | Procedure mime::encodingasciiP always returns 0 if the line contains \r | |||
| Type: | Bug | Version: | 1.6 and 1.7.2 | |
| Submitter: | ernest | Created on: | 2026-07-29 00:00:46 | |
| Subsystem: | mime | Assigned To: | ||
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Open | Last Modified: | 2026-09-05 23:37:06 | |
| Resolution: | None | Closed By: | nobody | |
| Closed on: | ||||
| Description: |
Since version 1.7.2 of the mime package a string that contains \r\n at the end of the line will cause the mail message to include a line stating the Content-Transfer-Encoding to be base64 even if it was not explicitly specified.
This is caused by the fact that the mime::encodingasciiP contains a bug, preventing it from properly validating the position of the \r character.
This causes the encoding to be set to base64, even for plain text.
The text is then actually base64 encoded if smtp sends a message, however, mail systems fail to recognize it as actually being base64 encoded, showing jibberish instead.
The bug already exists in the 1.6 version of mime, but version 1.7.2 now forces the Content-Transfer-Encoding and Content-Type to be included if smtp builds the header.
This causes the message to show up garbled.
mime::getheader adds
set tencoding [getTransferEncoding $token]
if {$tencoding ne {}} {
lappend result Content-Transfer-Encoding $tencoding
}
lappend result Content-Type [getContentType $token]
In smtp::sendmessage after the comment line:
# Take all the headers defined earlier and add them to the MIME message.
the sendmessage procedure gets fed the Content-Transfer-Encoding header.
In tcllib version 1.19 with mime package 1.6, this did not occur.
But in tcllib 2.0 with mime package 1.7.2 it cannot be avoided that the Content-Transfer-Encoding is added, and it's value is wrong.
The code for the procedure mime::encodingasciiP reads:
# ::mime::encodingasciiP --
#
# Checks if a string is a pure ascii string, or if it has a non-standard
# form.
#
# Arguments:
# line The line to check.
#
# Results:
# Returns 1 if \r only occurs at the end of lines, and if all
# characters in the line are between the ASCII codes of 32 and 126.
proc ::mime::encodingasciiP {line} {
foreach c [split $line {}] {
switch -- $c {
{ } - \t - \r - \n {
}
default {
binary scan $c c c
if {($c < 32) || ($c > 126)} {
return 0
}
}
}
}
if {
[set r [string first \r $line]] < 0
||
$r == {[string length $line] - 1}
} {
return 1
}
return 0
}
Near the end, the line 2246:
$r == {[string length $line] - 1}
should read:
$r == [expr {[string length $line] - 1}]
Otherwise, the stuff between the curly braces will be interpreted as a string instead of evaluating the expression, which will cause the position of the carriage return to never equal the string "[string length $line] - 1".
And thus always return 0.
| |||
| User Comments: |
ernest added on 2026-09-05 23:37:06:
Maybe this should be a separate ticket, but if a text has at least one line that is longer than 76 characters, or any of the other line requirements for E-mail fail, the Content-Transfer-Encoding line with setting base64 is forced to be added, but if the line is forcibly added, the code assumes that the user added that line and encoded the message in base64. The getbody routine has a switch where the base64 case is empty. While it should set the encoding variable to base64 if it was forcibly added by the package itself. Which also happens when the CR problem is detected, either by mistake or if it was a valid bad CR condition. ernest added on 2026-08-19 01:55:16: It looks like even though the line "Content-Transfer-Encoding: base64" is included when sending the mail message, the content is actually not encoded in base64. However, since the line is included, the mail client will interpret the unencoded data as being base64 encoded, and decoding non-encoded data causes jibberish to be displayed. So either if the code forces the line about base64 encoding to be included, it should also actually encode the data, or leave the message as is. ernest added on 2026-07-29 21:38:48: Oops, the example should have been without a newline. So it should read:
set SimpleLine "Simple line\r"
Don't know how to edit a User Comment.
ernest added on 2026-07-29 00:19:32: Here is some example code:
######################################################################
# Name: mime_test
# Purpose: Test mime package.
#
# History:
# 29-07-2026 E.R. Schreurs
# 1.0 (Initial Version)
#
proc mime_test { args } {
package require mime
package require smtp
# SMTP Config
set mailserver "smtp.dummy.com"
set sender "[email protected]"
set username ""
set password ""
set SimpleLine "Simple line\r\n"
set result [mime::encodingasciiP $SimpleLine]
puts "encodingasciiP for \"Simple line\\r\\n\" is $result"
set body "Simple line\r\nAnotherline\r\n"
set recipient "[email protected]"
#
# Create a complete MIME encoded message
#
set header [list From [email protected]]
append mailmsg "Subject: Test mime package\n"
append mailmsg "To: [email protected]\n"
append mailmsg "From: [email protected]\n"
append mailmsg "Sender: [email protected]\n"
append mailmsg "MIME-Version: 1.0\n"
append mailmsg "Content-Type: text/plain; charset=Cp1252\n"
append mailmsg "\n"; # blank line is the end of the header
append mailmsg $body
puts stdout "Mail message: $mailmsg"
set token [mime::initialize -string $mailmsg]
smtp::sendmessage $token \
-recipients $recipient \
-debug $debug \
-header $header \
-originator $sender \
-username [lindex $username 0] \
-password [lindex $password 0] \
-servers $mailserver
mime::finalize $token -subordinates all
return
}
| |||
Home
Blog
Docs
Downloads
Tickets
Timeline
Branches
Tags
Wiki