Sending Transactional Emails With Sendinblue in Kotlin
Tags: kotlin, marketing, mobile, programming
Published: 2024-01-19 20:51:09

In this tutorial, we are going to explain how to send transactional emails with Sendinblue.

> _"Sendinblue is a comprehensive suite of SaaS communication tools, including email marketing, transactional emails, text messages, and [more](https://www.sendinblue.com/features/)."_ — [_Sendinblue's official documentation_](https://developers.sendinblue.com/docs)

Sendinblue's [transactional email documentation](https://developers.sendinblue.com/docs/send-a-transactional-email) recommends the use of their [API clients](https://developers.sendinblue.com/docs/api-clients), which are available and documented in: [C#](https://github.com/sendinblue/APIv3-csharp-library), [Go](https://github.com/sendinblue/APIv3-go-library), [Java](https://github.com/sendinblue/APIv3-java-library), [Node JS](https://github.com/sendinblue/APIv3-nodejs-library), [PHP](https://github.com/sendinblue/APIv3-php-library), [Python](https://github.com/sendinblue/APIv3-python-library), and [Ruby](https://github.com/sendinblue/APIv3-ruby-library). What about [Kotlin](https://kotlinlang.org/)?

First of all, we need to understand what a transactional email is.

> _"A transactional email is an email that is automatically sent by your website to a single recipient resulting from a transaction or a specific event performed by that person, such as an eCommerce purchase or a password reset request."_ — [The Complete Marketer's Guide to Transactional Emails](https://www.sendinblue.com/blog/transactional-emails-guide/)

Now we are ready to dive into the tutorial.

## Creating a New Sendinblue Account

Before anything else, we need an account with Sendinblue. Several [plans](https://www.sendinblue.com/pricing/) are available, but we will use the free one, which comes with some limitations, as described in [Is there a limit to the free plan?](https://help.sendinblue.com/hc/en-us/articles/208580669-Is-there-a-limit-to-the-free-plan-)

You can create a new account on [this](https://app.sendinblue.com/account/register) page.

When you've done that, it's time to get our Sendinblue [API key](https://help.sendinblue.com/hc/en-us/articles/209467485-What-s-an-API-key-and-how-can-I-get-mine-), which is necessary to start calling Sendinblue API. You can retrieve your API key as described in the documentation under [Get your API key](https://developers.sendinblue.com/docs/getting-started#quick-start).

Now, we can start writing some lines of code.

## Installing the Sendinblue API Client

In order to send transactional emails in Kotlin, we need to install the [Sendinblue API client](https://github.com/sendinblue/APIv3-java-library).

If you are a Gradle user, add this dependency to your project's build file:

```groovy
compile "com.sendinblue:sib-api-v3-sdk:4.1.1"
```

Otherwise, if you are a Maven user, add the following dependency to your project's build POM:

```xml
<dependency>
    <groupId>com.sendinblue</groupId>
    <artifactId>sib-api-v3-sdk</artifactId>
    <version>4.1.1</version>
    <scope>compile</scope>
</dependency>
```

The Sendinblue API client is now installed and ready to be used.

## Defining a New Template Including Dynamic Content

Sendinblue allows users to define their own [custom templates](https://help.sendinblue.com/hc/en-us/articles/360000946299-Create-customize-transactional-email-templates) for designing emails. Templates support dynamic content, which can be filled with [contact attributes](https://help.sendinblue.com/hc/en-us/articles/360000268730#attributes) (by default, a contact comes with the following attributes: `EMAIL`, `FIRSTNAME`, `LASTNAME`, `SMS`), [predefined variables](https://help.sendinblue.com/hc/en-us/articles/360000268730#predefined), and custom-defined [transactional parameters](https://help.sendinblue.com/hc/en-us/articles/360000268730#transactional).

The goal of this tutorial is not to show how to define a template in Sendinblue. So, to complete this step, following the official guide is highly recommended.

**Please note**: Do not forget to save the ID of the template you just created since we are going to use it in the next step.

Now we have all the required building blocks to send our first transactional email in Kotlin. Let's see how in the next step.

## Sending a Transactional Email

Time to see how can we send a transactional email in Kotlin. This can be easily achieved with the following lines of code:

```kotlin
import java.util.*
import sendinblue.ApiException
import sendinblue.Configuration
import sendinblue.auth.ApiKeyAuth
import sibApi.SmtpApi
import sibModel.SendSmtpEmail
import sibModel.SendSmtpEmailTo

object Sendinblue {
  
  fun sendTestEmail() {
    val sendinblueApiClient = Configuration.getDefaultApiClient()

    val apiKeyAuth = sendinblueApiClient.getAuthentication("api-key") as ApiKeyAuth
    apiKeyAuth.apiKey = "xkeysib-o1y..." // replace this with your API key

    val sendinblueSmtpApiInstance = SmtpApi()
    val sendinblueSendSmtpEmail = SendSmtpEmail()

    // defining template parameters
    val parameters = HashMap<String, String>()
    parameters["ORDER"] = "12345"
    parameters["DATE"] = "12/06/19"

    // defining to whom to send the transactional email 
    val recipients = ArrayList<SendSmtpEmailTo>()
    val recipient = SendSmtpEmailTo()
    recipient.email = "thomas.bianchi@email.com" // replace this with the recipient's email address
    recipient.name = "Thomas Bianchi" // replace this with the recipient's full name
    recipients.add(recipient)

    sendinblueSendSmtpEmail.templateId(1) // replace this with your template id
    sendinblueSendSmtpEmail.to = recipients
    sendinblueSendSmtpEmail.params(parameters)

    sendinblueSmtpApiInstance.sendTransacEmail(sendSmtpEmail)
  }
  
}
```

Transactional parameters must be defined in a [`Map`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/) object. A Map is a collection that holds pairs of `<key,value>` objects. In each pair, the `key` is the name of a parameter as defined in the template, and the `value` is the corresponding value we want to give to the selected parameter.

The Sendinblue API client requires a list of `SendSmtpEmailTo` objects, each of which represents one recipient.

Each recipient must have an email, while a name is optional. The former should be a contact registered in Sendinblue and assigned to a contact list, and the latter is the name that will be attached to the email recipient, which will appear in the email headers, but not in the email body.

If you created a template as described in the previous step, this is the result you should get:

![](https://writech.run/wp-content/uploads/2023/09/1xj1oxPOUTlQhVYjyjjMUTg.png)

### Extra

We have just seen how to send a transactional email with the official Sendinblue API client, but the same result can be achieved using the [`https://api.sendinblue.com/v3/smtp/email`](https://api.sendinblue.com/v3/smtp/email) API.

## Conclusion

In this tutorial, we showed how to send transactional emails in Kotlin. We managed to achieve our goal by harnessing the official Sendinblue Java API client and taking advantage of Kotlin's peculiar feature of being interoperable with Java.

I hope that you found this article helpful — thanks for reading!
  
***
_The post "[Sending Transactional Emails With Sendinblue in Kotlin](https://writech.run/blog/sending-transactional-emails-with-sendinblue-in-kotlin-6f6920f49733/)" appeared first on [Writech](https://writech.run)._