aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

aws-doc-sdk-examples/gov2:Why are there no examples for Go v2 SES?

Open weimx2023 opened this issue 1 year ago • 1 comments

Background story

We are currently developing a business module for sending emails using the V2 version of the Go SDK

What does this example accomplish?

We would like to use some examples, such as setting up SES and sending emails.

Which AWS service(s)?

SES

Which AWS SDKs or tools?

  • [ ] All languages
  • [ ] .NET
  • [ ] C++
  • [X] Go (v2)
  • [ ] Java
  • [ ] Java (v2)
  • [ ] JavaScript
  • [ ] JavaScript (v3)
  • [ ] Kotlin
  • [ ] PHP
  • [ ] Python
  • [ ] Ruby
  • [ ] Rust
  • [ ] Swift
  • [ ] Not applicable

Are there existing code examples to leverage?

In the repository at https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/go/ses/README.md, we can find examples for v1.

Do you have any reference code?

No response

weimx2023 avatar Oct 14 '24 06:10 weimx2023

After searching for AWS SDK v2 SES docs,I wrote this sample code. I'm open to suggestions for a better implementation. I hope it helps! What do you think about?

// filename: aws_sesv2.go
package main

import (
	"context"
	"time"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/sesv2"
	"github.com/aws/aws-sdk-go-v2/service/sesv2/types"
)

var (
	Charset   = "UTF-8"
	Sender    = "[email protected]"
	Recipient = "[email protected]"
	Subject   = "Test Subject"
	TextBody  = "Hello from AWS SES V2!"
)

// Example usage
func main() {
	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		// replace with your better error handling
		panic(err)
	}

	sesClient := sesv2.NewFromConfig(cfg)

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	input := &sesv2.SendEmailInput{
		Destination: &types.Destination{
			ToAddresses: []string{Recipient},
		},
		Content: &types.EmailContent{
			Simple: &types.Message{
				Body: &types.Body{
					Text: &types.Content{
						Charset: &Charset,
						Data:    &TextBody,
					},
					// Html: &types.Content{
					// 	Charset: &Charset,
					// 	Data:    &TextBody,
					// },
				},
				Subject: &types.Content{
					Charset: &Charset,
					Data:    &Subject,
				},
			},
		},
		FromEmailAddress: &Sender,
	}

	_, err = sesClient.SendEmail(ctx, input)
	if err != nil {
		// replace with your better error handling
		panic(err)
	}
}

Run

Credentials are coming from environment variables by default.

AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar AWS_REGION=us-west-2 go run aws_sesv2.go

mertssmnoglu avatar Sep 19 '25 22:09 mertssmnoglu