forked from awsdocs/aws-doc-sdk-examples
/
SendMessage.go
107 lines (93 loc) · 3.54 KB
/
SendMessage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[ses.go.send_message]
package main
// snippet-start:[ses.go.send_message.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/service/ses/sesiface"
)
// snippet-end:[ses.go.send_message.imports]
// snippet-start:[ses.go.send_message.consts]
const (
// Subject is the subject line for the email
Subject = "Amazon SES Test (AWS SDK for Go)"
// HTMLBody is the HTML body for the email
HTMLBody = "<h1>Amazon SES Test Email (AWS SDK for Go)</h1><p>This email was sent with " +
"<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the " +
"<a href='https://aws.amazon.com/sdk-for-go/'>AWS SDK for Go</a>.</p>"
// TextBody is the email body for recipients with non-HTML email clients
TextBody = "This email was sent with Amazon SES using the AWS SDK for Go."
// CharSet is the character encoding for the email
CharSet = "UTF-8"
)
// snippet-end:[ses.go.send_message.consts]
// SendMsg sends an email message to an Amazon SES recipient
// Inputs:
// svc is the Amazon SES service client
// sender is the email address in the From field
// recipient is the email address in the To field
// Output:
// If success, nil
// Otherwise, an error from the call to SendEmail
func SendMsg(svc sesiface.SESAPI, sender, recipient *string) error {
// snippet-start:[ses.go.send_message.call]
_, err := svc.SendEmail(&ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{},
ToAddresses: []*string{
recipient,
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(HTMLBody),
},
Text: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(TextBody),
},
},
Subject: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(Subject),
},
},
Source: sender,
})
// snippet-end:[ses.go.send_message.call]
return err
}
func main() {
// snippet-start:[ses.go.send_message.args]
sender := flag.String("t", "", "The email address for the 'From' field")
recipient := flag.String("t", "", "The email address for the 'To' field")
subject := flag.String("s", "Amazon SES Test (AWS SDK for Go)", "The text for the 'Subject' field")
flag.Parse()
if *sender == "" || *recipient == "" || *subject == "" {
fmt.Println("You must supply an email address for the sender and recipient, and a subject")
fmt.Println("-f SENDER -t RECIPIENT -s SUBJECT")
return
}
// snippet-end:[ses.go.send_message.args]
// snippet-start:[ses.go.send_message.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := ses.New(sess)
// snippet-end:[ses.go.send_message.session]
err := SendMsg(svc, sender, recipient)
if err != nil {
fmt.Println("Got an error sending message:")
fmt.Println(err.Error())
return
}
fmt.Println("Email sent to address: " + *recipient)
}
// snippet-end:[ses.go.send_message]