-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathservice.go
49 lines (40 loc) · 1.3 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package mail
import (
"fmt"
"net/smtp"
"strings"
"time"
"github.com/sirupsen/logrus"
)
type Mail struct {
To string
Subject string
Body string
}
func (mail *Mail) BuildMessage() []byte {
message := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n"
message += fmt.Sprintf("From: Foreign Exposure program<%s>\r\n", sender)
message += fmt.Sprintf("Subject: %s | Foreign Exposure Program\r\n", mail.Subject)
message += fmt.Sprintf("To: %s\r\n\r\n", mail.To)
message += strings.Replace(mail.Body, "\n", "<br>", -1)
message += "<br><br>--<br>Foreign Exposure program<br>"
message += "<br><br>--<br>Academics and career council<br>"
message += "Indian Institute of Technology Kanpur<br><br>"
message += "<small>This is an auto-generated email. Please do not reply.</small>"
return []byte(message)
}
func Service(mailQueue chan Mail) {
addr := fmt.Sprintf("%s:%s", host, port)
auth := smtp.PlainAuth("", sender, pass, host)
for mail := range mailQueue {
message := mail.BuildMessage()
to := mail.To
toSendArray := make([]string, 0)
toSendArray = append(toSendArray, to)
if err := smtp.SendMail(addr, auth, sender, toSendArray, message); err != nil {
logrus.Errorf("Error sending mail: %v", to)
logrus.Errorf("Error: %v", err)
}
time.Sleep(1 * time.Second)
}
}