2019-12-03 15:27:49 +00:00
|
|
|
package koushin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-12-03 16:18:17 +00:00
|
|
|
"time"
|
2019-12-03 15:27:49 +00:00
|
|
|
|
|
|
|
"github.com/emersion/go-message/mail"
|
|
|
|
"github.com/emersion/go-smtp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) connectSMTP() (*smtp.Client, error) {
|
|
|
|
var c *smtp.Client
|
|
|
|
var err error
|
|
|
|
if s.smtp.tls {
|
|
|
|
c, err = smtp.DialTLS(s.smtp.host, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect to SMTPS server: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c, err = smtp.Dial(s.smtp.host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect to SMTP server: %v", err)
|
|
|
|
}
|
|
|
|
if !s.smtp.insecure {
|
|
|
|
if err := c.StartTLS(nil); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, fmt.Errorf("STARTTLS failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type OutgoingMessage struct {
|
2019-12-03 16:18:17 +00:00
|
|
|
From string
|
|
|
|
To []string
|
2019-12-03 15:46:03 +00:00
|
|
|
Subject string
|
2019-12-03 16:18:17 +00:00
|
|
|
Text string
|
2019-12-03 15:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *OutgoingMessage) WriteTo(w io.Writer) error {
|
2019-12-03 15:46:03 +00:00
|
|
|
from := []*mail.Address{{"", msg.From}}
|
2019-12-03 15:27:49 +00:00
|
|
|
|
2019-12-03 15:46:03 +00:00
|
|
|
to := make([]*mail.Address, len(msg.To))
|
|
|
|
for i, addr := range msg.To {
|
2019-12-03 15:27:49 +00:00
|
|
|
to[i] = &mail.Address{"", addr}
|
|
|
|
}
|
|
|
|
|
|
|
|
var h mail.Header
|
|
|
|
h.SetDate(time.Now())
|
|
|
|
h.SetAddressList("From", from)
|
|
|
|
h.SetAddressList("To", to)
|
2019-12-03 15:46:03 +00:00
|
|
|
if msg.Subject != "" {
|
|
|
|
h.SetText("Subject", msg.Subject)
|
|
|
|
}
|
2019-12-03 15:27:49 +00:00
|
|
|
|
|
|
|
mw, err := mail.CreateWriter(w, h)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create mail writer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var th mail.InlineHeader
|
|
|
|
th.Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
|
|
|
tw, err := mw.CreateSingleInline(th)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create text part: %v", err)
|
|
|
|
}
|
|
|
|
defer tw.Close()
|
|
|
|
|
2019-12-03 15:46:03 +00:00
|
|
|
if _, err := io.WriteString(tw, msg.Text); err != nil {
|
2019-12-03 15:27:49 +00:00
|
|
|
return fmt.Errorf("failed to write text part: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tw.Close(); err != nil {
|
|
|
|
return fmt.Errorf("failed to close text part: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mw.Close(); err != nil {
|
|
|
|
return fmt.Errorf("failed to close mail writer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendMessage(c *smtp.Client, msg *OutgoingMessage) error {
|
2019-12-03 15:46:03 +00:00
|
|
|
if err := c.Mail(msg.From, nil); err != nil {
|
2019-12-03 15:27:49 +00:00
|
|
|
return fmt.Errorf("MAIL FROM failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-12-03 15:46:03 +00:00
|
|
|
for _, to := range msg.To {
|
2019-12-03 15:27:49 +00:00
|
|
|
if err := c.Rcpt(to); err != nil {
|
|
|
|
return fmt.Errorf("RCPT TO failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := c.Data()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("DATA failed: %v", err)
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
if err := msg.WriteTo(w); err != nil {
|
|
|
|
return fmt.Errorf("failed to write outgoing message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
return fmt.Errorf("failed to close SMTP data writer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|