Add function to mimeword-encode mail headers, use it when sending
authorJamie Cameron <jcameron@webmin.com>
Sat, 20 Aug 2011 07:09:52 +0000 (00:09 -0700)
committerJamie Cameron <jcameron@webmin.com>
Sat, 20 Aug 2011 07:09:52 +0000 (00:09 -0700)
mailboxes/boxes-lib.pl
mailboxes/send_mail.cgi

index 366184e..6f2e177 100755 (executable)
@@ -1552,7 +1552,8 @@ sub encode_mimewords
 {
 my ($rawstr, %params) = @_;
 my $charset  = $params{Charset} || 'ISO-8859-1';
-my $encoding = lc($params{Encoding} || 'q');
+my $defenc = uc($charset) eq 'ISO-2022-JP' ? 'b' : 'q';
+my $encoding = lc($params{Encoding} || $defenc);
 my $NONPRINT = "\\x00-\\x1F\\x7F-\\xFF";
 
 if ($rawstr =~ /^[\x20-\x7E]*$/) {
@@ -1564,7 +1565,7 @@ if ($rawstr =~ /^[\x20-\x7E]*$/) {
 ###    We limit such words to 18 characters, to guarantee that the
 ###    worst-case encoding give us no more than 54 + ~10 < 75 characters
 my $word;
-$rawstr =~ s{([ a-zA-Z0-9\x00-\x1F\x7F-\xFF]{1,18})}{     ### get next "word"
+$rawstr =~ s{([ a-zA-Z0-9\x7F-\xFF]{1,18})}{     ### get next "word"
     $word = $1;
     $word =~ /(?:[$NONPRINT])|(?:^\s+$)/o ?
        encode_mimeword($word, $encoding, $charset) :   # unsafe chars
@@ -1574,6 +1575,35 @@ $rawstr =~ s/\?==\?/?= =?/g;
 return $rawstr;
 }
 
+# encode_mimewords_address(string, %params)
+# Given a string containing addresses into one with real names mime-words
+# escaped
+sub encode_mimewords_address
+{
+my ($rawstr, %params) = @_;
+my $charset  = $params{Charset} || 'ISO-8859-1';
+my $defenc = uc($charset) eq 'ISO-2022-JP' ? 'b' : 'q';
+my $encoding = lc($params{Encoding} || $defenc);
+if ($rawstr =~ /^[\x20-\x7E]*$/) {
+       # No encoding needed
+       return $rawstr;
+       }
+my @rv;
+foreach my $addr (&split_addresses($rawstr)) {
+       my ($email, $name, $orig) = @$addr;
+       if ($name =~ /^[\x20-\x7E]*$/) {
+               # No encoding needed
+               push(@rv, $orig);
+               }
+       else {
+               # Re-encode name
+               my $ename = encode_mimeword($name, $encoding, $charset);
+               push(@rv, $ename." <".$email.">");
+               }
+       }
+return join(", ", @rv);
+}
+
 # encode_mimeword(string, [encoding], [charset])
 # Converts a word with 8-bit characters to MIME words format
 sub encode_mimeword
index 5d6a2a6..0fad5f3 100755 (executable)
@@ -47,15 +47,18 @@ $subs = join("", map { "&sub=$_" } @sub);
 # Construct the email
 $in{'from'} || &error($text{'send_efrom'});
 $newmid = &generate_message_id($in{'from'});
+%enc = ( 'Charset' => $in{'charset'} );
 $mail->{'headers'} = [ [ 'From', $in{'from'} ],
-                      [ 'Subject', &encode_mimewords($in{'subject'}) ],
-                      [ 'To', &encode_mimewords($in{'to'}) ],
+                      [ 'Subject', &encode_mimewords($in{'subject'}, %enc) ],
+                      [ 'To', &encode_mimewords_address($in{'to'}, %enc) ],
                       [ 'Message-Id', $newmid ] ];
 if ($in{'cc'}) {
-       push(@{$mail->{'headers'}}, [ 'Cc', &encode_mimewords($in{'cc'}) ]);
+       push(@{$mail->{'headers'}},
+            [ 'Cc', &encode_mimewords_address($in{'cc'}, %enc) ]);
        }
 if ($in{'bcc'}) {
-       push(@{$mail->{'headers'}}, [ 'Bcc', &encode_mimewords($in{'bcc'}) ]);
+       push(@{$mail->{'headers'}},
+            [ 'Bcc', &encode_mimewords_address($in{'bcc'}, %enc) ]);
        }
 &add_mailer_ip_headers($mail->{'headers'});
 push(@{$mail->{'headers'}}, [ 'X-Priority', $in{'pri'} ]) if ($in{'pri'});