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 1

Sending emails with Alpas web framework — Part 1

Photo byDaniel Bradley onUnsplash

Previously, I showed you an example of integratingStripe payments with Alpas and providing the Kotlin equivalent to some other examples provided by Stripe. Then, in a follow-up post, I showedhow concise Alpas is as a framework 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 theAlpas 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 thewelcome.peb file, all I need to do is add an input to the form.

<inputid="email"placeholder="your@email.address">

Then, in theclient.js file, we’ll append some code to the payment successful statement.

else{// The payment succeeded!varemail=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 thestart.kt file, I’ll add the following route to receive the user’s email and then call a function to send an email.

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

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

funsend(call:HttpCall){valmail=MailMessage().apply{to=call.bodysubject="Order Confirmed"message="Here is your order receipt!"}call.config<MailConfig>().driver().send(mail)}

Test our progress

Alpas comes packaged withemail features 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:

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

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


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.


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