WordPress.org

Support

Support » Plugins and Hacks » WP Better Emails » [Resolved] Converts html emails if sent after a plain text (fix provided)

[Resolved] Converts html emails if sent after a plain text (fix provided)

  • The check for whether an email is plain text is done in the set_content_type() method, but this permanently attaches the send_html() method as soon as it gets 1 plain text email. If a subsequent email is already html, the send_html() will still try to convert it.

    To fix this I added a check that runs per email:
    I added

    var $convert_this_email;

    after

    var $options = array();
    		var $page = '';

    and in set_content_type() I changed

    return $content_type = 'text/html';
    			}

    to

    $this->convert_this_email = true;
    				return 'text/html';
    			}
    			$this->convert_this_email = false;

    and in send_html() I changed

    function send_html($phpmailer) {

    to

    function send_html($phpmailer) {
    			if (!$this->convert_this_email)
    				return;

    http://wordpress.org/extend/plugins/wp-better-emails/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author hellonico

    @nlemoine

    Hi,

    HTML emails are not wrapped if they are already “text/html”.

    Can you provide me the situation that make this happen ?

    wp_mail($email, $subject, $message, $message_headers);
    ...
    add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
    wp_mail($different_email, $different_subject, $html_message, $message_headers);

    The first call sends a plain text email that is converted by better emails. set_content_type() switched on converting for the first email, and left it on for all subsequent emails. The second call will now try to send its own html mail but better emails will still try to convert it.

    Plugin Author hellonico

    @nlemoine

    Yes, it will try but won’t if the email is already text/html.

    I don’t think you understand. The wp_mail_content_type filter is fired for every email send, and can be different each time. Your code attaches to phpmailer_init as soon as the first text/plain is encountered. It isn’t reset after each email is sent, it stays attached. If another email is being sent that is already html, the function that is still attached to phpmailer_init fires anyway.

    Imagine a doorman. He asks everyone if they need help with their bags when he opens the door. As soon as one person says “yes”, he will help everyone. If the next person says no, he will try to help them anyway, because one person said “yes”.

    Plugin Author hellonico

    @nlemoine

    You’re right, sorry, I read and answered so fast I didn’t take the time to have a depth look at it. I’ll had the fix in the next update.

    Thanks for your feedback.

    The patch provided above works but is not quite optimal. The problem is that set_content_type() calls add_action(). If you are sending 100 emails in a loop, you do not want to call add_action 100 times (even though WP will simply replace the existing hook)

    Below is my patch. How come this plugin has not been fixed yet? maximinime reported this 2 months ago and I emailed the author 1 month ago with the below patch.

    diff -Naur wp-better-emails/wpbe.php wp-better-emails-krb/wpbe.php
    --- wp-better-emails/wpbe.php	2013-03-18 10:33:38.944313476 -0400
    +++ wp-better-emails-krb/wpbe.php	2013-03-18 10:35:22.584185730 -0400
    @@ -1,6 +1,6 @@
     <?php
     /*
    -  Plugin Name: WP Better Emails
    +  Plugin Name: WP Better Emails (FIXED)
       Plugin URI: http://wordpress.org/extend/plugins/wp-better-emails/
       Description: Beautify the default text/plain WP mails into fully customizable HTML emails.
       Version: 0.2.4.1
    @@ -38,6 +38,7 @@
    
     		var $options = array();
     		var $page = '';
    +		var $send_as_html; //KRB
    
     		/**
     		 * Construct function (old way)
    @@ -63,6 +64,8 @@
     			add_filter('wp_mail_from', array($this, 'set_from_email'));
     			add_filter('wp_mail_content_type', array(&$this, 'set_content_type'), 100);
    
    +			add_action('phpmailer_init', array(&$this, 'send_html')); //KRB
    +
     			if (!is_admin())
     				return;
    
    @@ -77,7 +80,7 @@
     				add_action('admin_head', array(&$this, 'load_wp_tiny_mce'));
     			if( version_compare($wp_version, '3.2', '<') && version_compare($wp_version, '3.0.6', '>') )
     				add_action( 'admin_print_footer_scripts', 'wp_tiny_mce_preload_dialogs');
    -
    +
     			// Filters
     			add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'settings_link'));
     			add_filter('contextual_help', array(&$this, 'contextual_help'), 10, 3);
    @@ -363,8 +366,10 @@
     		function set_content_type($content_type) {
     			// Only convert if the message is text/plain and the template is ok
     			if ($content_type == 'text/plain' && $this->check_template() === true) {
    -				add_action('phpmailer_init', array(&$this, 'send_html'));
    +				$this->send_as_html = true;  //KRB
     				return $content_type = 'text/html';
    +			} else {
    +				$this->send_as_html = false;		//KRB
     			}
     			return $content_type;
     		}
    @@ -376,16 +381,18 @@
     		 * @param object $phpmailer
     		 */
     		function send_html($phpmailer) {
    -			// Set the original plain text message
    -			$phpmailer->AltBody = wp_specialchars_decode($phpmailer->Body, ENT_QUOTES);
    -			// Clean < and > around text links in WP 3.1
    -			$phpmailer->Body = $this->esc_textlinks($phpmailer->Body);
    -			// Convert line breaks & make links clickable
    -			$phpmailer->Body = nl2br(make_clickable($phpmailer->Body));
    -			// Add template to message
    -			$phpmailer->Body = $this->set_email_template($phpmailer->Body);
    -			// Replace variables in email
    -			$phpmailer->Body = $this->template_vars_replacement($phpmailer->Body);
    +			if ( $this->send_as_html ) {
    +				// Set the original plain text message
    +				$phpmailer->AltBody = wp_specialchars_decode( $phpmailer->Body, ENT_QUOTES );
    +				// Clean < and > around text links in WP 3.1
    +				$phpmailer->Body = $this->esc_textlinks( $phpmailer->Body );
    +				// Convert line breaks & make links clickable
    +				$phpmailer->Body = nl2br( make_clickable( $phpmailer->Body ) );
    +				// Add template to message
    +				$phpmailer->Body = $this->set_email_template( $phpmailer->Body );
    +				// Replace variables in email
    +				$phpmailer->Body = $this->template_vars_replacement( $phpmailer->Body );
    +			}
     		}
    
     		/**
    Plugin Author hellonico

    @nlemoine

    Hi there,

    Sorry for the late update, I had no time for side projects.

    The plugin has just been updated, containing your fix. Thanks for your contribution.

    N.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Converts html emails if sent after a plain text (fix provided)’ is closed to new replies.
Skip to toolbar