config.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE and LICENSE.gogs file.
  4. package smtp
  5. import (
  6. "crypto/tls"
  7. "fmt"
  8. "github.com/pkg/errors"
  9. "net"
  10. "net/smtp"
  11. )
  12. // Config contains configuration for SMTP authentication.
  13. //
  14. // ⚠️ WARNING: Change to the field name must preserve the INI key name for backward compatibility.
  15. type Config struct {
  16. Auth string
  17. Host string
  18. Port int
  19. AllowedDomains string
  20. TLS bool `ini:"tls"`
  21. SkipVerify bool
  22. }
  23. func (c *Config) doAuth(auth smtp.Auth) error {
  24. addr := fmt.Sprintf("%s:%d", c.Host, c.Port)
  25. conn, err := net.Dial("tcp", addr)
  26. if err != nil {
  27. return err
  28. }
  29. defer func() {
  30. _ = conn.Close()
  31. }()
  32. isSecureConn := false
  33. if c.Port == 465 {
  34. isSecureConn = true
  35. conn = tls.Client(conn, &tls.Config{
  36. InsecureSkipVerify: c.SkipVerify,
  37. ServerName: c.Host,
  38. })
  39. }
  40. client, err := smtp.NewClient(conn, c.Host)
  41. if err != nil {
  42. return err
  43. }
  44. if err = client.Hello("gogs"); err != nil {
  45. return err
  46. }
  47. if c.TLS && !isSecureConn {
  48. if ok, _ := client.Extension("STARTTLS"); ok {
  49. if err = client.StartTLS(&tls.Config{
  50. InsecureSkipVerify: c.SkipVerify,
  51. ServerName: c.Host,
  52. }); err != nil {
  53. return err
  54. }
  55. } else {
  56. return errors.New("SMTP server does not support TLS")
  57. }
  58. }
  59. if ok, _ := client.Extension("AUTH"); ok {
  60. if err = client.Auth(auth); err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. return errors.New("unsupported SMTP authentication method")
  66. }