Find out how to restrict access to your functions.
Sending emails with Transactional Email and Serverless using the Serverless Framework
- 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
-
Open a terminal and paste the following command to install the Serverless Framework CLI on your local machine:
npm install serverless -g -
Clone the GitHub repository containing the configuration files needed to deploy your function:
git clone github.com:scaleway/serverless-examples.git -
Access the
go-mail
directory:cd serverless-examples/functions/go-mail -
Paste the following command to install the Serverless plugin:
serverless plugin install -n serverless-scaleway-functions -
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.
-
In the same terminal as the previous steps, access the
.env
file in thego-mail
directory:nano .env -
Replace
CHANGE_ME
with your pertinent environment variables. Your.env
file should look like the following:SCW_DEFAULT_ORGANIZATION_ID: f7d9b14a-c5fe-4e50-bb6e-4c7474ee611dSCW_ACCESS_KEY: SCWDAC6A76BF6DC4EFD65SCW_SECRET_KEY: f5dd7248-ebf1-4cea-abe6-60b58eb5df58SENDER_MAIL: example@mail.com -
Press
CTRL O
andEnter
to save your changes. -
Press
CTRL X
to exit the.env
file.
Deploy your function
- In the same terminal as the previous steps, paste the following command to trigger the deployment of your function:
serverless deploy
- Log in to your Scaleway account and click the Functions section in the left-side menu. Your
gomail
namespace displays. - Click the
gomail
namespace, then click thegomail
function. - Click the Endpoints tab and copy your function’s domain. Make sure you save it as you will need it in the next steps.
- Click the Security tab, then click + Generate token.
Tip
- Follow the instructions that display in the pop-up and click Generate token.
- Copy your token and make sure that you save it before closing the pop-up.
- 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"}'