Send bulk emails using Mailgun and serverless Javascript hooks
Email remains the top communication tool for businesses. Either, if you need to create simple direct emails messages, or schedule templated bulk messages to customers - we all need flexible tools to communicate effectively with our customers.
This blog post shows how you can create a serverless background application for sending bulk HTML emails using the popular Mailgun SaaS.

This blog post also demonstrates some useful Codehooks concepts, such as background cron jobs, message queues, NoSql database, key-value store and secret environment variables.
Read on to learn more about effective email processing with Mailgun and Codehooks.
Quick preview: example application usage
Our example application runs in the background and monitors a mailing list (data collection) and sends an email to each contact in the list using a HTML template. New entries to the mailing list are POST'ed via a REST API endpoint or imported via the CLI tool. For scalability and resilience all emails are processed with the Codehooks message queue and serverless function workers.
After deploying the application, we can use the CLI to import a CSV file with mail addresses to the application collection mailinglist.
coho import --filepath ./mailinglist.csv --collection mailinglist
The running background process (cron job) picks up the new entries and sends each contact item to the message queue for processing by a worker function, which integrates with Mailgun to send the actual message.
We can monitor the process in realtime using the coho logs -f command shown below.
coho logs -f
dev 2022-09-27T20:30:00.717Z Email process starting 2 contacts
dev 2022-09-27T20:30:10.039Z [email protected] 1
dev 2022-09-27T20:30:11.133Z Mailgun reciept <[email protected]>
dev 2022-09-27T20:30:11.324Z [email protected] 1
dev 2022-09-27T20:30:12.449Z Mailgun reciept <[email protected]>
Now that we understand the basic use case of this application, let's go ahead and see how it's made, and how you can create and modify it yourself.
Get a Mailgun account
If you haven't already, navigate to the Mailgun website and create a new account, it's free to sign up. Follow the instructions, and notice the domain, API-url and API-key information needed in this article. To integrate the Codehooks serverless function we'll use the Mailgun REST API.
With a Mailgun account ready, we can go forward with creating a new project for our Codehooks application.
