Custom AND automated blog post emails using Wordpress

It’s easy to send an automated email every time you publish a new post by connecting your email marketing system to your blog’s RSS feed.

But the emails are boring and generic.

Writing special emails for each article is way more effective.

But involves heading into your email marketing system each time and writing & scheduling a new campaign.

Why not combine the best of both worlds?

In this solution, we’ll write the custom email inside Wordpress and your email marketing system will pick this up automatically when it sends the email. No generic post notifications! No messing around creating new campaigns all the time!

Skills required:

  • Installing and configuring Wordpress plugins.
  • Creating new files in your Wordpress template.
  • Copying & pasting PHP.

1. Install the Wordpress Plugin “Advanced Custom Fields”

2. Create a new field to contain your custom email text

  1. “Add New” Field Group 
    Name it whatever you like. I call mine “Email updates”.
  2. “+ Add Field”
    Field Label: Email subject
    Field Name: emailrss_subject
    Field Type: Text
    Field Instructions: Leave yourself a note if you want a reminder of how to write a great email subject line!
    (Leave the rest of the options blank or as the default.)
  3. “Close Field”
  4. “+ Add Field”
    Field Label: Post email
    Field Name: emailrss_description
    Field Type: Wysiwyg Editor
    Field Instructions: Leave yourself a note if you want a reminder of how to write a great email. (I personally remind myself to include the LINK to the article! This is pretty important.)
    Toolbar: Basic
    Show Media Upload Buttons: No
    (Leave the rest of the options blank or as the default.)
  5. “Close Field”
  6. Under “Options”
    Position: Normal (after content)
    Style: Standard (WP metabox)
  7. Publish your new fields

So this is all well and good. You can now add write a custom subject and formatted email for any new post, using this fields. (They’ll show up under your post content when you’re in Edit mode.)

But they will not show up in your default Wordpress RSS feed. We have to create a special, new feed.

3. Create a custom RSS feed showing your new fields

For the next step, you’ll need to edit PHP files in your Wordpress template. Take care and follow the instructions exactly.

a) Create a new file inside your Wordpress template folder called rss-email.php

Copy the following code into the blank file:

<?php
/**
* Template Name: Custom RSS Template — Email
*/
$postCount = 1; // Only show the last post
$posts = query_posts(‘showposts=’ . $postCount);
header(‘Content-Type: ‘.feed_content_type(‘rss-http’).’; charset=’.get_option(‘blog_charset’), true);
echo ‘<?xml version=”1.0" encoding=”’.get_option(‘blog_charset’).’”?’.’>’;
?>
<rss version=”2.0"
xmlns:content=”http://purl.org/rss/1.0/modules/content/"
xmlns:wfw=”http://wellformedweb.org/CommentAPI/"
xmlns:dc=”http://purl.org/dc/elements/1.1/"
xmlns:atom=”http://www.w3.org/2005/Atom"
xmlns:sy=”http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash=”http://purl.org/rss/1.0/modules/slash/"
<?php do_action(‘rss2_ns’); ?>>
<channel>
<title><?php bloginfo_rss(‘name’); ?></title>
<atom:link href=”<?php self_link(); ?>” rel=”self” type=”application/rss+xml” />
<link><?php bloginfo_rss(‘url’); ?></link>
<description><?php bloginfo_rss(‘description’); ?></description>
<lastBuildDate><?php echo mysql2date(‘D, d M Y H:i:s +0000’, get_lastpostmodified(‘GMT’), false); ?></lastBuildDate>
<language>en-AU</language>
<sy:updatePeriod><?php echo apply_filters( ‘rss_update_period’, ‘hourly’ ); ?></sy:updatePeriod>
<sy:updateFrequency><?php echo apply_filters( ‘rss_update_frequency’, ‘1’ ); ?></sy:updateFrequency>
<?php do_action(‘rss2_head’); ?>
<?php while(have_posts()) : the_post(); ?>
<item>
<title><?php $rsstitle = get_field(‘emailrss_subject’);

if( !empty($rsstitle) ) {
echo $rsstitle;
} else {
the_excerpt_rss();
} ?></title>
<link><?php the_permalink_rss(); ?></link>
<pubDate><?php echo mysql2date(‘D, d M Y H:i:s +0000’, get_post_time(‘Y-m-d H:i:s’, true), false); ?></pubDate>
<dc:creator><?php the_author(); ?></dc:creator>
<guid isPermaLink=”false”><?php the_guid(); ?></guid>
<description><![CDATA[<?php $post_id = get_the_ID();

$rssdesc = get_field(‘emailrss_description’);

if( !empty($rssdesc) ) {
echo $rssdesc;
} else {
the_excerpt_rss();
} ?>]]></description>
<content:encoded><![CDATA[<?php the_content_rss(); ?>]]></content:encoded>
<?php rss_enclosure(); ?>
<?php do_action(‘rss2_item’); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>

NOTE:
This code relies on you using the exact same “Field Names” for the new fields that I listed above. If you changed them at all, you’ll need to find the names in the code and update them with the names you chose.

b) Add the following code to the bottom of your theme’s functions.php file.

/**
* Customise RSS feed for Mailchimp
*
*/
add_action('init', 'customEmailRSS');
function customEmailRSS(){
add_feed('email', 'customEmailRSSFunc');
}
function customEmailRSSFunc(){
get_template_part('rss', 'email');
}

c) Test out your new custom RSS feed.

  1. Fill out your new fields for your most recent post. (It’s ok if you’ve already sent a email about it. This is just for testing.)
  2. Visit your original, default RSS feed:
    [yourblogdomain.com]/feed/
    Look for the tags labelled <title> and <description>. The text in these tags should NOT be the text you put in your new fields. (We’re just checking this feed first, to make sure that nothing’s changed that shouldn’t have.)
  3. Visit your new, custom RSS feed:
    [yourblogdomain.com]/feed/email
    Look for the tags labelled <title> and <description>. The text in these tags SHOULD BE the text you put in your new fields.

4. Set up your automated email campaign

  1. In your email marketing system, create an automated email campaign based off your new RSS feed.

In Mailchimp, that means making a new (or updating your existing) “RSS Feed” campaign using your NEW rss feed URL:
[yourblogdomain.com]/feed/email

eg. My actual feed URL is https://www.artisananalytics.com.au/feed/email

2. Set your email subject to be the “Title” of your RSS feed item. This will output your custom “Email subject” field.

In Mailchimp:

*|RSSITEM:TITLE|*

3. Set the main contents of the email to be the description of your RSS feed item. This will be your custom “Post email” text, including all the formatting, links etc.

In Mailchimp:

*|RSSITEMS:|* *|RSSITEM:CONTENT|**|END:RSSITEMS|*

I don’t include any other text! Just this RSS field, then my standard email footer.

TIP: Make sure you use the “Preview” option to check that your title (subject) and post email text is displayed correctly. The preview will use your most recent post, so make sure those fields are filled in.

BONUS TIP: You can use Mailchimp “merge tags” within your new custom fields in Wordpress. eg. Write “Hi *|FNAME|*” in Wordpress and your recipient’s first name will be displayed in the email. You can check this in the Preview by turning on “Live Merge Tags”.

Once your RSS campaign is saved, you are done. From now on, write your custom email directly into Wordpress and let your email get sent automatically!