aboutsummaryrefslogtreecommitdiff
path: root/smtp.go
blob: 4c720e2bef8cedb8aca4a5a7a41145a7cd8bf9ec (plain) (blame)
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
package alps

import (
	"crypto/tls"
	"fmt"

	"github.com/emersion/go-smtp"
)

func (s *Server) dialSMTP() (*smtp.Client, error) {
	if s.smtp.host == "" {
		return nil, fmt.Errorf("SMTP is disabled")
	}

	var c *smtp.Client
	var err error
	if s.smtp.tls {
		c, err = smtp.DialTLS(s.smtp.host, &tls.Config{InsecureSkipVerify: s.Options.SkipTlsVerification})
		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
}