Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Adam Miedema
Adam Miedema

Posted on • Originally published atMedium on

     

Sending emails with Alpas web framework — Part 2

Sending emails with Alpas web framework — Part 2

Photo byMikaela Wiedenhoff onUnsplash

Previously, wecaptured 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 useSparkPost as our email service provider and add the dependency to thebuild.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 namedSparkPostMailDriver.kt.

classSparkPostDriver(privatevalenv:Environment):MailDriver{overridefunsend(mail:MailMessage){valapiKey=env("SPARKPOST_API_KEY")valdefaultFromAddress=env("MAIL_FROM_ADDRESS")valclient=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 yourSparkPost 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 theMailConfig.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 tostart.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! 😍

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Keep moving forward.
  • Joined

More fromAdam Miedema

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp