mailer/internal_mail.go

69 lines
1.4 KiB
Go
Raw Normal View History

2025-01-12 13:03:17 +00:00
package mailer
import (
"crypto/tls"
"fmt"
"net/smtp"
"strconv"
)
// internal SMTP code, messy.
func (m Mailer) internal_mail(un string, msg []byte) {
m.wanted_send.Add(1)
wanted := m.wanted_send.Load()
mail_amt := m.mail_counter.Load()
fmt.Println("sent mail: " + strconv.Itoa(int(mail_amt)))
fmt.Println("wanted to send: " + strconv.Itoa(int(wanted)))
if mail_amt > 3000 {
fmt.Println("sending total too high.")
return
}
recip := []string{un}
auth := smtp.PlainAuth(m.From, m.From, m.Password, m.HostWithPort)
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: m.HostOnly,
}
conn, err := tls.Dial("tcp", m.HostWithPort, tlsconfig)
if err != nil {
fmt.Println("dial failed")
return
}
client, err := smtp.NewClient(conn, m.HostWithPort)
if err != nil {
fmt.Println("new client failed")
return
}
if err = client.Auth(auth); err != nil {
fmt.Println("auth failed")
return
}
if err = client.Mail(m.From); err != nil {
fmt.Println("from failed")
return
}
client.Rcpt(recip[0])
w, err := client.Data()
if err != nil {
fmt.Println("recip failed")
return
}
_, err = w.Write(msg)
if err != nil {
fmt.Println("open body failed")
return
}
err = w.Close()
if err != nil {
fmt.Println("close body failed")
return
}
err = client.Quit()
if err != nil {
fmt.Println("client quit failed")
return
}
fmt.Println("sent!")
m.mail_counter.Add(1)
}