Skip to content

Commit

Permalink
[add] Added Email functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
goodylili committed Oct 10, 2023
1 parent d210200 commit de6f01e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 45 deletions.
30 changes: 0 additions & 30 deletions pkg/auth/login/login.go
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
package login

import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)

func login(c *gin.Context) {
// Assume the user is authenticated
session := sessions.Default(c)
session.Set("authenticated", true)
err := session.Save()
if err != nil {
return
}
c.JSON(200, gin.H{"message": "authenticated"})
}

func secureEndpoint(c *gin.Context) {
if !isLoggedIn(c) {
c.JSON(401, gin.H{"message": "unauthorized"})
return
}
c.JSON(200, gin.H{"message": "Hello, authorized user!"})
}

func isLoggedIn(c *gin.Context) bool {
session := sessions.Default(c)
authenticated := session.Get("authenticated")
return authenticated != nil && authenticated.(bool)
}
33 changes: 18 additions & 15 deletions pkg/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ type Message struct {
}

type Email struct {
From string
To string
Subject string
Body string
From string
Recipient string
Subject string
Body string
}

func LoadEmail(header, username, introduction, content, url, action string) (bytes.Buffer, error) {
func LoadEmail(message Message) (bytes.Buffer, error) {
// Load and parse the email template
tmpl, err := template.ParseFiles("../static/index.html")
if err != nil {
_ = fmt.Errorf("error loading email template: %v", err)
}

data := Message{
Header: header,
UsersName: username,
Introduction: introduction,
Content: content,
URL: url,
Action: action,
Header: message.Header,
UsersName: message.UsersName,
Introduction: message.Introduction,
Content: message.Content,
URL: message.URL,
Action: message.Action,
}

var Body bytes.Buffer
Expand All @@ -51,7 +51,7 @@ func LoadEmail(header, username, introduction, content, url, action string) (byt
return Body, nil
}

func SendEmail(to, subject string, loadedMail bytes.Buffer) {
func SendEmail(Recipient, subject string, loadedMail bytes.Buffer) error {
envVars := []string{"SMTP_HOST", "SMTP_PORT", "SMTP_USER", "SMTP_PASS"}
for _, envVar := range envVars {
if os.Getenv(envVar) == "" {
Expand All @@ -70,9 +70,12 @@ func SendEmail(to, subject string, loadedMail bytes.Buffer) {
"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n" +
loadedMail.String())

err := smtp.SendMail(smtpHost+":"+smtpPort, smtpAuth, smtpUser, []string{to}, msg)
err := smtp.SendMail(smtpHost+":"+smtpPort, smtpAuth, smtpUser, []string{Recipient}, msg)
if err != nil {
_ = fmt.Errorf("smtp error while sending email: %s", err)
return
err = fmt.Errorf("smtp error while sending email: %s", err)
return err
}

return nil

}
46 changes: 46 additions & 0 deletions pkg/email/mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package email

// SendWelcomeMessage sends a welcome message to a new user
func SendWelcomeMessage(username, url, recipient string) error {
message := Message{
Header: "My App Africa",
UsersName: username,
Introduction: "Hey" + username + ", Welcome to My App Africa",
Content: "Thank you for signing up recipient My App Africa. Please click the link below recipient activate your account.",
URL: url,
Action: "Activate Account",
}
body, err := LoadEmail(message)
if err != nil {
return err
}

err = SendEmail(recipient, "Welcome to My App Africa", body)
if err != nil {
return err
}

return nil
}

// SendPassWordResetMessage sends a password reset message to a user
func SendPassWordResetMessage(username, url, recipient string) error {
message := Message{
Header: "My App Africa",
UsersName: username,
Introduction: "Hey" + username,
Content: "You have requested to reset your password. Please click the link below to reset your password.",
URL: url,
Action: "Reset Password",
}
body, err := LoadEmail(message)
if err != nil {
return err
}

err = SendEmail(recipient, "Welcome to My App Africa", body)
if err != nil {
return err
}
return nil
}

0 comments on commit de6f01e

Please sign in to comment.