1
0

config.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package smtp
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net/smtp"
  6. "github.com/cockroachdb/errors"
  7. )
  8. // Config contains configuration for SMTP authentication.
  9. //
  10. // ⚠️ WARNING: Change to the field name must preserve the INI key name for backward compatibility.
  11. type Config struct {
  12. Auth string
  13. Host string
  14. Port int
  15. AllowedDomains string
  16. TLS bool `ini:"tls"`
  17. SkipVerify bool
  18. }
  19. func (c *Config) doAuth(auth smtp.Auth) error {
  20. client, err := smtp.Dial(fmt.Sprintf("%s:%d", c.Host, c.Port))
  21. if err != nil {
  22. return err
  23. }
  24. defer client.Close()
  25. if err = client.Hello("gogs"); err != nil {
  26. return err
  27. }
  28. if c.TLS {
  29. if ok, _ := client.Extension("STARTTLS"); ok {
  30. if err = client.StartTLS(&tls.Config{
  31. InsecureSkipVerify: c.SkipVerify,
  32. ServerName: c.Host,
  33. }); err != nil {
  34. return err
  35. }
  36. } else {
  37. return errors.New("SMTP server does not support TLS")
  38. }
  39. }
  40. if ok, _ := client.Extension("AUTH"); ok {
  41. if err = client.Auth(auth); err != nil {
  42. return err
  43. }
  44. return nil
  45. }
  46. return errors.New("unsupported SMTP authentication method")
  47. }