-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Send TEM from a serverless function | ||
|
||
This example demonstrates how to send TEM with an SMTP server from Python functions. | ||
|
||
It assumes that you are familiar with how Serverless Functions and Scaleway Transactional Email work. | ||
If needed, you can check the Scaleway official documentation for serverless functions [here](https://www.scaleway.com/en/docs/serverless/functions/quickstart/) | ||
and for TEM [here](https://www.scaleway.com/en/docs/managed-services/transactional-email/quickstart/). | ||
|
||
## Requirements | ||
|
||
* You have configured your domain with Transactional Email | ||
* You have generated an API key with the permission `TransactionalEmailFullAccess` | ||
* You have [Python](https://www.python.org/) installed on your machine | ||
* You have [Terraform](https://registry.terraform.io/providers/scaleway/scaleway/latest/docs) installed on your machine | ||
|
||
## Testing with serverless offline for Python | ||
|
||
In order to test your function locally before the deployment, you can install our offline testing library with: | ||
|
||
```bash | ||
pip install scaleway_functions_python==0.2.0 | ||
``` | ||
|
||
Export your environment variables and then launch your function locally: | ||
```bash | ||
export TEM_PROJECT_ID=<the Project ID of the Project in which the TEM domain was created> | ||
export SECRET_KEY=<the secret key of the API key of the project used to manage your TEM domain> | ||
|
||
python handler.py | ||
``` | ||
|
||
Test your local function using `curl`: | ||
|
||
```bash | ||
curl http://localhost:8080 | ||
``` | ||
|
||
This should email the recipient defined in `handler.py`. | ||
|
||
## Deploy | ||
|
||
Use the Terraform configuration to deploy the function. | ||
|
||
```shell | ||
terraform init | ||
terraform apply -var "swc_project_id=$TEM_PROJECT_ID" -var "scw_secret_key=$SECRET_KEY" | ||
``` | ||
|
||
## Call the function | ||
|
||
When the deployment is complete, you can check the deployment succeeded either by: | ||
|
||
i) curl the container's endpoint with: | ||
```sh | ||
curl $(terraform output -raw endpoint) | ||
``` | ||
ii) hit it from a browser. | ||
|
||
Doing so will send an e-mail to the recipient defined in the file `handler.py`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
import logging | ||
import smtplib | ||
from email.mime.text import MIMEText | ||
|
||
def handle(event, context): | ||
|
||
# SMTP configuration, see https://www.scaleway.com/en/docs/managed-services/transactional-email/reference-content/smtp-configuration/ for more info | ||
|
||
host = "smtp.tem.scw.cloud" # The domain name or IP address of the SMTP server. If you are using Scaleway TEM, the domain to use is smtp.tem.scw.cloud | ||
port = 465 | ||
|
||
login = os.environ.get('TEM_PROJECT_ID') # Your Scaleway SMTP username is the Project ID of the Project in which the TEM domain was created | ||
password = os.environ.get('SECRET_KEY') # Your password is the secret key of the API key of the project used to manage your TEM domain | ||
|
||
sender_email = "[email protected]" # the email address that will appear as the sender | ||
receiver_email = "[email protected]" # the email address to send the email to | ||
|
||
message = MIMEText("You've successfully sent an email from Serverless Functions!", "plain") | ||
message["Subject"] = "Congratulations" | ||
message["From"] = sender_email | ||
message["To"] = receiver_email | ||
|
||
try: | ||
with smtplib.SMTP_SSL(host, port) as server: | ||
server.login(login, password) | ||
server.sendmail(sender_email, receiver_email, message.as_string()) | ||
except Exception as e: | ||
logging.error("Failed to send email: %s", e) | ||
|
||
if __name__ == "__main__": | ||
from scaleway_functions_python import local | ||
local.serve_handler(handle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
terraform { | ||
required_providers { | ||
scaleway = { | ||
source = "scaleway/scaleway" | ||
version = ">= 2.40" | ||
} | ||
archive = { | ||
source = "hashicorp/archive" | ||
version = ">= 2.4" | ||
} | ||
} | ||
required_version = ">= 1.0" | ||
} | ||
|
||
variable "swc_project_id" { | ||
type = string | ||
} | ||
|
||
variable "scw_secret_key" { | ||
type = string | ||
sensitive = true | ||
} | ||
|
||
data "archive_file" "function" { | ||
type = "zip" | ||
source_file = "${path.module}/handler.py" | ||
output_path = "${path.module}/function.zip" | ||
} | ||
|
||
resource "scaleway_function_namespace" "main" { | ||
name = "serverless-with-tem-example" | ||
description = "Serverless with TEM example" | ||
} | ||
|
||
resource "scaleway_function" "main" { | ||
namespace_id = scaleway_function_namespace.main.id | ||
name = "python-tem-smtp-server" | ||
runtime = "python311" | ||
handler = "handler.handle" | ||
privacy = "public" | ||
zip_file = data.archive_file.function.output_path | ||
zip_hash = data.archive_file.function.output_sha256 | ||
deploy = true | ||
|
||
secret_environment_variables = { | ||
TEM_PROJECT_ID = var.swc_project_id | ||
SECRET_KEY = var.scw_secret_key | ||
} | ||
} | ||
|
||
output "endpoint" { | ||
value = scaleway_function.main.domain_name | ||
} |