forked from baron/baron-sso
aws ses 구현
This commit is contained in:
79
backend/internal/service/ses_service.go
Normal file
79
backend/internal/service/ses_service.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"baron-sso-backend/internal/domain"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/ses"
|
||||
"github.com/aws/aws-sdk-go-v2/service/ses/types"
|
||||
)
|
||||
|
||||
type SesServiceImpl struct {
|
||||
client *ses.Client
|
||||
sender string
|
||||
}
|
||||
|
||||
func NewEmailService() domain.EmailService {
|
||||
region := os.Getenv("AWS_REGION")
|
||||
accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
|
||||
secretKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
|
||||
sender := os.Getenv("AWS_SES_SENDER")
|
||||
|
||||
if region == "" || accessKey == "" || secretKey == "" {
|
||||
log.Println("[EmailService] AWS configuration missing, email service will not work")
|
||||
return nil
|
||||
}
|
||||
|
||||
cfg, err := config.LoadDefaultConfig(context.TODO(),
|
||||
config.WithRegion(region),
|
||||
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("[EmailService] Failed to load AWS config: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return &SesServiceImpl{
|
||||
client: ses.NewFromConfig(cfg),
|
||||
sender: sender,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SesServiceImpl) SendEmail(to, subject, body string) error {
|
||||
if s == nil || s.client == nil {
|
||||
return fmt.Errorf("email service not initialized")
|
||||
}
|
||||
|
||||
input := &ses.SendEmailInput{
|
||||
Destination: &types.Destination{
|
||||
ToAddresses: []string{to},
|
||||
},
|
||||
Message: &types.Message{
|
||||
Body: &types.Body{
|
||||
Html: &types.Content{
|
||||
Charset: aws.String("UTF-8"),
|
||||
Data: aws.String(body),
|
||||
},
|
||||
},
|
||||
Subject: &types.Content{
|
||||
Charset: aws.String("UTF-8"),
|
||||
Data: aws.String(subject),
|
||||
},
|
||||
},
|
||||
Source: aws.String(s.sender),
|
||||
}
|
||||
|
||||
_, err := s.client.SendEmail(context.TODO(), input)
|
||||
if err != nil {
|
||||
log.Printf("[EmailService] Failed to send email to %s: %v", to, err)
|
||||
} else {
|
||||
log.Printf("[EmailService] Email sent successfully to %s", to)
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user