-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.go
65 lines (61 loc) · 2.07 KB
/
send.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package report
import (
"fmt"
"log"
"net"
"net/mail"
"net/smtp"
"strings"
"github.com/rothskeller/packet/envelope"
"github.com/rothskeller/packet/jnos"
"github.com/rothskeller/packet/message"
"github.com/rothskeller/wppsvr/config"
"github.com/rothskeller/wppsvr/store"
)
// Send generates the report for the session and sends it to all designated
// recipients, through the supplied open BBS connection and/or via SMTP. It
// also stores the report in the session.
func Send(st Store, conn *jnos.Conn, session *store.Session) {
report := Generate(st, session)
sendTo := session.ReportToText
if session.Flags&store.ReportToSenders != 0 {
sendTo = append(sendTo, report.Participants...)
}
session.Report = report.RenderPlainText()
st.UpdateSession(session)
subject := message.EncodeSubject(st.NextMessageID(session.Prefix), "ROUTINE", "", "SCCo Packet Practice Report")
body := new(envelope.Envelope).RenderBody(session.Report)
// To avoid potential problems with JNOS line length limits, we
// send to each recipient separately.
// conn.Send(subject, body, sendTo...)
for _, addr := range sendTo {
conn.Send(subject, body, addr)
}
if len(session.ReportToHTML) != 0 {
if err := report.SendHTML(session.ReportToHTML); err != nil {
log.Printf("ERROR: %s", err)
}
}
log.Printf("Sent report for %s on %s.", session.Name, session.End.Format("2006-01-02"))
}
// SendHTML sends the report in HTML format to the specified address(es).
func (r *Report) SendHTML(to []string) error {
conf := config.Get().SMTP
var addrs []string
for i, t := range to {
if addr, err := mail.ParseAddress(t); err == nil {
addrs = append(addrs, addr.Address)
to[i] = addr.String()
} else {
return fmt.Errorf("address %q: %s", t, err)
}
}
msg := r.RenderEmail(strings.Join(to, ", "))
from, _ := mail.ParseAddress(conf.From)
host, _, _ := net.SplitHostPort(conf.Server)
auth := smtp.PlainAuth("", conf.Username, conf.Password, host)
if err := smtp.SendMail(conf.Server, auth, from.Address, addrs, []byte(msg)); err != nil {
return fmt.Errorf("smtp.SendMail: %s", err)
}
return nil
}