-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_handler.go
69 lines (61 loc) · 1.58 KB
/
mail_handler.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
package main
import (
"encoding/json"
"github.com/clanbeat/mailer/Godeps/_workspace/src/github.com/gin-gonic/gin"
"github.com/clanbeat/mailer/src/sender"
"net/http"
)
func postEmail(c *gin.Context) {
m := &sender.Mailable{}
if err := c.BindJSON(m); err != nil {
c.JSON(badRequest(err.Error()))
return
}
if err := sender.Send(m); err != nil {
c.JSON(badRequest(err.Error()))
return
}
c.AbortWithStatus(http.StatusNoContent)
}
func getEmail(c *gin.Context) {
name := c.Param("name")
m := make(map[string]interface{})
if !sender.TemplateExists(name) {
c.JSON(http.StatusNotFound, gin.H{"error_message": "template missing"})
return
}
if testData[name] != nil {
if err := json.Unmarshal(testData[name], &m); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error_message": "something went wrong"})
return
}
}
c.HTML(http.StatusOK, name, m)
}
func sendTest(c *gin.Context) {
name := c.Param("name")
email := c.Param("email")
if !sender.TemplateExists(name) {
c.JSON(http.StatusNotFound, gin.H{"error_message": "template missing"})
return
}
d := make(map[string]interface{})
if testData[name] != nil {
if err := json.Unmarshal(testData[name], &d); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error_message": "something went wrong"})
return
}
}
m := &sender.Mailable{
To: email,
From: "Clanbeat <[email protected]>",
Subject: "Test email",
Template: name,
Message: d,
}
if err := sender.Send(m); err != nil {
c.JSON(badRequest(err.Error()))
return
}
c.JSON(http.StatusOK, gin.H{"message": "email sent"})
}