aboutsummaryrefslogtreecommitdiff
path: root/api/mail.go
blob: e0f6ccb9939cf5af933cae8c2ee5ce957b1efc41 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package api

import (
	"fmt"
	"strings"

	"github.com/matcornic/hermes"
	"gopkg.in/gomail.v2"
)

var MailTemplateEngine hermes.Hermes

func ConfigureMailTemplateEngine() {
	var link string
	if strings.Contains(CONFIG.Domain, "localhost") {
		link = fmt.Sprintf("http://%s", CONFIG.Domain)
	} else {
		link = fmt.Sprintf("https://%s", CONFIG.Domain)
	}

	MailTemplateEngine = hermes.Hermes{
		Product: hermes.Product{
			Name:      "CryptoPF",
			Link:      link,
			Copyright: "Copyright © 2017 CryptoPF. All rights reserved.",
		},
	}
}

func SendResetPasswordMail(to, token string) error {
	mail := hermes.Email{
		Body: hermes.Body{
			Name: to,
			Intros: []string{
				"You have received this email because a password reset request for your CryptoPF account was received.",
			},
			Actions: []hermes.Action{
				{
					Instructions: "Click the button below to reset your password:",
					Button: hermes.Button{
						Color: "#DC4D2F",
						Text:  "Reset your password",
						Link:  fmt.Sprintf("%s/change-password?token=%s", MailTemplateEngine.Product.Link, token),
					},
				},
			},
			Outros: []string{
				"If you did not request a password reset, no further action is required on your part.",
			},
			Signature: "Thanks",
		},
	}

	body, err := MailTemplateEngine.GenerateHTML(mail)
	if err != nil {
		return err
	}

	return SendEmail(to, "Password reset", body)
}

func SendConfirmationMail(to, token string) error {
	mail := hermes.Email{
		Body: hermes.Body{
			Name: to,
			Intros: []string{
				"Welcome to CryptoPF! We're very excited to have you on board.",
			},
			Actions: []hermes.Action{
				{
					Instructions: "To get started with CryptoPF, please click here:",
					Button: hermes.Button{
						Text: "Confirm your account",
						Link: fmt.Sprintf("%s/confirm?token=%s", MailTemplateEngine.Product.Link, token),
					},
				},
			},
			Outros: []string{
				"Need help, or have questions? Just reply to this email, we'd love to help.",
			},
			Signature: "Thanks",
		},
	}

	body, err := MailTemplateEngine.GenerateHTML(mail)
	if err != nil {
		return err
	}

	return SendEmail(to, "Confirm your email", body)
}

func SendEmail(to, subject, body string) error {
	m := gomail.NewMessage()
	m.SetAddressHeader("From", MAIL_CONFIG.AddressFrom, "CryptoPF")
	m.SetAddressHeader("To", to, to)
	m.SetHeader("Subject", subject)
	m.SetBody("text/html", body)

	d := gomail.NewPlainDialer(MAIL_CONFIG.SmtpAddress, 587, MAIL_CONFIG.Login, MAIL_CONFIG.Password)

	return d.DialAndSend(m)
}