Compare commits
No commits in common. "6873ad1c5610f733f8f6383cf22fad50e98b1087" and "998850b708b69b54ed08334efd6b8042bf69b1c2" have entirely different histories.
6873ad1c56
...
998850b708
@ -33,10 +33,7 @@ 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...")
|
|
||||||
listenForMail()
|
|
||||||
fmt.Printf("Starting application on port %s\n", portNumber)
|
fmt.Printf("Starting application on port %s\n", portNumber)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
@ -55,17 +52,6 @@ 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
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
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,7 +20,6 @@ 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,8 +36,6 @@ 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,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go-udemy-web-1/internal/models"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
@ -16,5 +15,4 @@ 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,8 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// User is the user model
|
// User is the user model
|
||||||
type User struct {
|
type User struct {
|
||||||
@ -60,11 +58,3 @@ 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
|
|
||||||
}
|
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/smtp"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
_ "github.com/jackc/pgx/v5/stdlib"
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
@ -18,11 +17,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
// Send test email
|
|
||||||
from := "me@here.com"
|
|
||||||
auth := smtp.PlainAuth("", from, "", "localhost")
|
|
||||||
_ = smtp.SendMail("localhost:1025", auth, from, []string{"you@there.com"}, []byte("Hello world"))
|
|
||||||
|
|
||||||
log.Println("Connected to database!")
|
log.Println("Connected to database!")
|
||||||
|
|
||||||
// test my connection
|
// test my connection
|
||||||
|
Loading…
x
Reference in New Issue
Block a user