Fixup email settings
Adds support for switching between STARTTLS, TLS and no TLS. Adds support for not specifying a username and password. Fixes #5
This commit is contained in:
parent
948108217c
commit
1ac2492682
4 changed files with 51 additions and 17 deletions
|
@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|||
## Unreleased
|
||||
### Changed
|
||||
* Make including contact information in the user agent optional
|
||||
* Support selecting a TLS mode for email (STARTTLS, TLS or none)
|
||||
* Support not using a username/password for email authentication
|
||||
|
||||
## 0.3.8 - 2025-01-18
|
||||
### Fixed
|
||||
|
|
|
@ -56,13 +56,14 @@ server:
|
|||
# This is a required field
|
||||
externalURL: "http://127.0.0.1:8080"
|
||||
email:
|
||||
# Defaults are not provided for any email option
|
||||
# All of the below are required fields
|
||||
password: "yourapikey"
|
||||
username: "apikey"
|
||||
# These are required fields
|
||||
host: "smtp.sendgrid.net"
|
||||
port: 587
|
||||
from: "Walrss <walrss@yourdomain.com>"
|
||||
# These fields are not required
|
||||
tls: "" # valid values are: starttls, tls, no - default: starttls
|
||||
password: "yourapikey"
|
||||
username: "apikey"
|
||||
platform:
|
||||
disableRegistration: false
|
||||
disableSecureCookies: false
|
||||
|
|
|
@ -42,13 +42,13 @@ func getUserAgent(st *state.State) string {
|
|||
} else if core.Version != "" {
|
||||
o += "/" + core.Version
|
||||
}
|
||||
|
||||
|
||||
var parts []string
|
||||
if st.Config.Platform.ContactInformation != "" {
|
||||
parts = append(parts, st.Config.Platform.ContactInformation)
|
||||
}
|
||||
parts = append(parts, "https://github.com/codemicro/walrss")
|
||||
|
||||
|
||||
o += " (" + strings.Join(parts, ", ") + ")"
|
||||
ua.ua = o
|
||||
})
|
||||
|
@ -370,17 +370,43 @@ func sendEmail(st *state.State, plain, html []byte, to, subject string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
return (&email.Email{
|
||||
e := &email.Email{
|
||||
From: st.Config.Email.From,
|
||||
To: []string{to},
|
||||
Subject: subject,
|
||||
Text: plain,
|
||||
HTML: html,
|
||||
}).SendWithStartTLS(
|
||||
fmt.Sprintf("%s:%d", st.Config.Email.Host, st.Config.Email.Port),
|
||||
smtp.PlainAuth("", st.Config.Email.Username, st.Config.Email.Password, st.Config.Email.Host),
|
||||
&tls.Config{
|
||||
ServerName: st.Config.Email.Host,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
var smtpAuth smtp.Auth
|
||||
if st.Config.Email.Username != "" || st.Config.Email.Password != "" {
|
||||
smtpAuth = smtp.PlainAuth("", st.Config.Email.Username, st.Config.Email.Password, st.Config.Email.Host)
|
||||
}
|
||||
|
||||
smtpAddr := fmt.Sprintf("%s:%d", st.Config.Email.Host, st.Config.Email.Port)
|
||||
|
||||
var sendFn func(*email.Email) error
|
||||
|
||||
switch st.Config.Email.TLS {
|
||||
case "no":
|
||||
sendFn = func(e *email.Email) error {
|
||||
return e.Send(smtpAddr, smtpAuth)
|
||||
}
|
||||
case "tls":
|
||||
sendFn = func(e *email.Email) error {
|
||||
return e.SendWithTLS(smtpAddr, smtpAuth, &tls.Config{
|
||||
ServerName: st.Config.Email.Host,
|
||||
})
|
||||
}
|
||||
case "starttls":
|
||||
sendFn = func(e *email.Email) error {
|
||||
return e.SendWithStartTLS(smtpAddr, smtpAuth, &tls.Config{
|
||||
ServerName: st.Config.Email.Host,
|
||||
})
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknown TLS option %s", st.Config.Email.TLS)
|
||||
}
|
||||
|
||||
return sendFn(e)
|
||||
}
|
||||
|
|
|
@ -23,10 +23,11 @@ func New() *State {
|
|||
type Config struct {
|
||||
Email struct {
|
||||
Host string `fig:"host" validate:"required"`
|
||||
Username string `fig:"username" validate:"required"`
|
||||
Password string `fig:"password" validate:"required"`
|
||||
From string `fig:"from" validate:"required"`
|
||||
Port int `fig:"port" validate:"required"`
|
||||
Username string `fig:"username"`
|
||||
Password string `fig:"password"`
|
||||
From string `fig:"from" validate:"required"`
|
||||
TLS string `fig:"tls" default:"starttls"`
|
||||
}
|
||||
Server struct {
|
||||
Host string `fig:"host" default:"127.0.0.1"`
|
||||
|
@ -69,6 +70,10 @@ func LoadConfig() (*Config, error) {
|
|||
|
||||
cfg.Server.ExternalURL = strings.TrimSuffix(cfg.Server.ExternalURL, "/")
|
||||
|
||||
if cfg.Email.TLS != "tls" && cfg.Email.TLS != "starttls" && cfg.Email.TLS != "no" {
|
||||
return nil, errors.New("invalid email.tls value: must be 'starttls', 'tls' or 'no'")
|
||||
}
|
||||
|
||||
if !cfg.Debug {
|
||||
log.Logger = log.Logger.Level(zerolog.InfoLevel)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue