From 502a451767dd0f938bb6613c552cf8f98cbafeb3 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 5 Dec 2019 15:08:37 +0100 Subject: [PATCH] First implementation of send mail feature --- Makefile | 1 + compose/docker-compose.yml | 3 ++ compose/docker-compose_outside_docker.yml | 5 +++ config/config.go | 1 + go.mod | 1 + go.sum | 2 ++ orm/school.go | 43 ++++++++++++++++++----- templates/schools_show.html.tpl | 12 ++++++- 8 files changed, 58 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 51bbf0dd..0d5d9783 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ dev: killall main || echo "Process was not running." docker-compose -f compose/docker-compose_outside_docker.yml down docker-compose -f compose/docker-compose_outside_docker.yml up -d db + docker-compose -f compose/docker-compose_outside_docker.yml up -d smtp go run main.go --config=config/config_outside_docker.yaml & all: dockerized diff --git a/compose/docker-compose.yml b/compose/docker-compose.yml index b4b8a1c7..7b5c65d5 100644 --- a/compose/docker-compose.yml +++ b/compose/docker-compose.yml @@ -21,6 +21,9 @@ services: - ./sql:/docker-entrypoint-initdb.d env_file: - db.env + + smtp: + image: digiplant/fake-smtp volumes: db: diff --git a/compose/docker-compose_outside_docker.yml b/compose/docker-compose_outside_docker.yml index 74bd5e57..272fb6f1 100644 --- a/compose/docker-compose_outside_docker.yml +++ b/compose/docker-compose_outside_docker.yml @@ -23,6 +23,11 @@ services: - db.env ports: - 3307:3306 + + smtp: + image: digiplant/fake-smtp + ports: + - "1025:25" volumes: db: diff --git a/config/config.go b/config/config.go index 1ec4edc2..04c1e18a 100644 --- a/config/config.go +++ b/config/config.go @@ -89,6 +89,7 @@ type ConfigT struct { Password string From string Cc string + Bcc string } Sync struct { diff --git a/go.mod b/go.mod index 97cda9ff..0b103387 100644 --- a/go.mod +++ b/go.mod @@ -19,5 +19,6 @@ require ( github.com/sethvargo/go-password v0.1.3 github.com/smartystreets/goconvey v1.6.4 // indirect github.com/urfave/negroni v1.0.0 // indirect + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index 94180ed7..fb9a5682 100644 --- a/go.sum +++ b/go.sum @@ -163,6 +163,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/orm/school.go b/orm/school.go index 6f1d3b13..6321825d 100644 --- a/orm/school.go +++ b/orm/school.go @@ -1,28 +1,30 @@ package orm import ( + "crypto/tls" "fmt" "net/http" "strings" + "time" + "git.andreafazzi.eu/andrea/oef/config" "git.andreafazzi.eu/andrea/oef/renderer" "github.com/jinzhu/gorm" + "gopkg.in/gomail.v2" ) type School struct { gorm.Model - Name string - Email string - Code string + Name string + Email string + Code string + EmailSentDate *time.Time UserID uint User *User Participants []*Participant - - // SelectedElement map[uint]string `gorm:"-"` - // AllElements []*Element `gorm:"-"` } func (model *School) GetID() uint { return model.ID } @@ -64,12 +66,35 @@ func (model *School) AfterDelete(tx *gorm.DB) error { return nil } +func (model *School) AfterCreate(tx *gorm.DB) error { + m := gomail.NewMessage() + m.SetHeader("From", config.Config.Smtp.From) + m.SetHeader("To", model.Email) + m.SetHeader("Bcc", config.Config.Smtp.Bcc) + m.SetBody("text/plain", "SMTP test message.") + + dialer := gomail.NewDialer( + config.Config.Smtp.Host, + config.Config.Smtp.Port, + config.Config.Smtp.Username, + config.Config.Smtp.Password, + ) + dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true} + + if err := dialer.DialAndSend(m); err != nil { + return err + } + + if err := tx.Model(model).Update("email_sent_date", time.Now()).Error; err != nil { + return err + } + + return nil +} + func (model *School) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) { if r.Method == "GET" { school := new(School) - // if err := DB().Find(&school.AllContests).Error; err != nil { - // return nil, err - // } return school, nil } else { school := new(School) diff --git a/templates/schools_show.html.tpl b/templates/schools_show.html.tpl index 60c374c8..cfc92db9 100644 --- a/templates/schools_show.html.tpl +++ b/templates/schools_show.html.tpl @@ -26,7 +26,17 @@

Informazioni sulla scuola

La denominazione della scuola è {{.Data.Name}}.

Il codice meccanografico della scuola è {{.Data.Code}}.

-

La mail istituzionale della scuola è {{.Data.Email}}.

+

La mail istituzionale della scuola è {{.Data.Email}}. +

+ {{if .Data.EmailSentDate}} + Una mail contenente le credenziali di accesso per l'iscrizione + degli studenti è stata inviata a questo indirizzo in data + {{.Data.EmailSentDate|prettyDate}} alle + ore {{.Data.EmailSentDate|convertTime}}. + {{else}} + A questo indirizzo non è stata inviata ancora nessuna mail. + {{end}} +

Il nome utente della scuola è {{if .Data.User}}{{.Data.User.Username}}{{end}}

La sua password è {{if .Data.User}}{{.Data.User.Password}}{{end}}