Create an Email Newsletter Using Mule
This article shows how to create an email newsletter using Mule as shown below. Read on to find out more!
Join the DZone community and get the full member experience.
Join For FreeThis article shows how to create an email newsletter using Mule as shown below.
Here, I have used NewsAPI, you can use any API of your choosing. Almost all APIs need clients to authenticate themselves before serving the request. So, the first step is to register for a dev account on the portal and get an API key.
Creating a Mule Application
- Create a new Mule project. Configure a scheduler source that will trigger the application every day at 8 AM.
- Configure an HTTP requester to call the API and fetch news metadata. You can head to the documentation of the API being used and get the hostname, base URL, etc.
The API Key is passed in header X-API-Key. Also have passed the query parameter to fetch news only for India.
xxxxxxxxxx
<http:headers>
<![CDATA[#[
output application/java --- { "X-Api-Key" : "${apiKey}" }]]]>
</http:headers>
<http:query-params >
<![CDATA[#[
output application/java --- { country: "in" }]]]>
</http:query-params>
- Once the metadata is fetched, we will format it using Dataweave.
xxxxxxxxxx
%dw 2.0
output application/xml writeDeclaration = false
---
section: {
h2: "Here's your news feed generated by Mule",
(payload.articles filter ($.publishedAt > (now() - |P1D|) as String {format: "yyyy-MM-dd'T'hh:mm:ss"}) map (article, articleIndex) -> {
div @(style: " border: 1px solid #000; padding: 5px 10px;margin: 10px;height:100px;"): {
(img @(src: article.urlToImage, style: "height:100px; width: 100px;margin-right: 5px;float: left;"): "") if(article.urlToImage != null),
a @(href: article.url, style: "text-decoration: none;color: #002133;"):
h2: article.title, (p: article.description) if (article.description != null)
}
})
}
Since the API doesn't support fetching records published in a certain time period, I have filtered the payload to include only articles published in the last 24 hours. Also, have applied some basic styling to make the content readable.
Configure an SMTP Send operation to send mail. Create a global Email configuration Open the Advanced tab, select Edit inline in the drop-down and add mail.smtp.starttls.enable property with true value.
Note: If using Gmail, don't forget to turn on Less Secure Apps. Also, change Content-type to text/HTML to send HTML enriched content
That's it. Deploy the application and it will send a mail every day at 8 AM to the configured email with news articles from the last 24 hours.
To test the application locally, the scheduler can be replaced with an HTTP listener.
Published at DZone with permission of Abhay Yadav. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Essential Architecture Framework: In the World of Overengineering, Being Essential Is the Answer
-
How To Integrate Microsoft Team With Cypress Cloud
-
Web Development Checklist
-
Top 10 Pillars of Zero Trust Networks
Comments