diff options
Diffstat (limited to 'api/mail.go')
-rw-r--r-- | api/mail.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/api/mail.go b/api/mail.go new file mode 100644 index 0000000..e0f6ccb --- /dev/null +++ b/api/mail.go | |||
@@ -0,0 +1,103 @@ | |||
1 | package api | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "strings" | ||
6 | |||
7 | "github.com/matcornic/hermes" | ||
8 | "gopkg.in/gomail.v2" | ||
9 | ) | ||
10 | |||
11 | var MailTemplateEngine hermes.Hermes | ||
12 | |||
13 | func ConfigureMailTemplateEngine() { | ||
14 | var link string | ||
15 | if strings.Contains(CONFIG.Domain, "localhost") { | ||
16 | link = fmt.Sprintf("http://%s", CONFIG.Domain) | ||
17 | } else { | ||
18 | link = fmt.Sprintf("https://%s", CONFIG.Domain) | ||
19 | } | ||
20 | |||
21 | MailTemplateEngine = hermes.Hermes{ | ||
22 | Product: hermes.Product{ | ||
23 | Name: "CryptoPF", | ||
24 | Link: link, | ||
25 | Copyright: "Copyright © 2017 CryptoPF. All rights reserved.", | ||
26 | }, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | func SendResetPasswordMail(to, token string) error { | ||
31 | mail := hermes.Email{ | ||
32 | Body: hermes.Body{ | ||
33 | Name: to, | ||
34 | Intros: []string{ | ||
35 | "You have received this email because a password reset request for your CryptoPF account was received.", | ||
36 | }, | ||
37 | Actions: []hermes.Action{ | ||
38 | { | ||
39 | Instructions: "Click the button below to reset your password:", | ||
40 | Button: hermes.Button{ | ||
41 | Color: "#DC4D2F", | ||
42 | Text: "Reset your password", | ||
43 | Link: fmt.Sprintf("%s/change-password?token=%s", MailTemplateEngine.Product.Link, token), | ||
44 | }, | ||
45 | }, | ||
46 | }, | ||
47 | Outros: []string{ | ||
48 | "If you did not request a password reset, no further action is required on your part.", | ||
49 | }, | ||
50 | Signature: "Thanks", | ||
51 | }, | ||
52 | } | ||
53 | |||
54 | body, err := MailTemplateEngine.GenerateHTML(mail) | ||
55 | if err != nil { | ||
56 | return err | ||
57 | } | ||
58 | |||
59 | return SendEmail(to, "Password reset", body) | ||
60 | } | ||
61 | |||
62 | func SendConfirmationMail(to, token string) error { | ||
63 | mail := hermes.Email{ | ||
64 | Body: hermes.Body{ | ||
65 | Name: to, | ||
66 | Intros: []string{ | ||
67 | "Welcome to CryptoPF! We're very excited to have you on board.", | ||
68 | }, | ||
69 | Actions: []hermes.Action{ | ||
70 | { | ||
71 | Instructions: "To get started with CryptoPF, please click here:", | ||
72 | Button: hermes.Button{ | ||
73 | Text: "Confirm your account", | ||
74 | Link: fmt.Sprintf("%s/confirm?token=%s", MailTemplateEngine.Product.Link, token), | ||
75 | }, | ||
76 | }, | ||
77 | }, | ||
78 | Outros: []string{ | ||
79 | "Need help, or have questions? Just reply to this email, we'd love to help.", | ||
80 | }, | ||
81 | Signature: "Thanks", | ||
82 | }, | ||
83 | } | ||
84 | |||
85 | body, err := MailTemplateEngine.GenerateHTML(mail) | ||
86 | if err != nil { | ||
87 | return err | ||
88 | } | ||
89 | |||
90 | return SendEmail(to, "Confirm your email", body) | ||
91 | } | ||
92 | |||
93 | func SendEmail(to, subject, body string) error { | ||
94 | m := gomail.NewMessage() | ||
95 | m.SetAddressHeader("From", MAIL_CONFIG.AddressFrom, "CryptoPF") | ||
96 | m.SetAddressHeader("To", to, to) | ||
97 | m.SetHeader("Subject", subject) | ||
98 | m.SetBody("text/html", body) | ||
99 | |||
100 | d := gomail.NewPlainDialer(MAIL_CONFIG.SmtpAddress, 587, MAIL_CONFIG.Login, MAIL_CONFIG.Password) | ||
101 | |||
102 | return d.DialAndSend(m) | ||
103 | } | ||