Skip to content
Snippets Groups Projects
Select Git revision
  • d82e4aa5108e5a8a864939cc5a1ef39a78dcc830
  • main default protected
  • v1.303.0
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
22 results

ses.go

Blame
  • ses.go 5.99 KiB
    package ses
    
    import (
    	"bytes"
    	"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"
    	sesTypes "github.com/aws/aws-sdk-go-v2/service/ses/types"
    	"github.com/prozz/aws-embedded-metrics-golang/emf"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/s3"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
    	"gopkg.in/gomail.v2"
    	"io"
    	"strings"
    )
    
    const (
    	InvalidParameterValueErrorString = "InvalidParameterValue"
    )
    
    var (
    	clients = map[string]*ClientWithHelpers{}
    
    	ErrorSESClientNotEstablished = errors.Error("could not establish an SES client")
    )
    
    type ClientWithHelpers struct {
    	SESClient *ses.Client
    }
    
    type Email struct {
    	Recipient           string                 `json:"recipient"`
    	FromName            string                 `json:"from_name"`
    	FromEmail           string                 `json:"from_email"`
    	Subject             string                 `json:"subject"`
    	EmailCcName         string                 `json:"email_cc_name"`
    	EmailCcAddress      string                 `json:"email_cc_address"`
    	ReplyToEmailAddress string                 `json:"reply_to_email_address"`
    	Body                EmailBody              `json:"body"`
    	Attachments         *[]s3.S3UploadResponse `json:"attachments"`
    }
    
    type EmailBody struct {
    	HTMLBody string `json:"html_body"`
    	TextBody string `json:"text_body"`
    }
    
    func GetClient(region ...string) *ClientWithHelpers {
    	sesRegion := "eu-west-1"
    
    	// Set custom region
    	if region != nil && len(region) > 0 {
    		sesRegion = region[0]
    	}
    
    	// Check if client exists for region, if it does return it
    	if sesClient, ok := clients[sesRegion]; ok && sesClient != nil {
    		return sesClient
    	}
    
    	cfg, err := config.LoadDefaultConfig(context.TODO(),
    		config.WithRegion(sesRegion),
    	)
    	if err != nil {
    		return &ClientWithHelpers{}
    	}