forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailjet.go
82 lines (67 loc) · 1.81 KB
/
mailjet.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
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample mailjet is a demonstration on sending an e-mail from App Engine flexible environment.
package main
import (
"fmt"
"log"
"net/http"
"os"
"google.golang.org/appengine"
)
// [START gae_flex_mailjet_config]
import "github.com/mailjet/mailjet-apiv3-go"
// [END gae_flex_mailjet_config]
func main() {
http.HandleFunc("/send", sendEmail)
appengine.Main()
}
var (
mailjetClient = mailjet.NewMailjetClient(
mustGetenv("MJ_APIKEY_PUBLIC"),
mustGetenv("MJ_APIKEY_PRIVATE"),
)
fromEmail = mustGetenv("MJ_FROM_EMAIL")
)
func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalf("%s environment variable not set.", k)
}
return v
}
// [START gae_flex_mailjet_send_email]
func sendEmail(w http.ResponseWriter, r *http.Request) {
to := r.FormValue("to")
if to == "" {
http.Error(w, "Missing 'to' parameter.", http.StatusBadRequest)
return
}
messagesInfo := []mailjet.InfoMessagesV31{
{
From: &mailjet.RecipientV31{
Email: fromEmail,
Name: "Mailjet Pilot",
},
To: &mailjet.RecipientsV31{
mailjet.RecipientV31{
Email: to,
Name: "passenger 1",
},
},
Subject: "Your email flight plan!",
TextPart: "Dear passenger, welcome to Mailjet! May the delivery force be with you!",
HTMLPart: "<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!",
},
}
messages := mailjet.MessagesV31{Info: messagesInfo}
resp, err := mailjetClient.SendMailV31(&messages)
if err != nil {
msg := fmt.Sprintf("Could not send mail: %v", err)
http.Error(w, msg, 500)
return
}
fmt.Fprintf(w, "%d email(s) sent!", len(resp.ResultsV31))
}
// [END gae_flex_mailjet_send_email]