better emails
This commit is contained in:
7 files changed
+396
-28
No files matched your search
@@ -12,8 +12,6 @@ import (
|
||||
"github.com/SowinskiBraeden/school-management-api/models"
|
||||
"github.com/howeyc/gopass"
|
||||
|
||||
"net/smtp"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
@@ -207,11 +205,11 @@ func Enroll(c *fiber.Ctx) error {
|
||||
student.AccountData.TempPassword = true
|
||||
|
||||
// Send student personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
subject := "Password Changed"
|
||||
receiver := student.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{student.PersonalData.Email}, message)
|
||||
if err != nil {
|
||||
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": student.PersonalData.FirstName, "password": tempPass}); err {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
@@ -342,15 +340,15 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
||||
teacher.AccountData.TempPassword = true
|
||||
|
||||
// Send teacher personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
subject := "Password Changed"
|
||||
receiver := teacher.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{teacher.PersonalData.Email}, message)
|
||||
if err != nil {
|
||||
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName, "password": tempPass}); err {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to teacners email",
|
||||
"message": "Could not send password to teachers email",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
@@ -430,15 +428,15 @@ func CreateAdmin(c *fiber.Ctx) error {
|
||||
admin.TempPassword = true
|
||||
|
||||
// Send admin personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
subject := "Password Changed"
|
||||
receiver := admin.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{admin.Email}, message)
|
||||
if err != nil {
|
||||
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": admin.FirstName, "password": tempPass}); err {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to admins email",
|
||||
"message": "Could not send password to students email",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
@@ -537,6 +535,21 @@ func StudentLogin(c *fiber.Ctx) error {
|
||||
|
||||
if localAccountDisabled || student.AccountData.AccountDisabled {
|
||||
cancel()
|
||||
|
||||
// Send student email warning of disabled account
|
||||
subject := "Account Disabled"
|
||||
receiver := student.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
if err := r.Send("./templates/accountDisabled.html", map[string]string{"username": student.PersonalData.FirstName}); err {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to students email",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Account is Disabled, contact an Admin",
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,79 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/smtp"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server string
|
||||
Port int
|
||||
Email string
|
||||
Password string
|
||||
}
|
||||
|
||||
var config Config = Config{
|
||||
Server: "smtp.gmail.com",
|
||||
Port: 587,
|
||||
Email: os.Getenv("SYSTEM_EMAIL"),
|
||||
Password: os.Getenv("SYSTEM_PASSWORD"),
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
from string
|
||||
to []string
|
||||
subject string
|
||||
body string
|
||||
}
|
||||
|
||||
const (
|
||||
MIME = "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
|
||||
)
|
||||
|
||||
func NewRequest(to []string, subject string) *Request {
|
||||
return &Request{
|
||||
to: to,
|
||||
subject: subject,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) parseTemplate(fileName string, data interface{}) error {
|
||||
t, err := template.ParseFiles(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
if err = t.Execute(buffer, data); err != nil {
|
||||
return err
|
||||
}
|
||||
r.body = buffer.String()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Request) sendMail() bool {
|
||||
body := "To: " + r.to[0] + "\r\nSubject: " + r.subject + "\r\n" + MIME + "\r\n" + r.body
|
||||
SMTP := fmt.Sprintf("%s:%d", config.Server, config.Port)
|
||||
if err := smtp.SendMail(SMTP, smtp.PlainAuth("", config.Email, config.Password, config.Server), config.Email, r.to, []byte(body)); err != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *Request) Send(templateName string, items interface{}) bool {
|
||||
err := r.parseTemplate(templateName, items)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return false
|
||||
}
|
||||
if ok := r.sendMail(); ok {
|
||||
log.Printf("Email has been sent to %s\n", r.to)
|
||||
return true
|
||||
} else {
|
||||
log.Printf("Failed to send the email to %s\n", r.to)
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/smtp"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -339,6 +338,12 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
// Alert email the password has changed
|
||||
subject := "Password Changed"
|
||||
receiver := student.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
r.Send("./templates/selfPasswordChanged.html", map[string]string{"username": student.PersonalData.FirstName})
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"success": true,
|
||||
"message": "successfully updated student password",
|
||||
@@ -413,12 +418,11 @@ func ResetStudentPassword(c *fiber.Ctx) error {
|
||||
defer cancel()
|
||||
|
||||
// Send student personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
subject := "Password Changed"
|
||||
receiver := student.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{student.PersonalData.Email}, message)
|
||||
if err != nil {
|
||||
cancel()
|
||||
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": student.PersonalData.FirstName, "password": tempPass}); err {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to students email",
|
||||
@@ -1263,6 +1267,18 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
subject := "Password Changed"
|
||||
receiver := teacher.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
if err := r.Send("./templates/selfPasswordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName}); err {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to teachers email",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"success": true,
|
||||
"message": "successfully updated teacher password",
|
||||
@@ -1335,13 +1351,12 @@ func ResetTeacherPassword(c *fiber.Ctx) error {
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
// Send student personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
// Send teacher personal email temp password
|
||||
subject := "Password Changed"
|
||||
receiver := teacher.PersonalData.Email
|
||||
r := NewRequest([]string{receiver}, subject)
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{teacher.PersonalData.Email}, message)
|
||||
if err != nil {
|
||||
cancel()
|
||||
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName, "password": tempPass}); err {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to teachers email",
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width"/>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Account Disabled</title>
|
||||
<style type="text/css">
|
||||
body{
|
||||
margin: 0 auto;
|
||||
padding: 0;
|
||||
min-width: 100%;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
table{
|
||||
margin: 50px 0 50px 0;
|
||||
}
|
||||
.header{
|
||||
height: 40px;
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.content{
|
||||
height: 100px;
|
||||
font-size: 18px;
|
||||
line-height: 30px;
|
||||
}
|
||||
.subscribe{
|
||||
height: 70px;
|
||||
text-align: center;
|
||||
}
|
||||
.button{
|
||||
text-align: center;
|
||||
font-size: 18px;
|
||||
font-family: sans-serif;
|
||||
font-weight: bold;
|
||||
padding: 0 30px 0 30px;
|
||||
}
|
||||
.button a{
|
||||
color: #FFFFFF;
|
||||
text-decoration: none;
|
||||
}
|
||||
.buttonwrapper{
|
||||
margin: 0 auto;
|
||||
}
|
||||
.footer{
|
||||
text-transform: uppercase;
|
||||
text-align: center;
|
||||
height: 40px;
|
||||
font-size: 14px;
|
||||
font-style: italic;
|
||||
}
|
||||
.footer a{
|
||||
color: #000000;
|
||||
text-decoration: none;
|
||||
font-style: normal;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body bgcolor="#009587">
|
||||
<table bgcolor="#FFFFFF" width="100%" border="0" cellspacing="0" cellpadding="0">
|
||||
<tr class="header">
|
||||
<td style="padding: 40px;">
|
||||
Uh Oh! Account Disabled!
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="content">
|
||||
<td style="padding:10px;">
|
||||
<p>
|
||||
Hi <b>{{ .username }}</b>, <br/>
|
||||
Your account has been disabled due to too many incorrect login attempts, please contact an admin to enable your account.
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="footer">
|
||||
<td style="padding: 40px;">
|
||||
This is an automated system email // DO NOT REPLY
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,95 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width"/>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Password has been changed</title>
|
||||
<style type="text/css">
|
||||
body{
|
||||
margin: 0 auto;
|
||||
padding: 0;
|
||||
min-width: 100%;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
table{
|
||||
margin: 50px 0 50px 0;
|
||||
}
|
||||
.header{
|
||||
height: 40px;
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.content{
|
||||
height: 100px;
|
||||
font-size: 18px;
|
||||
line-height: 30px;
|
||||
}
|
||||
.subscribe{
|
||||
height: 70px;
|
||||
text-align: center;
|
||||
}
|
||||
.button{
|
||||
text-align: center;
|
||||
font-size: 18px;
|
||||
font-family: sans-serif;
|
||||
font-weight: bold;
|
||||
padding: 0 30px 0 30px;
|
||||
}
|
||||
.button a{
|
||||
color: #FFFFFF;
|
||||
text-decoration: none;
|
||||
}
|
||||
.buttonwrapper{
|
||||
margin: 0 auto;
|
||||
}
|
||||
.footer{
|
||||
text-transform: uppercase;
|
||||
text-align: center;
|
||||
height: 40px;
|
||||
font-size: 14px;
|
||||
font-style: italic;
|
||||
}
|
||||
.footer a{
|
||||
color: #000000;
|
||||
text-decoration: none;
|
||||
font-style: normal;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body bgcolor="#009587">
|
||||
<table bgcolor="#FFFFFF" width="100%" border="0" cellspacing="0" cellpadding="0">
|
||||
<tr class="header">
|
||||
<td style="padding: 40px;">
|
||||
Password Change!
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="content">
|
||||
<td style="padding:10px;">
|
||||
<p>
|
||||
Hi <b>{{ .username }}</b>, <br/>
|
||||
Your password has been changed by the system.
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="subscribe">
|
||||
<td style="padding: 20px 0 0 0;">
|
||||
<h3>Your new password is:</h3>
|
||||
<table bgcolor="#009587" border="0" cellspacing="0" cellpadding="0" class="buttonwrapper">
|
||||
<tr>
|
||||
<td class="button" height="45">
|
||||
{{ .password }}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="footer">
|
||||
<td style="padding: 40px;">
|
||||
This is an automated system email // DO NOT REPLY
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,83 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width"/>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>Password has been changed</title>
|
||||
<style type="text/css">
|
||||
body{
|
||||
margin: 0 auto;
|
||||
padding: 0;
|
||||
min-width: 100%;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
table{
|
||||
margin: 50px 0 50px 0;
|
||||
}
|
||||
.header{
|
||||
height: 40px;
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
}
|
||||
.content{
|
||||
height: 100px;
|
||||
font-size: 18px;
|
||||
line-height: 30px;
|
||||
}
|
||||
.subscribe{
|
||||
height: 70px;
|
||||
text-align: center;
|
||||
}
|
||||
.button{
|
||||
text-align: center;
|
||||
font-size: 18px;
|
||||
font-family: sans-serif;
|
||||
font-weight: bold;
|
||||
padding: 0 30px 0 30px;
|
||||
}
|
||||
.button a{
|
||||
color: #FFFFFF;
|
||||
text-decoration: none;
|
||||
}
|
||||
.buttonwrapper{
|
||||
margin: 0 auto;
|
||||
}
|
||||
.footer{
|
||||
text-transform: uppercase;
|
||||
text-align: center;
|
||||
height: 40px;
|
||||
font-size: 14px;
|
||||
font-style: italic;
|
||||
}
|
||||
.footer a{
|
||||
color: #000000;
|
||||
text-decoration: none;
|
||||
font-style: normal;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body bgcolor="#009587">
|
||||
<table bgcolor="#FFFFFF" width="100%" border="0" cellspacing="0" cellpadding="0">
|
||||
<tr class="header">
|
||||
<td style="padding: 40px;">
|
||||
Password Change!
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="content">
|
||||
<td style="padding:10px;">
|
||||
<p>
|
||||
Hi <b>{{ .username }}</b>, <br/>
|
||||
Your password has been changed by you. If you did not change your password, contact an admin immediatley!
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr class="footer">
|
||||
<td style="padding: 40px;">
|
||||
This is an automated system email // DO NOT REPLY
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user