-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (74 loc) · 2.14 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
)
type Email struct {
Name string `json:"name"`
Phone string `json:"phone"`
Email string `json:"email"`
Message string `json:"message"`
}
const (
Sender = "[email protected]"
Recipient = "[email protected]"
// The character encoding for the email.
CharSet = "UTF-8"
)
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// field to fill struct is in body
body := request.Body
var email Email
log.Println("Messaged received:", body)
err := json.Unmarshal([]byte(body), &email)
if err != nil {
log.Println("Error unmarshaling: ", err)
return events.APIGatewayProxyResponse{
Body: fmt.Sprintf("Error Unmarshaling body" + err.Error()),
StatusCode: 400,
}, nil
}
email.Message = strings.TrimSuffix(email.Message, "\n")
email.Message = strings.Map(func(r rune) rune {
if r == '\n' {
return ' '
}
return r
}, email.Message)
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-west-2")},
)
emailClient := ses.New(sess)
data := fmt.Sprintf("{ \"name\":\""+"%s"+"\", \"phone\": \""+"%s"+"\", \"email\": \""+"%s"+"\", \"message\": \""+"%s"+"\"}", email.Name, email.Phone, email.Email, email.Message)
input := &ses.SendTemplatedEmailInput{
Source: aws.String(Sender),
Template: aws.String("shoestringEmailTemplate"),
ReplyToAddresses: []*string{aws.String(email.Email)},
Destination: &ses.Destination{
ToAddresses: []*string{aws.String(Recipient)},
},
TemplateData: aws.String(data),
}
_, err = emailClient.SendTemplatedEmail(input)
if err != nil {
log.Println("Error sending email: ", err)
return events.APIGatewayProxyResponse{
Body: fmt.Sprintf("Error sending email" + err.Error()),
StatusCode: 400,
}, nil
}
return events.APIGatewayProxyResponse{
Body: "Success",
StatusCode: 200,
}, nil
}
func main() {
lambda.Start(Handler)
}