62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
"github.com/aws/aws-sdk-go-v2/service/ses"
|
|
"github.com/aws/aws-sdk-go-v2/service/ses/types"
|
|
)
|
|
|
|
func ResetUserEmail(userEmail Email, customerId CustomerId) error {
|
|
// Load AWS configuration
|
|
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
|
|
if err != nil {
|
|
return errors.New(fmt.Sprintf("unable to load SDK config, %v", err))
|
|
}
|
|
|
|
nkodeResetJwt, err := ResetNKodeToken(userEmail, customerId)
|
|
if err != nil {
|
|
return errors.New(fmt.Sprintf("unable to load SDK config, %v", err))
|
|
}
|
|
// Create an SES client
|
|
sesClient := ses.NewFromConfig(cfg)
|
|
|
|
// Define sender and recipient
|
|
sender := "mail@nkode.tech"
|
|
|
|
// Define email subject and body
|
|
subject := "nKode Reset"
|
|
htmlBody := fmt.Sprintf("<h1>Hello!</h1><p>Click the link to reset your nKode.</p><a href=\"http://%s?token=%s\">Reset nKode</a>", FrontendHost, nkodeResetJwt)
|
|
|
|
// Construct the email message
|
|
input := &ses.SendEmailInput{
|
|
Destination: &types.Destination{
|
|
ToAddresses: []string{string(userEmail)},
|
|
},
|
|
Message: &types.Message{
|
|
Body: &types.Body{
|
|
Html: &types.Content{
|
|
Data: aws.String(htmlBody),
|
|
},
|
|
},
|
|
Subject: &types.Content{
|
|
Data: aws.String(subject),
|
|
},
|
|
},
|
|
Source: aws.String(sender),
|
|
}
|
|
|
|
// Send the email
|
|
resp, err := sesClient.SendEmail(context.TODO(), input)
|
|
if err != nil {
|
|
return errors.New(fmt.Sprintf("failed to send email, %v", err))
|
|
}
|
|
|
|
// Output the message ID of the sent email
|
|
fmt.Printf("Email sent successfully, Message ID: %s\n", *resp.MessageId)
|
|
return nil
|
|
}
|