Skip to content

Commit 66992ee

Browse files
committed
inital commit
1 parent a576c36 commit 66992ee

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

.github/workflow/deploy.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Deploy to DigitalOcean Functions
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v2
18+
with:
19+
go-version: '1.20'
20+
21+
- name: Build Go function
22+
run: go build -o main ./sendTransactionalEmail/main
23+
24+
- name: Login to DigitalOcean
25+
uses: digitalocean/action-doctl@v2
26+
with:
27+
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
28+
29+
- name: Deploy function
30+
run: |
31+
doctl functions deploy your-function-name \
32+
--entrypoint ./main \
33+
--runtime go1.20 \
34+
--region your-region \
35+
--source .
36+

sendTransactionalEmail/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/codepumper/functions/sendTransactionalEmail
2+
3+
go 1.22.3
4+
5+
require github.com/resend/resend-go/v2 v2.6.0 // indirect

sendTransactionalEmail/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/resend/resend-go/v2 v2.6.0 h1:bHwF79iCYC3V9H7/DL0MAIoz0hiAqM+Rq9G4EhgooyE=
2+
github.com/resend/resend-go/v2 v2.6.0/go.mod h1:ihnxc7wPpSgans8RV8d8dIF4hYWVsqMK5KxXAr9LIos=

sendTransactionalEmail/main.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"time"
7+
"context"
8+
9+
"github.com/resend/resend-go/v2"
10+
)
11+
12+
type Request struct {
13+
Name string `json:"name"`
14+
}
15+
16+
type Response struct {
17+
StatusCode int `json:"statusCode,omitempty"`
18+
Headers map[string]string `json:"headers,omitempty"`
19+
Body string `json:"body,omitempty"`
20+
}
21+
22+
func Main(ctx context.Context, event Event) (*Response, error) {
23+
apiKey := os.Getenv("RESEND_API_KEY")
24+
if apiKey == "" {
25+
fmt.Println("RESEND_API_KEY environment variable is not set")
26+
return
27+
}
28+
29+
emailAddress := os.Getenv("EMAIL_ADDRESS")
30+
if apiKey == "" {
31+
fmt.Println("EMAIL_ADDRESS environment variable is not set")
32+
return
33+
}
34+
35+
client := resend.NewClient(apiKey)
36+
37+
params := &resend.SendEmailRequest{
38+
From: "Acme <[email protected]>",
39+
To: []string{emailAddress},
40+
Html: "<strong>hello world</strong>",
41+
Subject: "Hello from Golang",
42+
}
43+
44+
maxRetries := 5
45+
sent, err := sendEmailWithRetry(client, params, maxRetries)
46+
if err != nil {
47+
fmt.Println(err.Error())
48+
return
49+
}
50+
51+
fmt.Println("Email sent successfully, ID:", sent.Id)
52+
53+
return &Response{
54+
Body: fmt.Sprintf("Hello from you function!"),
55+
}, nil
56+
}
57+
58+
59+
func sendEmailWithRetry(client *resend.Client, params *resend.SendEmailRequest, maxRetries int) (*resend.SendEmailResponse, error) {
60+
var sent *resend.SendEmailResponse
61+
var err error
62+
63+
for i := 0; i < maxRetries; i++ {
64+
sent, err = client.Emails.Send(params)
65+
if err == nil {
66+
return sent, nil
67+
}
68+
69+
// Print the error
70+
fmt.Println("Attempt", i+1, "failed:", err.Error())
71+
72+
// Exponential backoff delay
73+
delay := time.Duration(math.Pow(2, float64(i))) * time.Second
74+
fmt.Printf("Retrying in %v seconds...\n", delay.Seconds())
75+
time.Sleep(delay)
76+
}
77+
78+
return nil, fmt.Errorf("failed to send email after %d attempts: %w", maxRetries, err)
79+
}

0 commit comments

Comments
 (0)