diff --git a/mail/service.go b/mail/service.go
index 6a7e629..7cc444b 100644
--- a/mail/service.go
+++ b/mail/service.go
@@ -5,7 +5,7 @@ import (
"net/smtp"
"strings"
"time"
-
+ "text/template"
"github.com/sirupsen/logrus"
)
@@ -16,23 +16,39 @@ type Mail struct {
}
func (mail *Mail) BuildMessage() []byte {
- message := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n"
- message += fmt.Sprintf("From: Recruitment Automation System IITK<%s>\r\n", sender)
- message += fmt.Sprintf("Subject: %s | Recruitment Automation System\r\n", mail.Subject)
+
+ type TemplateData struct {
+ To []string
+ Subject string
+ Body string
+ }
+
+ var message strings.Builder
+
+ msg := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n"
+ msg += fmt.Sprintf("From: Recruitment Automation System IITK<%s>\r\n", sender)
+ msg += fmt.Sprintf("Subject: %s\r\n", mail.Subject)
// If mass mailing, BCC all the users
if len(mail.To) == 1 {
- message += fmt.Sprintf("To: %s\r\n\r\n", mail.To[0])
+ msg += fmt.Sprintf("To: %s\r\n\r\n", mail.To[0])
} else {
- message += fmt.Sprintf("To: Undisclosed Recipients<%s>\r\n\r\n", webteam)
+ msg += fmt.Sprintf("To: Undisclosed Recipients<%s>\r\n\r\n", webteam)
}
- message += strings.Replace(mail.Body, "\n", "
", -1)
- message += "
--
Recruitment Automation System
"
- message += "Indian Institute of Technology Kanpur
"
- message += "This is an auto-generated email. Please do not reply."
+ message.WriteString(msg)
+
+ tmpl := template.Must(template.New("Template").Parse(DefaultTemplate))
+ err := tmpl.Execute(&message, TemplateData{
+ Subject: mail.Subject,
+ Body: mail.Body,
+ })
+ if err != nil {
+ logrus.Errorf("Error executing email template: %v", err)
+ return nil
+ }
- return []byte(message)
+ return []byte(message.String())
}
func batchEmails(to []string, batch int) [][]string {
@@ -66,4 +82,4 @@ func Service(mailQueue chan Mail) {
time.Sleep(1 * time.Second)
}
}
-}
+}
\ No newline at end of file
diff --git a/mail/templates.go b/mail/templates.go
new file mode 100644
index 0000000..a5b49ef
--- /dev/null
+++ b/mail/templates.go
@@ -0,0 +1,151 @@
+package mail
+
+const (
+ DefaultTemplate = `
+
+
+
+
+
+
+
+
+
+
+
+ |
+
+
+
+ {{.Subject}}
+ {{.Body}}
+ This is an auto-generated email. Please do not reply.
+
+
+
+
+
+
+
+ |
+
+
+ |
+
+
+
+ |
+
+
+
+
+
+
+ Website | RAS Portal | PhD Portal
+ ©2024 Recruitment Automation System. Students' Placement Office, IIT Kanpur
All rights reserved.
+ |
+
+
+
+ |
+
+
+
+
+ `
+
+ // OTPTemplate and RecruitedTemplate are incomplete.
+
+ OTPTemplate = `
+
+
+
+`
+
+ RecruitedTemplate = `
+
+
+
+
+
+
+
+
+
+
+
+
+
+ |
+
+
+
+ Congratualtions!
+ You have been recruited for the position of Software Developer at SPO IITK.
+ This is an auto-generated email. Please do not reply.
+
+
+
+
+
+
+
+ |
+
+
+ |
+
+
+
+ |
+
+
+
+
+
+
+ Website | RAS Portal | PhD Portal
+ ©2024 Recruitment Automation System. Students' Placement Office, IIT Kanpur
All rights reserved.
+ |
+
+
+
+ |
+
+
+
+
+ `
+)
\ No newline at end of file