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 Free
This 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.
- Once the metadata is fetched, we will format it using Dataweave.
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.