-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qinru-wang
committed
May 28, 2021
1 parent
2576de8
commit 7f8d92e
Showing
5 changed files
with
178 additions
and
2 deletions.
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,128 @@ | ||
package awsses | ||
|
||
import ( | ||
"log" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ses" | ||
) | ||
|
||
type EmailInfo struct { | ||
Sender string `json:"sender"` | ||
RecipientTo []string `json:"to"` | ||
RecipientCc []string `json:"cc"` | ||
RecipientBcc []string `json:"bcc"` | ||
Subject string `json:"subject"` | ||
HtmlBody string `json:"htmlbody"` | ||
TextBody string `json:"textbody"` | ||
CharSet string `json:"charset"` | ||
} | ||
|
||
func CreateRecipientList(addresses []string) []*string { | ||
|
||
recipientList := []*string{} | ||
|
||
if len(addresses) > 0 { | ||
for _, addr := range addresses { | ||
recipientList = append(recipientList, aws.String(addr)) | ||
} | ||
} | ||
|
||
return recipientList | ||
} | ||
|
||
func CreateEmailContent(charset string, data string) *ses.Content { | ||
content := &ses.Content{} | ||
|
||
if len(data) > 0 { | ||
data = strings.Replace(data, " ", "\n", -1) | ||
content.SetCharset(charset) | ||
content.SetData(data) | ||
} | ||
|
||
return content | ||
} | ||
|
||
func SendEmail(info EmailInfo) (*ses.SendEmailOutput, error) { | ||
// Create a new session in the us-west-2 region. | ||
// Replace us-west-2 with the AWS Region you're using for Amazon SES. | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String("ap-northeast-1")}, | ||
) | ||
|
||
if err != nil { | ||
log.Fatalf("create session failed : %s", err.Error()) | ||
return nil, err | ||
} | ||
|
||
// Create an SES session. | ||
svc := ses.New(sess) | ||
|
||
// Assemble the email. | ||
input := &ses.SendEmailInput{} | ||
|
||
// Set Email Destination | ||
destination := &ses.Destination{} | ||
|
||
if len(info.RecipientTo) > 0 { | ||
destination.SetToAddresses(CreateRecipientList(info.RecipientTo)) | ||
} | ||
if len(info.RecipientBcc) > 0 { | ||
destination.SetBccAddresses(CreateRecipientList(info.RecipientBcc)) | ||
} | ||
if len(info.RecipientCc) > 0 { | ||
destination.SetCcAddresses(CreateRecipientList(info.RecipientCc)) | ||
} | ||
|
||
input.Destination = destination | ||
|
||
// Set Email Body | ||
message := &ses.Message{} | ||
body := &ses.Body{} | ||
|
||
if len(info.HtmlBody) > 0 { | ||
body.SetHtml(CreateEmailContent(info.CharSet, info.HtmlBody)) | ||
} | ||
|
||
body.SetText(CreateEmailContent(info.CharSet, info.TextBody)) | ||
message.SetBody(body) | ||
message.SetSubject(CreateEmailContent(info.CharSet, info.Subject)) | ||
|
||
input.Message = message | ||
|
||
// Set Email Source | ||
input.SetSource(info.Sender) | ||
|
||
// Attempt to send the email. | ||
result, err := svc.SendEmail(input) | ||
|
||
// Display error messages if they occur. | ||
if err != nil { | ||
if aerr, ok := err.(awserr.Error); ok { | ||
switch aerr.Code() { | ||
case ses.ErrCodeMessageRejected: | ||
log.Fatalln(ses.ErrCodeMessageRejected, aerr.Error()) | ||
case ses.ErrCodeMailFromDomainNotVerifiedException: | ||
log.Fatalln(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error()) | ||
case ses.ErrCodeConfigurationSetDoesNotExistException: | ||
log.Fatalln(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error()) | ||
default: | ||
log.Fatalln(aerr.Error()) | ||
} | ||
} else { | ||
// Print the error, cast err to awserr.Error to get the Code and | ||
// Message from an error. | ||
log.Fatalln(err.Error()) | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
log.Printf("Send Email : %#v", info) | ||
log.Println(result) | ||
|
||
return result, 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,20 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/wangqinru/notification-go/awsses" | ||
) | ||
|
||
func awsses_test() { | ||
|
||
mailInfo := awsses.EmailInfo{RecipientTo: []string{"hogehoge@gmail.com"}, Subject: "Test Subject", TextBody: "Test TextBody"} | ||
|
||
output, err := awsses.SendEmail(mailInfo) | ||
|
||
if err != nil { | ||
log.Fatalf("Call Chat Work API messages Error %s \n", err.Error()) | ||
} else { | ||
log.Printf("Call Chat Work API Response : %+v\n", output.GoString()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/wangqinru/notification-go | ||
|
||
go 1.16 | ||
|
||
require github.com/aws/aws-sdk-go v1.38.50 |
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,25 @@ | ||
github.com/aws/aws-sdk-go v1.38.50 h1:9+dEpZbgjBMeoOes6QfZMC87uDMwM8Lw4E79L0/rPZI= | ||
github.com/aws/aws-sdk-go v1.38.50/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= | ||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= | ||
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= | ||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= | ||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= |