Sending emails with Alpas web framework — Part 1
Tags: kotlin, alpas, framework, webdev
Published: 2020-05-14 19:58:39

---
title: Sending emails with Alpas web framework — Part 1
published: true
date: 2020-05-14 19:55:29 UTC
tags: kotlin,alpas,framework,web-development
canonical_url: 
---

### Sending emails with Alpas web framework — Part 1

![](https://cdn-images-1.medium.com/max/1024/0*bmccYCu4r6RoNPz7)<figcaption>Photo by <a href="https://unsplash.com/@_danbrad?utm_source=medium&amp;utm_medium=referral">Daniel Bradley</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Previously, I showed you an example of integrating [Stripe payments with Alpas](https://dev.to/armiedema/accept-a-stripe-payment-example-with-alpas-11ej) and providing the Kotlin equivalent to some other examples provided by Stripe. Then, in a follow-up post, I showed [how concise Alpas is as a framework](https://dev.to/armiedema/the-concise-web-framework-11df) by reducing that example even further.

Now, I am going to work off of that latest example and show how you can capture an email address and send an order confirmation email. This will be a multi-post series and, in this post, I’ll lay down the foundation of capturing an email and making it available to the [Alpas](https://alpas.dev) back-end.

### Get the front-end ready

To get started, I’ll make some updates to the front-end to expose an input field for the user to enter their email as well as update the javascript to send the email to the back-end when Stripe confirms a successfully processed payment.

In the [welcome.peb](https://github.com/armgitaar/stripe-example/blob/singlepage/src/main/resources/templates/welcome.peb) file, all I need to do is add an input to the form.

```html
  <input id="email" placeholder="your@email.address">
```

Then, in the [client.js](https://github.com/armgitaar/stripe-example/blob/singlepage/src/main/resources/web/js/client.js) file, we’ll append some code to the payment successful statement.

```javascript
else {
    // The payment succeeded!
    var email = document.getElementById("email").value;
    orderComplete(result.paymentIntent.id);
    fetch("/payment-success", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            'X-CSRF-TOKEN': document.head.querySelector(" [name~=csrf-token][content]").content
        },
        body: email
    });
}
```

The above will take the value entered into the email input field and then post it to the back-end given a successful payment has been verified by Stripe.

### Get the back-end ready

Now that the front-end captures and sends the email for a successful order to the back-end, let’s use this email to send an email to the user confirming their order.

In the [start.kt](https://github.com/armgitaar/stripe-example/blob/singlepage/src/main/kotlin/start.kt) file, I’ll add the following route to receive the user’s email and then call a function to send an email.

```kotlin
post("payment-success") {  
  send(this)
}
```

Now, I’ll add the **send** function that will compile the email contents and call an action to send the email.

```kotlin
fun send(call: HttpCall) {
    val mail = MailMessage().apply {  
        to = call.body
        subject = "Order Confirmed"
        message = "Here is your order receipt!"
}  
call.config<MailConfig>().driver().send(mail)
}
```

### Test our progress

Alpas comes packaged with [email features](https://alpas.dev/docs/mail) that make crafting and sending emails super easy and, at this juncture, we can already do a pretty solid test.

Running the project on my local renders this:

![](https://cdn-images-1.medium.com/max/458/1*KyTyQm-lSxODJGLKfZN9pA.png)

Now, adding an email address, a Stripe test credit card, and clicking ‘Pay’ shows:

![](https://cdn-images-1.medium.com/max/466/1*xLHieEqtJRMZG9-IZuLG9A.png)

Alpas holds onto a preview of the email if there is nowhere for the email to go. In the project, navigating to **storage > mails** shows records of created emails. Here is the email that was generated from the actions above.

![](https://cdn-images-1.medium.com/max/1024/1*fKIHhhmSL1LshB6jtbX7vw.png)

* * *

Just a couple lines of code got us pretty far with generating an order confirmation email with the Alpas framework. In the next part, we will look at how we can hook this all up to an email service to send a real-life email.

* * *