Include Content from an RSS Feed in an Email Message

RSS feeds are XML-based lists of the latest articles published on a website or other content source. You can use AMPscript to pull content from an RSS feed and add that content to an email message. An advantage of this approach is that AMPscript retrieves content from the RSS feed at send time, ensuring that the content you add to the email is as current as possible.

Basic Code Example 

In this example, the AMPscript takes the five most recent 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 after adding five articles to the message.

In the body of the email message, include this code example.

1%%[
2  Var @xml, @titles, @title, @descs, @desc, @links, @link, @cnt
3  Set @xml = HTTPGet("https://example.com/xmlfile.xml")
4  Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
5  Set @descs = BuildRowsetFromXML(@xml,"//item/description",1)
6  Set @links = BuildRowsetFromXML(@xml,"//item/link",1)
7
8  If RowCount(@titles) > 5 then
9    Set @rows = 5
10  Else
11    Set @rows = RowCount(@titles)
12  EndIf
13
14  If @rows >= 1 then
15    For @cnt = 1 to @rows do
16      Set @title = Field(Row(@titles,@cnt),"Value")
17      Set @desc = Field(Row(@descs,@cnt), "Value")
18      Set @link = Field(Row(@links,@cnt), "Value")
19]%%
20
21<div style="border: 1px solid #444; background-color: #F7F7F7; margin: 0.76em 0; padding: 0.76em;">
22  <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>
23  <span style="font: normal normal 0.76em Arial, Helvetica, sans-serif; color: #444;">%%=v(@desc)=%%</span>
24</div>
25
26%%[
27    Next @cnt
28  EndIf
29]%%

You can modify the HTML in the example to fit the formatting of your email message. To make sure that the message pulls the correct content and that the content is formatted correctly, always send emails before you send to your customers.

If the RSS feed contains HTML formatting, remove any AMPscript from the text version of the email, and provide the VAWP URL. If you don’t make these changes, the email fails.

Code Example that Removes Colons from RSS Data 

If you reference a node that includes a colon, the AMPscript function returns an error. This code example uses the Replace() function to remove the colon so that you can successfully add content from your RSS feed to your emails.

1%%[
2  Var @xml, @xml1, @titles, @title, @descs, @desc, @links, @link, @cnt
3  Set @xml1 = HTTPGet("https://example.com/xmlfile.xml")
4  Set @xml = Replace(@xml1,'content:encoded','contentencoded')
5  Set @titles = BuildRowsetFromXML(@xml,"//item/title",1)
6  Set @descs = BuildRowsetFromXML(@xml,"//item/contentencoded",1)
7  Set @links = BuildRowsetFromXML(@xml,"//item/link",1)
8
9  If RowCount(@titles) > 5 then
10    Set @rows = 5
11  Else
12    Set @rows = RowCount(@titles)
13  EndIf
14
15  If @rows >= 1 then
16    For @cnt = 1 to 5 do
17      Set @title = Field(Row(@titles,@cnt),"Value")
18      Set @desc = Field(Row(@descs,@cnt), "Value")
19      Set @link = Field(Row(@links,@cnt), "Value")
20]%%
21
22<div style="border: 1px solid #444; background-color: #F7F7F7; margin: 0.76em 0; padding: 0.76em;">
23  <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>
24  <span style="font: normal normal 0.76em Arial, Helvetica, sans-serif; color: #444;">%%=v(@desc)=%%</span>
25</div>
26
27%%[
28     Next @cnt
29  EndIf
30]%%

See Also