A fix for cat2email in WordPress 2.1 and above

Club Troppo, a site that I administer, relies on the cat2email plugin to post certain categories to interested users (there are lots). When I upgraded to WP 2.1, a lot of users began to complain that they were getting improperly formatted files. The headers would be something like this:

Content-type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset="UTF-8"

Obviously problematic, as many email programs would then render HTML mails as plain text. Hilarity ensues.

Well first off, there’s a tiny bug in the cat2email code. It should put out ‘Content-Type’, not ‘Content-type’. Fix this and you get a different set of incorrect headers:

Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hmm, still useless.

The culprit, it turns out, is WordPress. In 2.1 and 2.2, the wp_mail function was modified to use the PHPMailer class. If you crack open wp-includes/pluggable.php, you’ll see that the wp_mail function always forces conversion to plaintext. Pretty dodgy.

What it ought to do is check for custom headers and work with them. I’ve submitted a patch to the WordPress mob, and if you really need it, see below.

--- /Users/jacques/Downloads/wordpress/wp-includes/pluggable.php
+++ pluggable.php
@@ -187,7 +187,15 @@
$phpmailer->AddAddress("$to", "");
$phpmailer->Subject = $subject;
$phpmailer->Body = $message;
- $phpmailer->IsHTML(false);
+ if ( strstr( $headers, 'Content-Type: text/html' ) )
+ {
+ $phpmailer->IsHTML(true);
+ }
+ else
+ {
+ $phpmailer->IsHTML(false);
+ }
$phpmailer->IsMail(); // set mailer to use php mail()

do_action_ref_array(‘phpmailer_init’, array(&$phpmailer));

Update: Looks like a more robust fix is scheduled for WordPress 2.3.

This entry was posted in Geekery. Bookmark the permalink.