1
0

smtp_config.go 818 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package smtp
  2. const (
  3. defaultHost = "localhost"
  4. defaultPort = 587
  5. )
  6. type Config struct {
  7. host string
  8. port uint16
  9. username string
  10. password string
  11. }
  12. type ConfigOption func(*Config)
  13. type ConfigOptions []ConfigOption
  14. func (s *ConfigOptions) Add(option ConfigOption) {
  15. *s = append(*s, option)
  16. }
  17. func NewConfig(opts ...ConfigOption) Config {
  18. c := &Config{}
  19. for _, opt := range opts {
  20. opt(c)
  21. }
  22. return *c
  23. }
  24. func WithUsername(username string) ConfigOption {
  25. return func(s *Config) {
  26. s.username = username
  27. }
  28. }
  29. func WithPassword(password string) ConfigOption {
  30. return func(s *Config) {
  31. s.password = password
  32. }
  33. }
  34. func WithHost(host string) ConfigOption {
  35. return func(s *Config) {
  36. s.host = host
  37. }
  38. }
  39. func WithPort(port uint16) ConfigOption {
  40. return func(s *Config) {
  41. s.port = port
  42. }
  43. }