Forem

Adam Miedema
Adam Miedema

Posted on • Originally published at Medium on

3

Sending emails with Alpas web framework — Part 2

Sending emails with Alpas web framework — Part 2

Photo by Mikaela Wiedenhoff on Unsplash

Previously, we captured an email via a Stripe purchase flow and generated a test email within the Alpas framework.

Now, let’s hook this all up with an email service and deliver a real email! 💌

Add mail service dependency

For this example, we’ll use SparkPost as our email service provider and add the dependency to the build.gradle file.

Add the following line to the list of dependencies:

implementation 'com.sparkpost:sparkpost-lib:0.21'

After adding, refresh Gradle to import SparkPost into the project.

Create a driver

Now that we have SparkPost added to our project, we can now create a mail driver. I added a new folder titled ‘drivers’ under the kotlin directory and then created a new file named SparkPostMailDriver.kt.

class SparkPostDriver(private val env: Environment) : MailDriver {
    override fun send(mail: MailMessage) {
        val apiKey = env("SPARKPOST_API_KEY")
        val defaultFromAddress = env("MAIL_FROM_ADDRESS")

        val client = Client(apiKey)
        client.sendMessage(defaultFromAddress, mail.to, mail.subject, mail.message, mail.message)
    }
}

You can see that the class refers to an API Key and a default ‘from’ email address that are located in the .env file. You will first need to setup your SparkPost account and ensure you correctly set up your domain with them as well as create an API key. Once you do that, you can then add those values to your .env file. Just an fyi — SparkPost will take a little time to fully enable sending an email.

Update the mail config

Let’s now register the new driver in the MailConfig.kt file.

Simply, add the following to the init:

addDriver("sparkpost", lazy {  
  SparkPostDriver(env)
} )

Call the new driver in send the message function

Last step, go to start.kt, where we were previously sending the email via our local, and we will explicitly call the SparkPost driver to send our emails thru.

Simply, add “sparkpost” to the driver as shown below:

call.config<MailConfig>().driver("sparkpost").send(mail)

Send a real email!

Everything is now setup to send a real-life email to a real-life inbox. Run your project, fill in the email and credit card text fields, and then click Pay.

Our payment will be successful and a receipt will be sent to the provided email. ✉️ 🎉


In the next part, we will take a look at queues! 😍

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay