· 5 years ago · May 11, 2020, 03:02 PM
1package main
2
3import (
4 "os"
5 "fmt"
6 "net/url"
7 "strings"
8 "github.com/aws/aws-sdk-go/aws"
9 "github.com/aws/aws-lambda-go/events"
10 "github.com/aws/aws-lambda-go/lambda"
11 "github.com/aws/aws-sdk-go/aws/endpoints"
12 "github.com/aws/aws-sdk-go/aws/session"
13 "github.com/aws/aws-sdk-go/service/ses"
14 "github.com/aws/aws-sdk-go/aws/awserr"
15)
16
17const (
18 // This address must be verified with Amazon SES.
19 // Sender = "noreply@dc.siemens.com"
20
21 // The subject line for the email.
22 Subject = "Contact request from fluid web pages"
23
24 // The character encoding for the email.
25 CharSet = "UTF-8"
26)
27
28// Response is of type APIGatewayProxyResponse since we're leveraging the
29// AWS Lambda Proxy Request functionality (default behavior)
30//
31// https://serverless.com/framework/docs/providers/aws/events/apigateway/#lambda-proxy-integration
32type Response events.APIGatewayProxyResponse
33
34// StringSlice converts a slice of string values into a slice of
35// string pointers
36func StringSlice(src []string) []*string {
37 dst := make([]*string, len(src))
38 for i := 0; i < len(src); i++ {
39 dst[i] = &(src[i])
40 }
41 return dst
42}
43
44func Handler(request events.APIGatewayProxyRequest) (Response, error) {
45 sess, err := session.NewSession(&aws.Config{
46 Region: aws.String(endpoints.EuWest1RegionID)},
47 )
48
49 // Create an SES session.
50 svc := ses.New(sess)
51
52 fmt.Println("MAIL_SENDER: " + os.Getenv("MAIL_SENDER"))
53 fmt.Println("MAIL_RECIPIENT: " + os.Getenv("MAIL_RECIPIENT"))
54 fmt.Println("Form Body: " + request.Body)
55
56 // Parsing Query
57 m, err := url.ParseQuery(request.Body)
58
59 // Replacing absent parameters by empty string
60 keys := [9]string{"salutation", "firstName", "lastName", "company",
61 "jobrole", "email", "country", "topic", "message"}
62 for _, key := range keys{
63 if _, ok := m[key]; !ok {
64 m[key] = []string{""}
65 }
66 }
67
68 // Formatting for text and HTML
69 TextBody := fmt.Sprintf("Salutation: %s\n"+
70 "First name: %s\n"+
71 "Last name: %s\n"+
72 "Company: %s\n"+
73 "Job role: %s\n"+
74 "Email: %s\n"+
75 "Country / Region: %s\nSelected topics: %s\n"+
76 "Message: %v", m["salutation"][0], m["firstName"][0], m["lastName"][0],
77 m["company"][0], m["jobrole"][0], m["email"][0], m["country"][0],
78 m["topic"][0], m["message"][0])
79
80 HtmlBody := fmt.Sprintf("<p><strong>Salutation</strong>: %s</p>"+
81 "<p><strong>First name</strong>: %s</p>\n"+
82 "<p><strong>Last name</strong>: %s</p>\n"+
83 "<p><strong>Company</strong>: %s</p>\n"+
84 "<p><strong>Job role</strong>: %s</p>\n"+
85 "<p><strong>Email</strong>: %s</p>\n"+
86 "<p><strong>Country / Region</strong>: %s</p>\n"+
87 "<p><strong>Selected topics</strong>: %s</p>\n"+
88 "<p><strong>Message</strong>: %s</p>", m["salutation"][0], m["firstName"][0], m["lastName"][0],
89 m["company"][0], m["jobrole"][0], m["email"][0], m["country"][0],
90 m["topic"][0], m["message"][0])
91
92 // Assemble the email.
93 input := &ses.SendEmailInput{
94 Destination: &ses.Destination{
95 CcAddresses: []*string{
96 },
97 ToAddresses: StringSlice(strings.Split(os.Getenv("MAIL_RECIPIENT"), ",")),
98 },
99 Message: &ses.Message{
100 Body: &ses.Body{
101 Html: &ses.Content{
102 Charset: aws.String(CharSet),
103 Data: aws.String(HtmlBody),
104 },
105 Text: &ses.Content{
106 Charset: aws.String(CharSet),
107 Data: aws.String(TextBody),
108 },
109 },
110 Subject: &ses.Content{
111 Charset: aws.String(CharSet),
112 Data: aws.String(Subject),
113 },
114 },
115 Source: aws.String(os.Getenv("MAIL_SENDER")),
116 }
117
118 // Attempt to send the email.
119 result, err := svc.SendEmail(input)
120
121 // Display error messages if they occur.
122 if err != nil {
123 if aerr, ok := err.(awserr.Error); ok {
124 switch aerr.Code() {
125 case ses.ErrCodeMessageRejected:
126 fmt.Println(ses.ErrCodeMessageRejected, aerr.Error())
127 case ses.ErrCodeMailFromDomainNotVerifiedException:
128 fmt.Println(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
129 case ses.ErrCodeConfigurationSetDoesNotExistException:
130 fmt.Println(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
131 default:
132 fmt.Println(aerr.Error())
133 }
134 } else {
135 // Print the error, cast err to awserr.Error to get the Code and
136 // Message from an error.
137 fmt.Println(err.Error())
138 }
139
140 resp := Response {
141 StatusCode: 500,
142 IsBase64Encoded: false,
143 Body: err.Error(),
144 }
145 return resp, nil
146 }
147
148 fmt.Println("Email Sent to address: " + os.Getenv("MAIL_RECIPIENT"))
149 fmt.Println(result)
150
151 resp := Response {
152 StatusCode: 200,
153 IsBase64Encoded: false,
154 Body: "Successful",
155 // TODO: remove and set cors settings via API gateway
156 Headers: map[string]string{
157 "Access-Control-Allow-Origin": "*",
158 },
159 }
160 return resp, nil
161}
162
163func main() {
164 lambda.Start(Handler)
165}