| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/nicholas-fedor/shoutrrr/pkg/format" |
| 12 |
"github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/util" |
| 14 |
) |
| 15 |
|
| 16 |
// Config is the configuration needed to send e-mail notifications over SMTP. |
| 17 |
type Config struct { |
| 18 |
Host string `desc:"SMTP server hostname or IP address" url:"Host"` |
| 19 |
Username string `desc:"SMTP server username" url:"User" default:""` |
| 20 |
Password string `desc:"SMTP server password or hash (for OAuth2)" url:"Pass" default:""` |
| 21 |
Port uint16 `desc:"SMTP server port, common ones are 25, 465, 587 or 2525" url:"Port" default:"25"` |
| 22 |
FromAddress string `desc:"E-mail address that the mail are sent from" key:"fromaddress,from"` |
| 23 |
FromName string `desc:"Name of the sender" key:"fromname" optional:"yes"` |
| 24 |
ToAddresses []string `desc:"List of recipient e-mails" key:"toaddresses,to"` |
| 25 |
Subject string `desc:"The subject of the sent mail" default:"Shoutrrr Notification" key:"subject,title"` |
| 26 |
Auth authType `desc:"SMTP authentication method" default:"Unknown" key:"auth"` |
| 27 |
Encryption encMethod `desc:"Encryption method" default:"Auto" key:"encryption"` |
| 28 |
UseStartTLS bool `desc:"Whether to use StartTLS encryption" default:"Yes" key:"usestarttls,starttls"` |
| 29 |
UseHTML bool `desc:"Whether the message being sent is in HTML" default:"No" key:"usehtml"` |
| 30 |
ClientHost string `desc:"SMTP client hostname" default:"localhost" key:"clienthost"` |
| 31 |
RequireStartTLS bool `desc:"Fail if StartTLS is enabled but unsupported" default:"No" key:"requirestarttls"` |
| 32 |
SkipTLSVerify bool `desc:"Whether to skip TLS certificate verification" default:"No" key:"skiptlsverify"` |
| 33 |
Timeout time.Duration `desc:"Timeout for SMTP operations" default:"10s" key:"timeout"` |
| 34 |
} |
| 35 |
|
| 36 |
// Scheme is the identifying part of this service's configuration URL. |
| 37 |
const Scheme = "smtp" |
| 38 |
|
| 39 |
// Static errors for configuration validation. |
| 40 |
var ( |
| 41 |
ErrFromAddressMissing = errors.New("fromAddress missing from config URL") |
| 42 |
ErrToAddressMissing = errors.New("toAddress missing from config URL") |
| 43 |
) |
| 44 |
|
| 45 |
// Clone returns a copy of the config. |
| 46 |
func (c *Config) Clone() Config { |
| 47 |
clone := *c |
| 48 |
clone.ToAddresses = make([]string, len(c.ToAddresses)) |
| 49 |
copy(clone.ToAddresses, c.ToAddresses) |
| 50 |
|
| 51 |
return clone |
| 52 |
} |
| 53 |
|
| 54 |
// Enums returns the fields that should use a corresponding EnumFormatter to Print/Parse their values. |
| 55 |
func (c *Config) Enums() map[string]types.EnumFormatter { |
| 56 |
return map[string]types.EnumFormatter{ |
| 57 |
"Auth": AuthTypes.Enum, |
| 58 |
"Encryption": EncMethods.Enum, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// FixEmailTags replaces parsed spaces (+) in e-mail addresses with '+'. |
| 63 |
func (c *Config) FixEmailTags() { |
| 64 |
c.FromAddress = strings.ReplaceAll(c.FromAddress, " ", "+") |
| 65 |
for i, adr := range c.ToAddresses { |
| 66 |
c.ToAddresses[i] = strings.ReplaceAll(adr, " ", "+") |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// GetURL returns a URL representation of its current field values. |
| 71 |
func (c *Config) GetURL() *url.URL { |
| 72 |
resolver := format.NewPropKeyResolver(c) |
| 73 |
|
| 74 |
return c.getURL(&resolver) |
| 75 |
} |
| 76 |
|
| 77 |
// SetURL updates a ServiceConfig from a URL representation of its field values. |
| 78 |
func (c *Config) SetURL(serviceURL *url.URL) error { |
| 79 |
resolver := format.NewPropKeyResolver(c) |
| 80 |
|
| 81 |
return c.setURL(&resolver, serviceURL) |
| 82 |
} |
| 83 |
|
| 84 |
// getURL constructs a URL from the Config's fields using the provided resolver. |
| 85 |
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL { |
| 86 |
serviceURL := &url.URL{ |
| 87 |
User: util.URLUserPassword(c.Username, c.Password), |
| 88 |
Host: fmt.Sprintf("%s:%d", c.Host, c.Port), |
| 89 |
Path: "/", |
| 90 |
Scheme: Scheme, |
| 91 |
ForceQuery: true, |
| 92 |
} |
| 93 |
// Define primary keys in the exact order matching urlWithAllProps |
| 94 |
primaryKeys := []string{ |
| 95 |
"auth", |
| 96 |
"clienthost", |
| 97 |
"encryption", |
| 98 |
"fromaddress", |
| 99 |
"fromname", |
| 100 |
"subject", |
| 101 |
"toaddresses", |
| 102 |
"usehtml", |
| 103 |
"usestarttls", |
| 104 |
"timeout", |
| 105 |
} |
| 106 |
|
| 107 |
queryParts := make([]string, 0, len(primaryKeys)+1) |
| 108 |
for _, key := range primaryKeys { |
| 109 |
if key == "timeout" { |
| 110 |
queryParts = append( |
| 111 |
queryParts, |
| 112 |
fmt.Sprintf("%s=%s", key, url.QueryEscape(c.Timeout.String())), |
| 113 |
) |
| 114 |
|
| 115 |
continue |
| 116 |
} |
| 117 |
|
| 118 |
value, err := resolver.Get(key) |
| 119 |
if err != nil { |
| 120 |
continue // Skip invalid fields |
| 121 |
} |
| 122 |
|
| 123 |
queryParts = append(queryParts, fmt.Sprintf("%s=%s", key, url.QueryEscape(value))) |
| 124 |
} |
| 125 |
// Only include requirestarttls if explicitly set to true |
| 126 |
if c.RequireStartTLS { |
| 127 |
queryParts = append(queryParts, "requirestarttls=Yes") |
| 128 |
} |
| 129 |
// Only include skiptlsverify if explicitly set to true |
| 130 |
if c.SkipTLSVerify { |
| 131 |
queryParts = append(queryParts, "skiptlsverify=Yes") |
| 132 |
} |
| 133 |
|
| 134 |
serviceURL.RawQuery = strings.Join(queryParts, "&") |
| 135 |
|
| 136 |
return serviceURL |
| 137 |
} |
| 138 |
|
| 139 |
// setURL updates the Config from a URL using the provided resolver. |
| 140 |
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error { |
| 141 |
password, _ := serviceURL.User.Password() |
| 142 |
c.Username = serviceURL.User.Username() |
| 143 |
c.Password = password |
| 144 |
c.Host = serviceURL.Hostname() |
| 145 |
|
| 146 |
if port, err := strconv.ParseUint(serviceURL.Port(), 10, 16); err == nil { |
| 147 |
c.Port = uint16(port) |
| 148 |
} |
| 149 |
|
| 150 |
for key, vals := range serviceURL.Query() { |
| 151 |
if key == "timeout" { |
| 152 |
duration, err := time.ParseDuration(vals[0]) |
| 153 |
if err != nil { |
| 154 |
return fmt.Errorf("parsing timeout parameter %q: %w", vals[0], err) |
| 155 |
} |
| 156 |
|
| 157 |
c.Timeout = duration |
| 158 |
|
| 159 |
continue |
| 160 |
} |
| 161 |
|
| 162 |
if err := resolver.Set(key, vals[0]); err != nil { |
| 163 |
return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err) |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if serviceURL.String() != "smtp://[email protected]" { |
| 168 |
if len(c.FromAddress) < 1 { |
| 169 |
return ErrFromAddressMissing |
| 170 |
} |
| 171 |
|
| 172 |
if len(c.ToAddresses) < 1 { |
| 173 |
return ErrToAddressMissing |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
return nil |
| 178 |
} |