How to Include an RSS Feed in an Email Message using AMPscript

This document contains conceptual, procedural and scenario information about including RSS feeds in the content of an email message. You can use AMPscript to pull content from an RSS feed and put that content into an email message.

Prerequisites

You must be familiar with the email creation process and AMPscript before attempting to use the information in this document. Your RSS feed must also be operational and published to the Internet before you can use it in an email.

Why Include an RSS Feed in an Email Message

RSS presents an XML-based list that showcase the latest articles published on a website or other RSS-enabled content source. You can use AMPscript to take the information from an RSS feed during the send to make sure you include the most current information possible in your email send.

Scenario

Northern Trail Outfitters maintains a blog for several key employees to use in sharing their experiences with Northern Trail equipment and the outdoors. They also send out a weekly email newsletter, and they’d like to include links to the latest blog entries as part of the newsletter. They use AMPscript to pull the RSS feeds into the email at the time of send.

Procedures

Follow the procedures below to include an RSS feed in your email sends.

How to Include an RSS Feed in an Email Message:

  1. Create a content area that uses the HTML Only orientation in your account.
  2. Include the following content syndication link in that content area:
    %%before; httpget; 1 "INSERT URL HERE"%%
  3. Note the name of the content area containing the content syndication link. In this case, the content area is named RSSParse, and you can find it in my contents\RSSParse\RSSParse. This is a separate content area from the content area you will include in the email message – the AMPscript pulls the content from the RSSParse content area and puts it in the content area contained in the email message.
  4. In the email message’s content area, include the AMPscript shown below:
    %%[
     VAR @xml, @titles, @title, @descs, @desc, @imgs, @img, @urls, @url, @links, @link,   @cnt, @readmore ,@titlelink
                   
    /* This line specifies the content area from which the RSS content will be pulled for the email message. */
    SET @xml = ContentAreaByName("my contents\RSSFeed\RSSParse")
    SET @titles = BuildRowsetFromXML(@xml,"//item/title",1)
    SET @descs = BuildRowsetFromXML(@xml,"//item/description",1)
    SET @imgs = BuildRowsetFromXML(@xml,"//item/image",1)
    SET @urls = BuildRowsetFromXML(@xml,"//item/image/url",1)
    SET @links = BuildRowsetFromXML(@xml,"//item/link",1)

    /* This specifies the number of articles to retrieve */
    If RowCount(@titles) > 5 THEN
       SET @rows = 5
    ELSE
       SET @rows = RowCount(@titles)
    ENDIF

    /* Article loop begins */                        
    IF @rows >= 1 THEN for @cnt = 1 to @rows DO
       SET @title = Field(Row(@titles,@cnt),"Value")
       SET @desc = Field(Row(@descs,@cnt), "Value")
       SET @img = Field(Row(@imgs,@cnt), "Value")
       SET @url = Field(Row(@urls,@cnt), "Value")
       SET @link = Field(Row(@links,@cnt), "Value")
       SET @readmore = Concat(@title,"#readMore")    
       SET @titlelink = Concat(@title,"#titleLink")
    ]%%

    <table width="75%" class="full-width-mob" border="0" align="left" cellpadding="0" cellspacing="0">
       /* Begin article rows */
       <tr>
          <td>&nbsp;</td>
          <td style="font-family:Arial, Helvetica, sans-serif; color:#333333; font-size:18px; text-align:left; text-decoration:underline;"><a href="%%=RedirectTo(@link)=%%" alias="%%=v(@titlelink)=%%" title="%%=v(@title)=%%" style="color: #333333; text-decoration: underline;"><span style="color: #333333;">%%=v(@title)=%%</span></a>
          </td>
       </tr>
       <tr>
          <td height="5"></td>
          <td></td>
       </tr>
       <tr>
          <td width="11"></td>
          <td style="font-family:Arial, Helvetica, sans-serif; color:#666666; font-size:12px; text-align:left;">%%=v(@desc)=%%</td>
       </tr>
       <tr>
          <td height="5"></td>
          <td></td>
       </tr>
       <tr>
          <td></td>
          <td style="font-family:Arial, Helvetica, sans-serif; color:#333333; font-size:12px; text-align:left;"><a href="%%=RedirectTo(@link)=%%" alias="%%=v(@readmore)=%%" title="%%=v(@title)=%%" style="text-decoration:none; color:#333333;">Read More <span style="color:#981e32;">&#8250;&#8250;</span></a></td>
       </tr>
       /* End article rows */
    </table>

    %%[
    NEXT @cnt  
    ENDIF
    ]%%
    /* Article loop ends */

    Test the email before sending to make sure the message pulls the correct content.

    In the AMPscript listed above, the AMPscript takes the most recent 5 entries from the feed and includes them in the email. The @xml variable pulls in the content area with the content syndication, while the other variables pull information from the feed to include in the email. The FOR loop in the email counts how many times articles have been pulled from the feed and stops the message at 5.

    You can modify the HTML markup included in the example to better fit the formatting of your email message.
    NOTE: If the RSS Feed pulled into the email contains HTML formatting, be sure to clear the text version of any AMPscript and provide the VAWP URL as a mechanism to view the email. If you do not do this, the email send will fail.

    If you reference a node that includes a colon, the parser will not accept that colon and cause the operation to error out. Use the sample code below as an example to get around the colon and successfully add your RSS feed:
    [cc lang="html"]
    %%[
    Var @xml, @xml1, @titles, @title, @descs, @desc, @links, @link, @cnt
    Set @xml1 = ContentAreaByName("my contents\RSSParse\RSSParse") //This line specifies the content area from which the RSS content will be pulled for the email message.
    Set @xml = Replace(@xml1,'content:encoded','contentencoded')
    Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
    Set @descs = BuildRowsetFromXML(@xml,"//item/contentencoded",1)
    Set @links = BuildRowsetFromXML(@xml,"//item/link",1)

    If RowCount(@titles) > 5 THEN
       SET @rows = 5
    ELSE
       SET @rows = RowCount(@titles)
    ENDIF

    IF @rows >= 1 THEN
    for @cnt = 1 to 5 do
       Set @title = Field(Row(@titles,@cnt),"Value")
       Set @desc = Field(Row(@descs,@cnt), "Value")
       Set @link = Field(Row(@links,@cnt), "Value")
    ]%%

    <div style="border: 1px solid #444; background-color: #F7F7F7; margin: 0.76em 0; padding: 0.76em;">
    <h1 style="font: bold normal 1.0em Arial, Helvetica, sans-serif;"><a href="%%=RedirectTo(@link)=%%" alias="%%=v(@title)="%%" title="%%=v(@title)=%% style="color: #000;">%%=v(@title)=%%</a></h1>
    <span style="font: normal normal 0.76em Arial, Helvetica, sans-serif; color: #444;">%%=v(@desc)=%%</span>
    </div>

    %%[
    NEXT @cnt
    ENDIF
    ]%%

    Sample RSS Feed

    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
    <channel>
      <title>Newsletter | Website Blog</title>
      <link>http://blog.website.com/</link>
      <description>The latest blog posts from Website.com.</description>
      <language>en-us</language>
      <pubDate>Wed, 04 Sep 2013 14:21:57 +0000</pubDate>
      <lastBuildDate>Wed, 04 Sep 2013 14:21:57 +0000</lastBuildDate>
      <managingEditor>info@website.com</managingEditor>
      <item>
        <title>Tips to Help Prevent Identity Theft</title>
        <link>http://blog.website.com/credit/tips-to-help-prevent-child-identity-theft-at-back-to-school-time/</link>
        <description><![CDATA[<p>Back-to-school time means sharing your child’s information with school administrators, doctors and daycare workers. Learn how you can help protect your child’s personal information and help guard against the threat of child identity theft.</p>
    ]]></description>
        <image>
            <height>100</height>
            <width>100</width>
            <url>http://blog.website.com/wp-content/uploads/2013/08/shutterstock_78854017-150x150.jpg</url>
            <title>Tips to Help Prevent Child Identity Theft at Back-to-School Time</title>
            <link>http://blog.website.com/</link>
        </image>
        <guid>http://blog.website.com/credit/tips-to-help-prevent-child-identity-theft-at-back-to-school-time/</guid>
      </item>
      <item>
        <title>Credit Dating: Tips for Snagging Your Soul Mate</title>
        <link>http://blog.website.com/credit/credit-score-dating-tips-for-snagging-your-financial-soul-mate/</link>
        <description><![CDATA[<p>Lenders use your credit score to calculate your risk as a borrower. But potential suitors may use your credit score to calculate their risks, too. Learn how you can give your credit file a makeover and make it more appealing to potential mates.</p>
    ]]></description>
        <image>
            <height>100</height>
            <width>100</width>
            <url>http://blog.website.com/wp-content/uploads/2013/07/Credit-Score-Dating-Tips-for-Snagging-Your-Financial-Soul-Mate-150x150.jpg</url>
            <title>Credit Score Dating: Tips for Snagging Your Financial Soul Mate</title>
            <link>http://blog.website.com/</link>
        </image>
        <guid>http://blog.website.com/credit/credit-score-dating-tips-for-snagging-your-financial-soul-mate/</guid>
      </item>
      <item>
        <title>Estate Planning Tips for Children</title>
        <link>http://blog.website.com/family-money/nine-estate-planning-tips-for-children-with-special-needs/</link>
        <description><![CDATA[<p>Estate and financial planning can be daunting, especially if you’re the parent of a child with special needs. These nine steps can help you navigate the process and ensure your child is taken care of even after you’re gone.</p>
    ]]></description>
        <image>
            <height>100</height>
            <width>100</width>
            <url>http://blog.website.com/wp-content/uploads/2013/07/nine-estate-planning-tips-for-children-with-special-needs-150x150.jpg</url>
            <title>Nine Estate Planning Tips for Children With Special Needs</title>
            <link>http://blog.website.com/</link>
        </image>
        <guid>http://blog.website.com/family-money/nine-estate-planning-tips-for-children-with-special-needs/</guid>
      </item>
      <item>
        <title>How Much Insurance Do I Need? Tips on Estimating Coverage</title>
        <link>http://blog.website.com/retirement/how-much-life-insurance-do-i-need-tips-on-estimating-coverage/</link>
        <description><![CDATA[<p>Life insurance can help cover your final expenses, easing your family’s burden during a difficult time. Before you purchase coverage, there are four factors you may want to consider that can help you decide how much life insurance is right for you.</p>
    ]]></description>
        <image>
            <height>100</height>
            <width>100</width>
            <url>http://blog.website.com/wp-content/uploads/2013/07/shutterstock_72617296-150x150.jpg</url>
            <title>How Much Life Insurance Do I Need? Tips on Estimating Coverage</title>
            <link>http://blog.website.com/</link>
        </image>
        <guid>http://blog.website.com/retirement/how-much-life-insurance-do-i-need-tips-on-estimating-coverage/</guid>
    </item>
    <item>
        <title>Buying a Home? 5 Things Every First-Time Homebuyer Must Do</title>
        <link>http://blog.website.com/real-estate/buying-a-home-5-things-every-first-time-homebuyer-must-do/</link>
        <description><![CDATA[<p>Buying a home is exciting, but it can also feel overwhelming. Before you take the plunge into the real estate market, learn what you can do to prepare yourself and your finances for one of the biggest purchases you’ll ever make.</p>
    ]]></description>
        <image>
            <height>100</height>
            <width>100</width>
            <url>http://blog.website.com/wp-content/uploads/2013/07/shutterstock_111817019-150x150.jpg</url>
            <title>Buying a Home? 5 Things Every First-Time Homebuyer Must Do</title>
            <link>http://blog.website.com/</link>
        </image>
        <guid>http://blog.website.com/real-estate/buying-a-home-5-things-every-first-time- homebuyer-must-do/</guid>
     </item>
     <item>
        <title>Four Questions Before Purchasing a Travel Insurance Policy</title>
        <link>http://blog.website.com/insurance/four-questions-before-purchasing-a-travel-insurance-policy/</link>
        <description><![CDATA[<p>Nothing ruins a vacation more than a delayed flight, lost luggage or a medical emergency. If you’re considering purchasing a travel insurance policy to protect your non-refundable expenses in the event something goes wrong, be sure to ask some questions first.</p>
    ]]></description>
        <image>
            <height>100</height>
            <width>100</width>
            <url>http://blog.website.com/wp-content/uploads/2013/04/four-questions-before-purchasing-a-travel-insurance-policy-150x150.jpg</url>
            <title>Four Questions Before Purchasing a Travel Insurance Policy</title>
            <link>http://blog.website.com/</link>
        </image>
        <guid>http://blog.website.com/insurance/four-questions-before-purchasing-a-travel-insurance-policy/</guid>
      </item>
    </channel>
    </rss>