~bigbes/core-go

ref: 7457c44be5ec4f9247154d2fdec2fe8885db3f45 core-go/email/send.go -rw-r--r-- 2.8 KiB
7457c44b — Robin Jarry treewide: update to new multi worker dowork api 1 year, 8 months ago
                                                                                
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package email

import (
	"context"
	"fmt"
	"io"
	"net/mail"
	"strconv"

	_ "github.com/emersion/go-message/charset"
	"github.com/emersion/go-sasl"
	"github.com/emersion/go-smtp"

	"git.sr.ht/~sircmpwn/core-go/config"
)

type mailConfig struct {
	host string
	port int
	from string

	enctype  string
	authtype string

	user string
	pass string
}

func mailSetup(ctx context.Context) (*smtp.Client, *mail.Address, error) {
	conf := config.ForContext(ctx)
	mailconf := &mailConfig{enctype: "starttls", authtype: "plain"}
	var err error

	portStr, ok := conf.Get("mail", "smtp-port")
	if !ok {
		panic(fmt.Errorf("[mail]smtp-port unset"))
	}
	mailconf.port, err = strconv.Atoi(portStr)
	if err != nil {
		panic(fmt.Errorf("Unable to parse [mail]smtp-port (must be integer)"))
	}

	if mailconf.host, ok = conf.Get("mail", "smtp-host"); !ok {
		panic(fmt.Errorf("Missing SMTP configuration options [smtp-host]"))
	}
	if mailconf.from, ok = conf.Get("mail", "smtp-from"); !ok {
		panic(fmt.Errorf("Missing SMTP configuration options [smtp-from]"))
	}

	sender, err := mail.ParseAddress(mailconf.from)
	if err != nil {
		panic(err)
	}

	if enctype, ok := conf.Get("mail", "smtp-encryption"); ok {
		switch enctype {
		case "starttls", "tls", "insecure":
			mailconf.enctype = enctype
		default:
			panic(fmt.Errorf("Invalid SMTP configuration value for [smtp-encryption]"))
		}
	}

	if authtype, ok := conf.Get("mail", "smtp-auth"); ok {
		switch authtype {
		case "none", "plain":
			mailconf.authtype = authtype
		default:
			panic(fmt.Errorf("Invalid SMTP configuration value for [smtp-auth]"))
		}
	}
	if mailconf.authtype == "plain" {
		if mailconf.user, ok = conf.Get("mail", "smtp-user"); !ok {
			panic(fmt.Errorf("Missing SMTP configuration options [smtp-user]"))
		}
		if mailconf.pass, ok = conf.Get("mail", "smtp-password"); !ok {
			panic(fmt.Errorf("Missing SMTP configuration options [smtp-password]"))
		}
	}

	var c *smtp.Client
	addr := fmt.Sprintf("%s:%d", mailconf.host, mailconf.port)

	if mailconf.enctype == "tls" {
		c, err = smtp.DialTLS(addr, nil)
	} else if mailconf.enctype == "starttls" {
		c, err = smtp.DialStartTLS(addr, nil)
	} else {
		c, err = smtp.Dial(addr)
	}
	if err != nil {
		return nil, nil, err
	}

	if err = c.Hello("localhost"); err != nil {
		return nil, nil, err
	}

	if mailconf.authtype == "plain" {
		auth := sasl.NewPlainClient("", mailconf.user, mailconf.pass)
		if ok, _ := c.Extension("AUTH"); !ok {
			panic(fmt.Errorf("smtp: server doesn't support AUTH"))
		}
		if err = c.Auth(auth); err != nil {
			return nil, nil, err
		}
	}

	return c, sender, nil
}

// Sends an email. Blocks until it's sent or an error occurs.
func Send(ctx context.Context, msg io.Reader, rcpts []string) error {
	c, sender, err := mailSetup(ctx)
	if err != nil {
		return err
	}
	defer c.Close()
	return c.SendMail(sender.Address, rcpts, msg)
}