-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the ability to send mails via a cronjob
- Loading branch information
1 parent
d0ed986
commit 8050fe0
Showing
8 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<h1>Feedback via TumCampusApp:</h1> | ||
|
||
{{ if .Feedback.Valid }} | ||
<blockquote> | ||
{{ .Feedback.String }} | ||
</blockquote> | ||
{{ end }} | ||
|
||
<table> | ||
<tr> | ||
<th>Inforation type</th> | ||
<th>Details</th> | ||
</tr> | ||
{{ if .Latitude.Valid }} | ||
<tr> | ||
<th>Nutzer-Standort</th> | ||
<td> | ||
<a href="https://www.google.com/maps/search/?api=1&query={{ .Latitude.Float64 }},{{ .Longitude.Float64 }}"> | ||
latitude: {{ .Latitude.Float64 }}, longitude: {{ .Longitude.Float64 }} | ||
</a> | ||
</td> | ||
</tr> | ||
{{ end }} | ||
<tr> | ||
<th>OS-Version</th> | ||
<td>{{ if .OsVersion.Valid }}{{.OsVersion.String }}{{else}}unknown{{end}}</td> | ||
</tr> | ||
<tr> | ||
<th>App-Version</th> | ||
<td>{{ if .AppVersion.Valid }}{{.APPVersion.String }}{{else}}unknown{{end}}</td> | ||
</tr> | ||
</table> | ||
|
||
{{ if .ImageCount }} | ||
<h2>Fotos:</h2><br/> | ||
<ol> | ||
{{ range $val := Iterate .ImageCount }} | ||
<li> | ||
<a href="https://app.tum.de/File/feedback/{{ $.Id }}/{{ $val }}.png">Foto {{ $val }}</a> | ||
</li> | ||
{{ end }} | ||
</ol> | ||
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Feedback via TumCampusApp: | ||
|
||
{{- if .Feedback.Valid }} | ||
{{ .Feedback.String }} | ||
{{- end }} | ||
|
||
{{- if .Latitude.Valid }} | ||
- Nutzer-Standort: | ||
latitude: {{ .Latitude.Float64 }}, longitude: {{ .Longitude.Float64 }} | ||
https://www.google.com/maps/search/?api=1&query={{ .Latitude.Float64 }},{{ .Longitude.Float64 }} | ||
{{- end }} | ||
- OS-Version: {{ if .OsVersion.Valid }}{{.OsVersion.String }}{{else}}unknown{{end}} | ||
- App-Version: {{ if .AppVersion.Valid }}{{.APPVersion.String }}{{else}}unknown{{end}} | ||
|
||
{{ if .ImageCount }} | ||
Fotos: | ||
{{- range $val := Iterate .ImageCount }} | ||
- Foto {{ $val }}: https://app.tum.de/File/feedback/{{ $.Id }}/{{ $val }}.png | ||
{{- end -}} | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package cron | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"github.com/TUM-Dev/Campus-Backend/server/model" | ||
log "github.com/sirupsen/logrus" | ||
"gopkg.in/gomail.v2" | ||
htmlTemplate "html/template" | ||
"os" | ||
"strconv" | ||
textTemplate "text/template" | ||
"time" | ||
) | ||
import _ "embed" | ||
|
||
// Iterate is necessary, as go otherwise cannot count up in a for loop inside templates | ||
func Iterate(count int32) []int32 { | ||
var Items []int32 | ||
var i int32 | ||
for i = 0; i < count; i++ { | ||
Items = append(Items, i) | ||
} | ||
return Items | ||
} | ||
|
||
//go:embed emailTemplates/feedbackBody.gohtml | ||
var htmlFeedbackBody string | ||
|
||
//go:embed emailTemplates/feedbackBody.txt.tmpl | ||
var txtFeedbackBody string | ||
|
||
func (c *CronService) feedbackEmailCron() error { | ||
var results []model.Feedback | ||
if err := c.db.Find(&results, "processed = false").Scan(&results).Error; err != nil { | ||
log.WithError(err).Fatal("could not get unprocessed feedback") | ||
return err | ||
} | ||
funcMap := textTemplate.FuncMap{"Iterate": Iterate} | ||
parsedHtmlBody, err := htmlTemplate.New("htmlFeedbackBody").Funcs(funcMap).Parse(htmlFeedbackBody) | ||
if err != nil { | ||
log.WithError(err).Fatal("htmlFeedbackBody is not a valid template") | ||
return err | ||
} | ||
parsedTxtBody, err := textTemplate.New("txtFeedbackBody").Funcs(funcMap).Parse(txtFeedbackBody) | ||
if err != nil { | ||
log.WithError(err).Fatal("txtFeedbackBody is not a valid template") | ||
return err | ||
} | ||
|
||
smtpPort, err := strconv.Atoi(os.Getenv("SMTP_PORT")) | ||
if err != nil { | ||
log.WithError(err).Fatal("SMTP_PORT is not an integer") | ||
return err | ||
} | ||
d := gomail.NewDialer(os.Getenv("SMTP_URL"), smtpPort, os.Getenv("SMTP_USERNAME"), os.Getenv("SMTP_PASSWORD")) | ||
d.TLSConfig = &tls.Config{InsecureSkipVerify: true} | ||
for i, feedback := range results { | ||
m := gomail.NewMessage() | ||
// set message-headers | ||
m.SetAddressHeader("From", os.Getenv("SMTP_USERNAME"), "TUM Campus App") | ||
if feedback.Receiver.Valid { | ||
m.SetHeader("To", feedback.Receiver.String) | ||
} else { | ||
m.SetHeader("To", "[email protected]") | ||
} | ||
if feedback.ReplyTo.Valid { | ||
m.SetHeader("Reply-To", feedback.ReplyTo.String) | ||
} | ||
if feedback.Timestamp.Valid { | ||
m.SetDateHeader("Date", feedback.Timestamp.Time) | ||
} else { | ||
m.SetDateHeader("Date", time.Time{}) | ||
} | ||
m.SetHeader("Subject", "Feedback via Tum Campus App") | ||
|
||
// attach a body | ||
var txtBodyBuffer bytes.Buffer | ||
if err := parsedTxtBody.Execute(&txtBodyBuffer, feedback); err != nil { | ||
return err | ||
} | ||
m.SetBody("text/plain", txtBodyBuffer.String()) | ||
|
||
var htmlBodyBuffer bytes.Buffer | ||
if err := parsedHtmlBody.Execute(&htmlBodyBuffer, feedback); err != nil { | ||
return err | ||
} | ||
m.AddAlternative("text/html", htmlBodyBuffer.String()) | ||
|
||
// send mail | ||
if err := d.DialAndSend(m); err != nil { | ||
log.WithError(err).Error("could not send mail") | ||
continue | ||
} | ||
log.Trace("sending feedback %d to %s successfull", i, feedback.Receiver) | ||
|
||
// prevent the message being send the next time around | ||
if err := c.db.Find(model.Feedback{}, "id = ?", feedback.Id).Update("processed", "true").Error; err != nil { | ||
log.WithError(err).Error("could not prevent mail from being send again") | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters