First implementation of send mail feature

This commit is contained in:
Andrea Fazzi 2019-12-05 15:08:37 +01:00
parent cd19270aa3
commit 502a451767
8 changed files with 58 additions and 10 deletions

View file

@ -8,6 +8,7 @@ dev:
killall main || echo "Process was not running." 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 down
docker-compose -f compose/docker-compose_outside_docker.yml up -d db 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 & go run main.go --config=config/config_outside_docker.yaml &
all: dockerized all: dockerized

View file

@ -21,6 +21,9 @@ services:
- ./sql:/docker-entrypoint-initdb.d - ./sql:/docker-entrypoint-initdb.d
env_file: env_file:
- db.env - db.env
smtp:
image: digiplant/fake-smtp
volumes: volumes:
db: db:

View file

@ -23,6 +23,11 @@ services:
- db.env - db.env
ports: ports:
- 3307:3306 - 3307:3306
smtp:
image: digiplant/fake-smtp
ports:
- "1025:25"
volumes: volumes:
db: db:

View file

@ -89,6 +89,7 @@ type ConfigT struct {
Password string Password string
From string From string
Cc string Cc string
Bcc string
} }
Sync struct { Sync struct {

1
go.mod
View file

@ -19,5 +19,6 @@ require (
github.com/sethvargo/go-password v0.1.3 github.com/sethvargo/go-password v0.1.3
github.com/smartystreets/goconvey v1.6.4 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/urfave/negroni v1.0.0 // 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 gopkg.in/yaml.v2 v2.2.4
) )

2
go.sum
View file

@ -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 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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/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/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.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View file

@ -1,28 +1,30 @@
package orm package orm
import ( import (
"crypto/tls"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
"time"
"git.andreafazzi.eu/andrea/oef/config"
"git.andreafazzi.eu/andrea/oef/renderer" "git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"gopkg.in/gomail.v2"
) )
type School struct { type School struct {
gorm.Model gorm.Model
Name string Name string
Email string Email string
Code string Code string
EmailSentDate *time.Time
UserID uint UserID uint
User *User User *User
Participants []*Participant Participants []*Participant
// SelectedElement map[uint]string `gorm:"-"`
// AllElements []*Element `gorm:"-"`
} }
func (model *School) GetID() uint { return model.ID } func (model *School) GetID() uint { return model.ID }
@ -64,12 +66,35 @@ func (model *School) AfterDelete(tx *gorm.DB) error {
return nil 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) { func (model *School) Create(args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" { if r.Method == "GET" {
school := new(School) school := new(School)
// if err := DB().Find(&school.AllContests).Error; err != nil {
// return nil, err
// }
return school, nil return school, nil
} else { } else {
school := new(School) school := new(School)

View file

@ -26,7 +26,17 @@
<h2 class="karmen-relation-header">Informazioni sulla scuola</h2> <h2 class="karmen-relation-header">Informazioni sulla scuola</h2>
<p>La denominazione della scuola è <strong>{{.Data.Name}}</strong>.</p> <p>La denominazione della scuola è <strong>{{.Data.Name}}</strong>.</p>
<p>Il codice meccanografico della scuola è <strong>{{.Data.Code}}</strong>.</p> <p>Il codice meccanografico della scuola è <strong>{{.Data.Code}}</strong>.</p>
<p>La mail istituzionale della scuola è <strong>{{.Data.Email}}</strong>.</p> <p>La mail istituzionale della scuola è <strong>{{.Data.Email}}</strong>.
<p>
{{if .Data.EmailSentDate}}
Una mail contenente le credenziali di accesso per l'iscrizione
degli studenti è stata inviata a questo indirizzo in data
<strong>{{.Data.EmailSentDate|prettyDate}}</strong> alle
ore <strong>{{.Data.EmailSentDate|convertTime}}</strong>.
{{else}}
A questo indirizzo non è stata inviata ancora nessuna mail.
{{end}}
</p>
<p>Il nome utente della scuola è {{if .Data.User}}<strong>{{.Data.User.Username}}</strong>{{end}}</p> <p>Il nome utente della scuola è {{if .Data.User}}<strong>{{.Data.User.Username}}</strong>{{end}}</p>
<p>La sua password è {{if .Data.User}}<strong>{{.Data.User.Password}}</strong>{{end}}</p> <p>La sua password è {{if .Data.User}}<strong>{{.Data.User.Password}}</strong>{{end}}</p>