NavigationContentFooter
Jump toSuggest an edit

Sending emails with Transactional Email and Serverless using the Serverless Framework

Reviewed on 01 February 2024Published on 01 February 2024
  • transactional-email
  • serverless
  • emails
  • functions
  • serverless-framework

In this tutorial, you will learn how to send emails with Transactional Email and Serverless Functions using the Serverless Framework tool.

Before you start

To complete the actions presented below, you must have:

  • A Scaleway account logged into the console
  • Owner status or IAM permissions allowing you to perform actions in the intended Organization
  • Configured your domain with Transactional Email and added SPF, DKIM and MX records
  • Installed the 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 -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 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"}'
Docs APIScaleway consoleDedibox consoleScaleway LearningScaleway.comPricingBlogCarreer
© 2023-2024 – Scaleway