WordPress.org

Ready to get started?Download WordPress

Forums

wpMandrill
Not sending HTML (4 posts)

  1. ucfknight04
    Member
    Posted 1 year ago #

    Mandrill is not sending out the html emails. Only the plain text versions. Any ideas? I have checked and unchecked all the sending options in mandrillapp.com and still no luck. When I deactivate Mandrill the emails are then sent as HTML, so I am pretty sure it is the Mandrill app.

    http://wordpress.org/extend/plugins/wpmandrill/

  2. MC_Will
    Member
    Plugin Author

    Posted 1 year ago #

    Can you please paste the full headers of one of the emails you're getting?

  3. ucfknight04
    Member
    Posted 1 year ago #

    i think i figured it out.

  4. strangerstudios
    Member
    Posted 1 year ago #

    Just following up here because I ran into the same issue.

    The problem was that my code for making the WP emails HTML in the first place was not being run because it hooked into PHPMailer, which isn't being used anymore when wpMandrill is sending through the API.

    To illustrate, here is the code I used to make emails HTML and add a header and footer (popular HTML email plugins probably work in a similar way). This is the code that doesn't work with wpMandrill.

    /*
    Add header/footer to emails. Works with standard wp_mail.
    */
    function my_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 = preg_replace('#<(http://[^*]+)>#', '$1', $phpmailer->Body);
    	// Convert line breaks & make links clickable
    	$phpmailer->Body = wpautop ( make_clickable ($phpmailer->Body) );
    
    	// Add template to message
    	if(file_exists(TEMPLATEPATH . "/email_header.html"))
    	{
    		$phpmailer->Body = file_get_contents(TEMPLATEPATH . "/email_header.html") . "\n" . $phpmailer->Body;
    	}
    	if(file_exists(TEMPLATEPATH . "/email_footer.html"))
    	{
    		$phpmailer->Body = $phpmailer->Body . "\n" . file_get_contents(TEMPLATEPATH . "/email_footer.html");
    	}
    
    	// Replace variables in email
    	global $current_user;
    	$data = array(
    				"name" => $current_user->display_name,
    				"sitename" => get_option("blogname"),
    				"display_name" => $current_user->display_name,
    				"user_email" => $current_user->user_email,
    				"subject" => $phpmailer->Subject
    			);
    	foreach($data as $key => $value)
    	{
    		$phpmailer->Body = str_replace("!!" . $key . "!!", $value, $phpmailer->Body);
    	}
    
    	do_action("my_after_phpmailer_init", $phpmailer);
    }
    function my_wp_mail_content_type( $content_type ) {	
    
    	//check for template
    	if(file_exists(TEMPLATEPATH . "/email_header.html") || file_exists(TEMPLATEPATH . "/email_footer.html"))
    	{
    		add_action('phpmailer_init', 'my_send_html', 1);
    
    		//change to html if not already
    		if( $content_type == 'text/plain')
    		{
    			$content_type = 'text/html';
    		}
    	}
    	return $content_type;
    }
    add_filter('wp_mail_content_type', 'my_wp_mail_content_type', 1);

    And here is the code I wrote to add HTML wrappers (selectively) to some emails being sent by wpMandrill. This is the code that does work with wpMandrill.

    /*
    	Add html header footer to emails sent by wpMandrill using the mandrill_payload filter
    */
    function my_email_header_footer($message)
    {
    	//only wrap emails from info@mydomain.com and orders@mydomain.com
    	//(our invite form emails should not look official)
    	if(in_array($message['from_email'], array("info@mydomain.com", "orders@mydomain.com")))
    	{
    		// Add template to message
    		if(file_exists(TEMPLATEPATH . "/email_header.html"))
    		{
    			$message['html'] = file_get_contents(TEMPLATEPATH . "/email_header.html") . "\n" . $message['html'];
    		}
    		if(file_exists(TEMPLATEPATH . "/email_footer.html"))
    		{
    			$message['html'] = $message['html'] . "\n" . file_get_contents(TEMPLATEPATH . "/email_footer.html");
    		}
    
    		// Replace variables in email
    		global $current_user;
    		$data = array(
    					"name" => $current_user->display_name,
    					"sitename" => get_option("blogname"),
    					"display_name" => $current_user->display_name,
    					"user_email" => $current_user->user_email,
    					"subject" => $phpmailer->Subject
    				);
    		foreach($data as $key => $value)
    		{
    			$message['html'] = str_replace("!!" . $key . "!!", $value, $message['html']);
    		}
    	}
    
    	return $message;
    }
    add_filter("mandrill_payload", "my_email_header_footer");

    Hope this helps some people. I left some of the other things I'm doing like swapping template tags in the header/footer templates and checking the sender email in case people are interested in that.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags