Skip to content

Commit e73ce13

Browse files
athos & foil. create command to send expiration mails (#28)
* athos & foil. create command to send expiration mails Co-authored-by: Giacomo Sanchietti <giacomo.sanchietti@nethesis.it>
1 parent 6dcb8dd commit e73ce13

File tree

10 files changed

+1028
-16
lines changed

10 files changed

+1028
-16
lines changed

athos/configuration/configuration.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import (
3131
)
3232

3333
type Notifications struct {
34-
PortalUrl string `json:"portal_url"`
35-
HelpUrl string `json:"help_url"`
36-
DocsUrl string `json:"docs_url"`
37-
PortalTitle string `json:"portal_title"`
38-
Email struct {
34+
PortalUrl string `json:"portal_url"`
35+
HelpUrl string `json:"help_url"`
36+
DocsUrl string `json:"docs_url"`
37+
CommunityUrl string `json:"community_url"`
38+
PortalTitle string `json:"portal_title"`
39+
Email struct {
3940
From string `json:"from"`
4041
SMTPHost string `json:"smtp_host"`
4142
SMTPPort int `json:"smtp_port"`
@@ -53,25 +54,25 @@ type Configuration struct {
5354
Password string `json:"password"`
5455
} `json:"database"`
5556
Redis struct {
56-
Host string `json:"host"`
57-
Port string `json:"port"`
57+
Host string `json:"host"`
58+
Port string `json:"port"`
5859
} `json:"redis"`
59-
Cors struct {
60+
Cors struct {
6061
Headers []string `json:"headers"`
6162
Origins []string `json:"origins"`
6263
Methods []string `json:"methods"`
6364
} `json:"cors"`
6465
Auth0 struct {
65-
Domain string `json:"domain"`
66-
Audience string `json:"audience"`
66+
Domain string `json:"domain"`
67+
Audience string `json:"audience"`
6768
} `json:"auth0"`
6869
PayPal struct {
6970
ClientID string `json:"client_id"`
7071
ClientSecret string `json:"client_secret"`
7172
Sandbox bool `json:"sandbox"`
7273
} `json:"paypal"`
7374
Log struct {
74-
Level string `json:"level"`
75+
Level string `json:"level"`
7576
} `json:"log"`
7677
Billing struct {
7778
Country string `json:"country"`

athos/notifications/alert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func AlertNotification(alert models.Alert, isNew bool) {
3434
switch x := alert.System.Notification["emails"].(type) {
3535
case []interface{}:
3636
for _, e := range x {
37-
MailNotification(e.(string), alert, isNew)
37+
MailNotificationAlert(e.(string), alert, isNew)
3838
}
3939
default:
4040
}

athos/notifications/mail.go

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ package notifications
2323

2424
import (
2525
"fmt"
26+
"strconv"
27+
"strings"
2628
"time"
2729

2830
gomail "gopkg.in/gomail.v2"
2931

3032
"github.com/cbroglie/mustache"
3133
"github.com/nethesis/dartagnan/athos/configuration"
3234
"github.com/nethesis/dartagnan/athos/models"
35+
"github.com/nethesis/dartagnan/athos/utils"
3336
"github.com/nicksnyder/go-i18n/i18n"
3437
)
3538

@@ -94,7 +97,76 @@ func alertTextBody(alert models.Alert) string {
9497
return text
9598
}
9699

97-
func MailNotification(address string, alert models.Alert, isNew bool) bool {
100+
func expireHtmlBody(system models.System, period string) string {
101+
values := make(map[string]string)
102+
T, _ := i18n.Tfunc("en-US", "en-US", "en-US")
103+
104+
values["hello"] = T("hello")
105+
values["year"] = strconv.Itoa(time.Now().Year())
106+
107+
values["title"] = configuration.Config.Notifications.PortalTitle
108+
values["urlComm"] = configuration.Config.Notifications.CommunityUrl
109+
values["urlDocs"] = configuration.Config.Notifications.DocsUrl
110+
values["urlHelp"] = configuration.Config.Notifications.HelpUrl
111+
112+
values["systemUUID"] = T("server_uuid")
113+
values["serverName"] = system.UUID
114+
values["serverDetails"] = fmt.Sprintf(fmt.Sprintf("%s/servers/%d", configuration.Config.Notifications.PortalUrl, system.ID))
115+
116+
values["footerMessage"] = T("footer_message")
117+
118+
renderFile := ""
119+
if system.Subscription.SubscriptionPlan.Code == "trial" {
120+
values["message"] = T(period + "_trial")
121+
values["serverDetailsMessage"] = T("server_link")
122+
123+
renderFile = "templates/expiration-template-trial.mustache"
124+
} else {
125+
values["message"] = T(period+"_others", map[string]interface{}{
126+
"Subscription": system.Subscription.SubscriptionPlan.Name,
127+
})
128+
values["serverDetailsMessage"] = T("server_link_renew")
129+
values["ownerName"] = utils.GetBillingInfo(system.CreatorID).Name
130+
131+
renderFile = "templates/expiration-template-others.mustache"
132+
}
133+
134+
output, err := mustache.RenderFile(renderFile, values)
135+
if err != nil {
136+
fmt.Println(err)
137+
}
138+
139+
return output
140+
}
141+
142+
func expireTextBody(system models.System, period string) string {
143+
T, _ := i18n.Tfunc("en-US", "en-US", "en-US")
144+
145+
text := fmt.Sprintf("***** %s ***** \n\n", configuration.Config.Notifications.PortalTitle)
146+
text += fmt.Sprintf("%s: %s\n", T("server_uuid"), system.UUID)
147+
text += fmt.Sprintf("%s: %s\n", T("subscription"), system.Subscription.SubscriptionPlan.Name)
148+
149+
message := ""
150+
if system.Subscription.SubscriptionPlan.Code == "trial" {
151+
message = T(period + "_trial_plain")
152+
153+
} else {
154+
message = T(period+"_others_plain", map[string]interface{}{
155+
"Subscription": system.Subscription.SubscriptionPlan.Name,
156+
"Name": utils.GetBillingInfo(system.CreatorID).Name,
157+
})
158+
}
159+
160+
text += "\n" + message
161+
text += fmt.Sprintf("\n%s\n", fmt.Sprintf(fmt.Sprintf("%s/servers/%d", configuration.Config.Notifications.PortalUrl, system.ID)))
162+
163+
text += fmt.Sprintf("\n%s: %s\n", "Blog", configuration.Config.Notifications.CommunityUrl)
164+
text += fmt.Sprintf("%s: %s\n", "Support", strings.Split(configuration.Config.Notifications.HelpUrl, ":")[1])
165+
text += fmt.Sprintf("%s: %s\n", "Docs", configuration.Config.Notifications.DocsUrl)
166+
return text
167+
}
168+
169+
func MailNotificationAlert(address string, alert models.Alert, isNew bool) bool {
98170
i18n.MustLoadTranslationFile("templates/en-US-alert.json")
99171
T, _ := i18n.Tfunc("en-US", "en-US", "en-US")
100172
status := true
@@ -127,3 +199,32 @@ func MailNotification(address string, alert models.Alert, isNew bool) bool {
127199

128200
return status
129201
}
202+
203+
func MailNotificationExpire(address string, system models.System, period string) bool {
204+
i18n.MustLoadTranslationFile("templates/en-US-alert.json")
205+
T, _ := i18n.Tfunc("en-US", "en-US", "en-US")
206+
status := true
207+
m := gomail.NewMessage()
208+
m.SetHeader("From", configuration.Config.Notifications.Email.From)
209+
m.SetHeader("To", address)
210+
subject := fmt.Sprintf("[%s][%s] %s", T("expiration"), system.Subscription.SubscriptionPlan.Name, T(system.UUID))
211+
m.SetHeader("Subject", subject)
212+
m.SetBody("text/plain", expireTextBody(system, period))
213+
m.SetHeader("Message-Id", fmt.Sprintf("%d@dartagnan.project", time.Now().UnixNano()))
214+
215+
m.AddAlternative("text/html", expireHtmlBody(system, period))
216+
217+
d := gomail.NewDialer(
218+
configuration.Config.Notifications.Email.SMTPHost,
219+
configuration.Config.Notifications.Email.SMTPPort,
220+
configuration.Config.Notifications.Email.SMTPUser,
221+
configuration.Config.Notifications.Email.SMTPPassword,
222+
)
223+
224+
// send the email
225+
if err := d.DialAndSend(m); err != nil {
226+
status = false
227+
}
228+
229+
return status
230+
}

athos/templates/en-US-alert.json

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
[{
1+
[
2+
{
23
"id": "Alert report",
34
"translation": "Alert report"
45
},
@@ -93,5 +94,73 @@
9394
{
9495
"id": "alert_nut",
9596
"translation": "UPS on battery"
97+
},
98+
{
99+
"id": "hello",
100+
"translation": "Hello"
101+
},
102+
{
103+
"id": "expiration",
104+
"translation": "EXPIRATION"
105+
},
106+
{
107+
"id": "subscription",
108+
"translation": "Subscription"
109+
},
110+
{
111+
"id": "2w_trial",
112+
"translation": "<br>thank you for joining our Subscription trial program.<br><br>Your trial subscription will expire in <b>2 weeks</b>, we hope that everything is still up and running!<br><br>If you need help, remember to join our vibrant <a target=\"blank\" href=\"https://community.nethserver.org\">Community</a><br><br>· Would you like to secure your installation thanks to stable repositories?<br>· Do you want to sponsor the NethServer project?<br><br>Click on the button below and buy a subscription:"
113+
},
114+
{
115+
"id": "1w_trial",
116+
"translation": "<br>your trial subscription will expire in <b>1 week</b>.<br><br>If you are enjoining it and you want to still have access to stable repositories, buy a subscription by clicking the button below:"
117+
},
118+
{
119+
"id": "1d_trial",
120+
"translation": "<br>your trial subscription will expire <b>tomorrow</b>!<br><br>After subscription expiration, the server <b>will not be able</b> to download updates.<br><br>If you want to leave the subscription program, remember to unregister your machine following the <a href=\"https://docs.nethserver.org/en/v7/subscription.html#removing-a-subscription\">documentation</a>.<br><br>Otherwise, if you are still interested in accessing the stable repositories, buy a subscription now by clicking the button below:"
121+
},
122+
{
123+
"id": "1w_others",
124+
"translation": "<br>your <b>{{.Subscription}}</b> subscription will expire in <b>1 week</b>.<br><br>If you are enjoying it and you want to still have access to stable repositories, renew your subscription clicking the button below:"
125+
},
126+
{
127+
"id": "1d_others",
128+
"translation": "<br>your <b>{{.Subscription}}</b> subscription will expire <b>tomorrow</b>!<br><br>After subscription expiration, the server <b>will not be able</b> to download updates.<br><br>If you want to leave the subscription program, remember to unregister your machine following the <a href=\"https://docs.nethserver.org/en/v7/subscription.html#removing-a-subscription\">documentation</a>.<br><br>Otherwise, if you are still interested in accessing the stable repositories, renew your subscription now by clicking the button below:"
129+
},
130+
{
131+
"id": "2w_trial_plain",
132+
"translation": "Hi,\nthank you for joining our Subscription trial program.\n\nYour trial subscription will expire in 2 weeks, we hope that everything is still up and running!\n\nIf you need help, remember to join our vibrant community at: https://community.nethserver.org\n\n· Would you like to secure your installation thanks to stable repositories?\n· Do you want to sponsor the NethServer project?\n\nClick on the link below and buy a subscription:"
133+
},
134+
{
135+
"id": "1w_trial_plain",
136+
"translation": "Hi,\nyour trial subscription will expire in 1 week.\n\nIf you are enjoining it and you want to still have access to stable repositories, buy a subscription by clicking the link below:"
137+
},
138+
{
139+
"id": "1d_trial_plain",
140+
"translation": "Hi,\nyour trial subscription will expire tomorrow!\n\nAfter subscription expiration, the server will not be able to download updates.\n\nIf you want to leave the subscription program, remember to unregister your machine following the documentation at: https://docs.nethserver.org/en/v7/subscription.html#removing-a-subscription.\n\nOtherwise, if you are still interested in accessing the stable repositories, buy a subscription now by clicking the link below:"
141+
},
142+
{
143+
"id": "1w_others_plain",
144+
"translation": "Hi {{.Name}},\nyour {{.Subscription}} subscription will expire in 1 week.\n\nIf you are enjoining it and you want to still have access to stable repositories, renew your subscription by clicking the link below:"
145+
},
146+
{
147+
"id": "1d_others_plain",
148+
"translation": "Hi {{.Name}},\nyour {{.Subscription}} subscription will expire tomorrow!\n\nAfter subscription expiration, the server will not be able to download updates.\n\nIf you want to leave the subscription program, remember to unregister your machine following the documentation at: https://docs.nethserver.org/en/v7/subscription.html#removing-a-subscription.\n\nOtherwise, if you are still interested in accessing the stable repositories, renew your subscription now by clicking the link below:"
149+
},
150+
{
151+
"id": "server_link",
152+
"translation": "Upgrade your plan"
153+
},
154+
{
155+
"id": "server_link_renew",
156+
"translation": "Renew your plan"
157+
},
158+
{
159+
"id": "server_uuid",
160+
"translation": "System"
161+
},
162+
{
163+
"id": "footer_message",
164+
"translation": "Still convinced to not buy a subscription? <a href=\"mailto:support@nethserver.com\">Tell us</a> why and how can we improve our offer!<br><br>Thank you,<br>NethServer Subscription team"
96165
}
97-
]
166+
]

0 commit comments

Comments
 (0)