---
title: Sending emails with Transactional Email and Serverless using the Serverless Framework
description: Discover how to send emails with Scaleway's Transactional Email and Serverless using the Serverless Framework
tags: transactional-email serverless emails functions serverless-framework
products:
  - managed-services
  - serverless
  - functions
  - transactional-email
dates:
  validation: 2025-02-18
  posted: 2024-02-01
  validation_frequency: 12
difficulty: beginner
usecase:
  - best-practices
ecosystem:
  - scaleway-only
---

In this tutorial, you will learn how to send emails with [Transactional Email](/en/docs/transactional-email/quickstart/) and [Serverless Functions](/en/docs/serverless-functions/quickstart/) using the [Serverless Framework tool](/en/docs/serverless-functions/reference-content/deploy-function/#serverless-framework).

## Before you start

To complete the following actions, you must have:

* A Scaleway account logged into the [console](https://console.scaleway.com)
* [Owner](/en/docs/iam/concepts/glossary/#owner) status or [IAM permissions](/en/docs/iam/concepts/glossary/#permission) allowing you to perform actions in the intended Organization
* [Configured your domain with Transactional Email and added SPF, DKIM, and MX records](/en/docs/transactional-email/quickstart/)
* [Installed the Scaleway CLI](https://github.com/scaleway/scaleway-cli) on your machine

## Install and clone the necessary tools

1. Open a terminal and paste the following command to install the Serverless Framework CLI on your local machine:

   ```
   npm install serverless@3.41 -g
   ```

2. Clone the GitHub repository containing the configuration files needed to deploy your function:

   ```
   git clone github.com:scaleway/serverless-examples.git
   ```

3. Access the `go-mail` directory:

   ```
   cd serverless-examples/functions/go-mail
   ```

4. Paste the following command to install the Serverless plugin:

   ```
   serverless plugin install -n serverless-scaleway-functions
   ```

5. Copy and paste the following commands to export your environment variables. Make sure you add your **own variables**.

   ```
   export SCW_ACCESS_KEY=<Scaleway access key>
   export SCW_SECRET_KEY=<Scaleway secret key>
   export SCW_DEFAULT_ORGANIZATION_ID=<Scaleway default Organization>
   export SCW_DEFAULT_PROJECT_ID=<Scaleway default Project>
   export SCW_DEFAULT_REGION="fr-par"
   ```

## Add your variables

The `go-mail` directory you have cloned contains a `.env` file in which you will define your environment variables. The `serverless.yml` file will then fetch your variables from the `.env` file. This allows you to avoid hard-coding critical information in several files.

1. In the same terminal as the previous steps, access the `.env` file in the `go-mail` directory:

   ```
   nano .env
   ```

2. Replace `CHANGE_ME` with your pertinent environment variables. Your `.env` file should look like the following:

   ```
   SCW_DEFAULT_ORGANIZATION_ID: f7d9b14a-c5fe-4e50-bb6e-4c7474ee611d
   SCW_ACCESS_KEY: SCWDAC6A76BF6DC4EFD65
   SCW_SECRET_KEY: f5dd7248-ebf1-4cea-abe6-60b58eb5df58
   SENDER_MAIL: example@mail.com
   ```

3. Press `CTRL O` and `Enter` to save your changes.

4. Press `CTRL X` to exit the `.env` file.

## Deploy your function

1. In the same terminal as the previous steps, paste the following command to trigger the deployment of your function:
   ```
   serverless deploy
   ```

2. Log in to your Scaleway account and click the Functions section in the left-side menu. Your `gomail` namespace displays.

3. Click the `gomail` namespace, then click the `gomail` function.

4. Click the **Endpoints** tab and copy your function's domain. Make sure you save it as you will need it in the next steps.

5. Click the **Security** tab, then click **+ Generate token**.

   > **Tip**
   >
   > Find out [how to restrict access](/en/docs/serverless-functions/how-to/secure-a-function/#restrict-access-to-your-functions) to your functions.

6. Follow the instructions that display in the pop-up and click **Generate token**.

7. Copy your token and make sure that you save it before closing the pop-up.

8. Open your terminal and paste the following command to trigger your function. Make sure that you replace `$FUNCTION_DOMAIN` with your function's domain, `$FUNCTION_TOKEN` with the token you have generated, and `$MAIL_TO` with your recipient's email address. Optionally, you can also edit the `"subject"` and `"message"` fields.
   ```
   curl -v -X POST https://$FUNCTION_DOMAIN -H "X-Auth-Token: $FUNCTION_TOKEN" --data '{"to": "$MAILTO", "subject": "this is a test following the tem and serverless tuto", "message": "very very long message using the api"}'
   ```
