This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
JISQueueing/server/socket/mail.go

70 lines
1.3 KiB
Go

package socket
import (
"JISQueueing/common"
"bytes"
"encoding/base64"
"html/template"
"net/smtp"
"strings"
)
var (
bodyT = ``
t, _ = template.New("email").Parse(bodyT)
)
func sendThankYouEmail(ticket common.Ticket) {
buffer := new(bytes.Buffer)
t.Execute(buffer, ticket)
body := buffer.String()
SendMail(
"mail1.jisedu.or.id:25",
"IT.Q@jisedu.or.id",
"Thank you for coming to the JIS IT Helpdesk",
body,
[]string{ticket.Email},
)
}
func SendMail(addr, from, subject, body string, to []string) error {
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Mail(r.Replace(from)); err != nil {
return err
}
for i := range to {
to[i] = r.Replace(to[i])
if err = c.Rcpt(to[i]); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
msg := "To: " + strings.Join(to, ",") + "\r\n" +
"From: " + from + "\r\n" +
"Subject: " + subject + "\r\n" +
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
_, err = w.Write([]byte(msg))
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}