gitlab.com/ignitionrobotics/web/ign-go@v1.0.0-rc4/aws.go (about) 1 package ign 2 3 import ( 4 "errors" 5 "github.com/aws/aws-sdk-go/aws" 6 "github.com/aws/aws-sdk-go/aws/awserr" 7 "github.com/aws/aws-sdk-go/aws/session" 8 "github.com/aws/aws-sdk-go/service/ses" 9 ) 10 11 // SendEmail using AWS Simple Email Service (SES) 12 // The following environment variables must be set: 13 // 14 // AWS_REGION 15 // AWS_ACCESS_KEY_ID 16 // AWS_SECRET_ACCESS_KEY 17 func SendEmail(sender, recipient, subject, body string) error { 18 19 // The character encoding for the email. 20 charSet := "UTF-8" 21 22 sess := session.Must(session.NewSession()) 23 24 // Create an SES session. 25 svc := ses.New(sess) 26 27 // Assemble the email. 28 input := &ses.SendEmailInput{ 29 Destination: &ses.Destination{ 30 CcAddresses: []*string{}, 31 ToAddresses: []*string{ 32 aws.String(recipient), 33 }, 34 }, 35 Message: &ses.Message{ 36 Body: &ses.Body{ 37 Html: &ses.Content{ 38 Charset: aws.String(charSet), 39 Data: aws.String(body), 40 }, 41 }, 42 Subject: &ses.Content{ 43 Charset: aws.String(charSet), 44 Data: aws.String(subject), 45 }, 46 }, 47 Source: aws.String(sender), 48 } 49 50 // Attempt to send the email. 51 _, err := svc.SendEmail(input) 52 53 // Return error messages if they occur. 54 if err != nil { 55 if aerr, ok := err.(awserr.Error); ok { 56 var code string 57 switch aerr.Code() { 58 case ses.ErrCodeMessageRejected: 59 code = ses.ErrCodeMessageRejected 60 case ses.ErrCodeMailFromDomainNotVerifiedException: 61 code = ses.ErrCodeMailFromDomainNotVerifiedException 62 case ses.ErrCodeConfigurationSetDoesNotExistException: 63 code = ses.ErrCodeConfigurationSetDoesNotExistException 64 default: 65 code = "Unknown AWS SES error" 66 } 67 return errors.New(code + " " + aerr.Error()) 68 } 69 return errors.New(err.Error()) 70 } 71 72 return nil 73 }