[Mimedefang] base64 to quoted-printable

Dianne Skoll dfs at roaringpenguin.com
Fri Oct 13 10:20:04 EDT 2017

On Fri, 13 Oct 2017 06:53:33 -0700
"Michael Fox" <news at mefox.org> wrote:

> While I appreciate everyone's attempt to warn me that "there be
> dragons", the facts are that the client is what it is, it serves
> hundreds of people (in my location alone), and the problem needs
> solving.  

OK.  You will most likely have to do all your changes in
filter_end.  You'll need to traverse the MIME tree, looking for
text/* parts.  If any are Base-64 encoded, you'll need to load them in
and re-encode them using Quoted-Printable.  As you do this, you need
to build up a *new* replacement MIME::Entity.  Finally, call
replace_entire_message with your new entity as the argument.

I won't give the actual Perl code, but here's a basic sketch, completely
untested.  I really have no idea whether or not this will work, but I
think the basic approach is correct. :)

sub re_encode
	my ($in_entity, $out_entity, $did_something) = @_;

	# If it's multipart, recurse
	if ($in_entity->is_multipart) {
		my $new_entity = MIME::Entity->build(Type => $in_entity->type);
		foreach my $p (@{$in_entity->parts}) {
			if (re_encode($p, $new_entity, $did_something)) {
				$did_something = 1;
		return $did_something;

	# non-recursive case: Single part.
	# If $in_entity is base-64 encoded, make a new $new_entity
	# that is qp-encoded and call $out_entity->attach($new_entity)
	# and return 1 to indicate that a change was made.
	# Otherwise, call $out_entity->attach($in_entity)
	# and return 0.

sub filter_end
	my ($entity) = @_;
	my $replacement_entity = MIME::Entity->build(Type => 'multipart/mixed');
	my $changes_made = re_encode($entity, $replacement_entity, 0);
	if ($changes_made) {
		# Recursively remove single-part "multiparts" starting
		# from top-level.
		while ($replacement_entity->is_multipart && scalar(@{$replacement_entity->parts}) == 1) {
			$replacement_entity = $replacement_entity->parts(0);

More information about the MIMEDefang mailing list