Installing a mailer package and setting up a mail channel
This commit is contained in:
		| @ -33,7 +33,9 @@ func main() { | |||||||
| 		log.Fatal(err) | 		log.Fatal(err) | ||||||
| 	} | 	} | ||||||
| 	defer db.SQL.Close() | 	defer db.SQL.Close() | ||||||
|  | 	defer close(app.MailChan) | ||||||
|  |  | ||||||
|  | 	fmt.Println("Starting mail listener...") | ||||||
| 	fmt.Printf("Starting application on port %s\n", portNumber) | 	fmt.Printf("Starting application on port %s\n", portNumber) | ||||||
|  |  | ||||||
| 	srv := &http.Server{ | 	srv := &http.Server{ | ||||||
| @ -52,6 +54,19 @@ func run() (*driver.DB, error) { | |||||||
| 	gob.Register(models.Room{}) | 	gob.Register(models.Room{}) | ||||||
| 	gob.Register(models.Restriction{}) | 	gob.Register(models.Restriction{}) | ||||||
|  |  | ||||||
|  | 	mailChan := make(chan models.MailData) | ||||||
|  | 	app.MailChan = mailChan | ||||||
|  |  | ||||||
|  | 	listenForMail() | ||||||
|  |  | ||||||
|  | 	msg := models.MailData{ | ||||||
|  | 		To:      "John@do.do", | ||||||
|  | 		From:    "me@here.com", | ||||||
|  | 		Subject: "mail", | ||||||
|  | 		Content: "Hello, <strong>world</strong>", | ||||||
|  | 	} | ||||||
|  | 	app.MailChan <- msg | ||||||
|  |  | ||||||
| 	// change this to true when in production | 	// change this to true when in production | ||||||
| 	app.InProduction = false | 	app.InProduction = false | ||||||
|  |  | ||||||
|  | |||||||
							
								
								
									
										43
									
								
								cmd/web/send-mail.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								cmd/web/send-mail.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,43 @@ | |||||||
|  | package main | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"go-udemy-web-1/internal/models" | ||||||
|  | 	"log" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
|  | 	mail "github.com/xhit/go-simple-mail" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func listenForMail() { | ||||||
|  | 	go func() { | ||||||
|  | 		for { | ||||||
|  | 			msg := <-app.MailChan | ||||||
|  | 			sendMsg(msg) | ||||||
|  | 		} | ||||||
|  | 	}() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func sendMsg(m models.MailData) { | ||||||
|  | 	server := mail.NewSMTPClient() | ||||||
|  | 	server.Host = "localhost" | ||||||
|  | 	server.Port = 1025 // fake port | ||||||
|  | 	server.KeepAlive = false | ||||||
|  | 	server.ConnectTimeout = 10 * time.Second | ||||||
|  | 	server.SendTimeout = 10 * time.Second | ||||||
|  |  | ||||||
|  | 	client, err := server.Connect() | ||||||
|  | 	if err != nil { | ||||||
|  | 		errorLog.Println(err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	email := mail.NewMSG() | ||||||
|  | 	email.SetFrom(m.From).AddTo(m.To).SetSubject(m.Subject) | ||||||
|  | 	email.SetBody(mail.TextHTML, m.Content) | ||||||
|  |  | ||||||
|  | 	err = email.Send(client) | ||||||
|  | 	if err != nil { | ||||||
|  | 		log.Println(err) | ||||||
|  | 	} else { | ||||||
|  | 		log.Println("Email sent") | ||||||
|  | 	} | ||||||
|  | } | ||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							| @ -20,6 +20,7 @@ require ( | |||||||
| 	github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 | 	github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 | ||||||
| 	github.com/jackc/pgconn v1.14.3 | 	github.com/jackc/pgconn v1.14.3 | ||||||
| 	github.com/jackc/pgx/v5 v5.6.0 | 	github.com/jackc/pgx/v5 v5.6.0 | ||||||
|  | 	github.com/xhit/go-simple-mail v2.2.2+incompatible | ||||||
| ) | ) | ||||||
|  |  | ||||||
| require ( | require ( | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							| @ -36,6 +36,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P | |||||||
| github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||||||
| github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||||||
| github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||||||
|  | github.com/xhit/go-simple-mail v2.2.2+incompatible h1:Hm2VGfLqiQJ/NnC8SYsrPOPyVYIlvP2kmnotP4RIV74= | ||||||
|  | github.com/xhit/go-simple-mail v2.2.2+incompatible/go.mod h1:I8Ctg6vIJZ+Sv7k/22M6oeu/tbFumDY0uxBuuLbtU7Y= | ||||||
| golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= | golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= | ||||||
| golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= | golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= | ||||||
| golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= | golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| package config | package config | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"go-udemy-web-1/internal/models" | ||||||
| 	"html/template" | 	"html/template" | ||||||
| 	"log" | 	"log" | ||||||
|  |  | ||||||
| @ -15,4 +16,5 @@ type AppConfig struct { | |||||||
| 	InfoLog       *log.Logger | 	InfoLog       *log.Logger | ||||||
| 	ErrorLog      *log.Logger | 	ErrorLog      *log.Logger | ||||||
| 	Session       *scs.SessionManager | 	Session       *scs.SessionManager | ||||||
|  | 	MailChan      chan models.MailData | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| package models | package models | ||||||
|  |  | ||||||
| import "time" | import ( | ||||||
|  | 	"time" | ||||||
|  | ) | ||||||
|  |  | ||||||
| // User is the user model | // User is the user model | ||||||
| type User struct { | type User struct { | ||||||
| @ -58,3 +60,11 @@ type RoomRestriction struct { | |||||||
| 	ReservationID int | 	ReservationID int | ||||||
| 	RestrictionID int | 	RestrictionID int | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // MailData holds an email message | ||||||
|  | type MailData struct { | ||||||
|  | 	To      string | ||||||
|  | 	From    string | ||||||
|  | 	Subject string | ||||||
|  | 	Content string | ||||||
|  | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user