setup resend client for email service
This commit is contained in:
parent
4448c4a841
commit
82ac33ec66
5 changed files with 56 additions and 9 deletions
|
|
@ -1,6 +1,11 @@
|
|||
package service
|
||||
|
||||
import "context"
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"github.com/resend/resend-go/v2"
|
||||
)
|
||||
|
||||
type EmailParams struct {
|
||||
From string
|
||||
|
|
@ -13,6 +18,34 @@ type EmailClient interface {
|
|||
SendWithContext(ctx context.Context, params EmailParams) (string, error)
|
||||
}
|
||||
|
||||
type ResendClient struct {
|
||||
client *resend.Client
|
||||
}
|
||||
|
||||
func NewResendClient(apiKey string) *ResendClient {
|
||||
var client *resend.Client
|
||||
if apiKey != "" {
|
||||
client = resend.NewClient(apiKey)
|
||||
} else {
|
||||
slog.Warn("cannot initialize Resend client with empty api key")
|
||||
return nil
|
||||
}
|
||||
return &ResendClient{client: client}
|
||||
}
|
||||
|
||||
func (c *ResendClient) SendWithContext(ctx context.Context, params EmailParams) (string, error) {
|
||||
res, err := c.client.Emails.SendWithContext(ctx, &resend.SendEmailRequest{
|
||||
From: params.From,
|
||||
To: params.To,
|
||||
Subject: params.Subject,
|
||||
Text: params.Text,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return res.Id, nil
|
||||
}
|
||||
|
||||
type EmailService struct {
|
||||
client EmailClient
|
||||
fromEmail string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue