Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change mail settings for smtps #167

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3.5"

services:
database:
image: postgres
image: postgres:14.7
restart: always
environment:
- POSTGRES_USER=postgres
Expand Down
115 changes: 73 additions & 42 deletions mail/service.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mail

import (
"crypto/tls"
"fmt"
"net/smtp"
"strings"
"time"
"text/template"

"github.com/sirupsen/logrus"
)

Expand All @@ -16,41 +17,23 @@ type Mail struct {
}

func (mail *Mail) BuildMessage() []byte {

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)
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)

// If mass mailing, BCC all the users
if len(mail.To) == 1 {
msg += fmt.Sprintf("To: %s\r\n\r\n", mail.To[0])
message += fmt.Sprintf("To: %s\r\n\r\n", mail.To[0])
} else {
msg += fmt.Sprintf("To: Undisclosed Recipients<%s>\r\n\r\n", webteam)
message += fmt.Sprintf("To: Undisclosed Recipients<%s>\r\n\r\n", webteam)
}

message.WriteString(msg)
message += strings.Replace(mail.Body, "\n", "<br>", -1)
message += "<br><br>--<br>Recruitment Automation System<br>"
message += "Indian Institute of Technology Kanpur<br><br>"
message += "<small>This is an auto-generated email. Please do not reply.</small>"

bodyWithLineBreaks := strings.ReplaceAll(mail.Body, "\n", "<br>")

tmpl := template.Must(template.New("Template").Parse(DefaultTemplate))
err := tmpl.Execute(&message, TemplateData{
Subject: mail.Subject,
Body: bodyWithLineBreaks,
})
if err != nil {
logrus.Errorf("Error executing email template: %v", err)
return nil
}

return []byte(message.String())
return []byte(message)
}

func batchEmails(to []string, batch int) [][]string {
Expand All @@ -69,19 +52,67 @@ func batchEmails(to []string, batch int) [][]string {
}

func Service(mailQueue chan Mail) {
addr := fmt.Sprintf("%s:%s", host, port)
auth := smtp.PlainAuth("", user, pass, host)

for mail := range mailQueue {
message := mail.BuildMessage()
to := append(mail.To, webteam)
batches:= batchEmails(to, batch);
for _, emailBatch:= range batches {
if err := smtp.SendMail(addr, auth, sender, emailBatch, message); err != nil {
logrus.Errorf("Error sending mail: %v", emailBatch)
logrus.Errorf("Error: %v", err)
addr := fmt.Sprintf("%s:%s", host, port)
auth := smtp.PlainAuth("", user, pass, host)

tlsConfig := &tls.Config{
InsecureSkipVerify: true, // Change this to verify server certificate in production
}
conn, err := tls.Dial("tcp", addr, tlsConfig)
if err != nil {
logrus.Errorf("Error dialing SMTPS server: %v", err)
}

client, err := smtp.NewClient(conn, host)
if err != nil {
logrus.Errorf("Error creating SMTP client: %v", err)
}


if err := client.Auth(auth); err != nil {
logrus.Errorf("Error authenticating: %v", err)
}

for mail := range mailQueue {
message := mail.BuildMessage()
to := append(mail.To, webteam)
batches := batchEmails(to, batch)
for _, emailBatch := range batches {

// if err := client.StartTLS(tlsConfig); err != nil {
// logrus.Errorf("Error starting TLS: %v", err)
// continue
// }
if err := client.Mail(sender); err != nil {
logrus.Errorf("Error setting sender: %v", err)
}

for _, recipient := range emailBatch {
if err := client.Rcpt(recipient); err != nil {
logrus.Errorf("Error setting recipient %s: %v", recipient, err)
continue
}
}

// Send the email body
w, err := client.Data()
if err != nil {
logrus.Errorf("Error creating data writer: %v", err)
continue
}
_, err = w.Write(message)
if err != nil {
logrus.Errorf("Error writing email body: %v", err)
}
err = w.Close()
if err != nil {
logrus.Errorf("Error closing data writer: %v", err)
}
time.Sleep(1 * time.Second)
}
time.Sleep(1 * time.Second)
}
}

if err := client.Quit(); err != nil {
logrus.Errorf("Error closing connection: %v", err)
}
}
Loading