-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ROX-23254: Add AWS SES client V2 #1822
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Package email contains methods to send emails via AWS SES | ||
package email | ||
|
||
import ( | ||
"context" | ||
"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" | ||
"github.com/golang/glog" | ||
) | ||
|
||
// SES struct keeps necessary configuration for sending email via AWS SES | ||
type SES struct { | ||
sesClient SESClient | ||
} | ||
|
||
// NewSES creates a new SES instance with initialised AWS SES client using AWS Config | ||
func NewSES(ctx context.Context) (*SES, error) { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to laod AWS SDK config: %v", err) | ||
} | ||
sesClient := ses.NewFromConfig(cfg) | ||
|
||
return &SES{sesClient: sesClient}, nil | ||
} | ||
|
||
// SESClient is an interface that sends email using provided function | ||
type SESClient interface { | ||
SendEmail(ctx context.Context, params *ses.SendEmailInput, optFns ...func(*ses.Options)) (*ses.SendEmailOutput, error) | ||
} | ||
|
||
// SendEmail sends email via AWS SES API | ||
func (s *SES) SendEmail(ctx context.Context, sender string, to []string, subject, htmlBody, textBody string) (string, error) { | ||
input := &ses.SendEmailInput{ | ||
Source: aws.String(sender), | ||
Destination: &types.Destination{ | ||
ToAddresses: to, | ||
}, | ||
Message: &types.Message{ | ||
Subject: &types.Content{ | ||
Data: aws.String(subject), | ||
}, | ||
Body: &types.Body{ | ||
Html: &types.Content{ | ||
Data: aws.String(htmlBody), | ||
}, | ||
Text: &types.Content{ | ||
Data: aws.String(textBody), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
result, err := s.sesClient.SendEmail(ctx, input) | ||
if err != nil { | ||
glog.Errorf("Failed sending email: %v", err) | ||
return "", fmt.Errorf("failed to send email: %v", err) | ||
} | ||
|
||
return *result.MessageId, nil | ||
} |
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,82 @@ | ||
package email | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ses" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockSESClient struct { | ||
sender string | ||
to []string | ||
subject string | ||
htmlBody string | ||
textBody string | ||
|
||
SendEmailFunc func(ctx context.Context, params *ses.SendEmailInput, optFns ...func(*ses.Options)) (*ses.SendEmailOutput, error) | ||
} | ||
|
||
func (m *MockSESClient) SendEmail(ctx context.Context, params *ses.SendEmailInput, optFns ...func(*ses.Options)) (*ses.SendEmailOutput, error) { | ||
m.sender = *params.Source | ||
m.to = params.Destination.ToAddresses | ||
m.subject = *params.Message.Subject.Data | ||
m.htmlBody = *params.Message.Body.Html.Data | ||
m.textBody = *params.Message.Body.Text.Data | ||
|
||
return m.SendEmailFunc(ctx, params, optFns...) | ||
} | ||
|
||
func TestSendEmail_Success(t *testing.T) { | ||
sender := "[email protected]" | ||
to := []string{"[email protected]", "[email protected]"} | ||
subject := "subject" | ||
htmlBody := "<h1>HTML body</h1>" | ||
textBody := "text body" | ||
|
||
testMessageID := "test-message-id" | ||
|
||
mockClient := &MockSESClient{ | ||
SendEmailFunc: func(ctx context.Context, params *ses.SendEmailInput, optFns ...func(*ses.Options)) (*ses.SendEmailOutput, error) { | ||
return &ses.SendEmailOutput{ | ||
MessageId: aws.String(testMessageID), | ||
}, nil | ||
}, | ||
} | ||
mockedSES := SES{sesClient: mockClient} | ||
|
||
messageID, err := mockedSES.SendEmail(context.Background(), sender, to, subject, htmlBody, textBody) | ||
assert.NoError(t, err) | ||
assert.Equal(t, testMessageID, messageID) | ||
assert.Equal(t, sender, mockClient.sender) | ||
assert.Equal(t, to, mockClient.to) | ||
assert.Equal(t, subject, mockClient.subject) | ||
assert.Equal(t, htmlBody, mockClient.htmlBody) | ||
assert.Equal(t, textBody, mockClient.textBody) | ||
} | ||
|
||
func TestSendEmail_Failure(t *testing.T) { | ||
errorText := "failed to send email" | ||
|
||
mockClient := &MockSESClient{ | ||
SendEmailFunc: func(ctx context.Context, params *ses.SendEmailInput, optFns ...func(*ses.Options)) (*ses.SendEmailOutput, error) { | ||
return nil, errors.New(errorText) | ||
}, | ||
} | ||
mockedSES := SES{sesClient: mockClient} | ||
|
||
messageID, err := mockedSES.SendEmail( | ||
context.Background(), | ||
"[email protected]", | ||
[]string{"[email protected]"}, | ||
"Test Subject", | ||
"<h1>HTML body</h1>", | ||
"Text body", | ||
) | ||
assert.Error(t, err) | ||
assert.Equal(t, "", messageID) | ||
assert.Contains(t, err.Error(), errorText) | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the recommended way to get session in
aws-sdk-go-v2
Migrating from NewSession with aws.Config options