snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
110
server/pkg/mail/mail.go
Normal file
110
server/pkg/mail/mail.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package mail
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
gomail "gopkg.in/mail.v2"
|
||||
)
|
||||
|
||||
type (
|
||||
Dialer interface {
|
||||
DialAndSend(...*gomail.Message) error
|
||||
}
|
||||
|
||||
applyCfg func(*gomail.Dialer)
|
||||
)
|
||||
|
||||
const (
|
||||
addressCheckRE = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
|
||||
)
|
||||
|
||||
var (
|
||||
addressCheck *regexp.Regexp
|
||||
defaultDialer Dialer
|
||||
defaultFrom string
|
||||
defaultDialerError error
|
||||
)
|
||||
|
||||
func init() {
|
||||
addressCheck = regexp.MustCompile(addressCheckRE)
|
||||
}
|
||||
|
||||
// SetupDialer setups SMTP dialer
|
||||
//
|
||||
// Host variable can contain "<host>:<port>" that will override port value
|
||||
func SetupDialer(host string, port int, user, pass, from string, ff ...applyCfg) {
|
||||
if host == "" {
|
||||
defaultDialerError = fmt.Errorf("no hostname provided for SMTP")
|
||||
return
|
||||
}
|
||||
|
||||
if strings.Contains(host, ":") {
|
||||
parts := strings.SplitN(host, ":", 2)
|
||||
port, _ = strconv.Atoi(parts[1])
|
||||
host = parts[0]
|
||||
}
|
||||
|
||||
if port == 0 {
|
||||
defaultDialerError = fmt.Errorf("no port provided for SMTP")
|
||||
return
|
||||
}
|
||||
|
||||
if from == "" {
|
||||
from = "placeholder@example.net"
|
||||
}
|
||||
|
||||
if defaultDialerError != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defaultFrom = from
|
||||
dialer := gomail.NewDialer(
|
||||
host,
|
||||
port,
|
||||
user,
|
||||
pass,
|
||||
)
|
||||
|
||||
for _, fn := range ff {
|
||||
fn(dialer)
|
||||
}
|
||||
|
||||
defaultDialer = dialer
|
||||
}
|
||||
|
||||
func New() *gomail.Message {
|
||||
message := gomail.NewMessage()
|
||||
message.SetHeader("From", defaultFrom)
|
||||
return message
|
||||
}
|
||||
|
||||
// Send message with SMTP dialer
|
||||
func Send(message *gomail.Message, dd ...Dialer) (err error) {
|
||||
for _, d := range append(dd, defaultDialer) {
|
||||
if d == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if err = d.DialAndSend(message); err != nil {
|
||||
return fmt.Errorf("could not send email: %w", err)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// At this point, none of the dialer could be used,
|
||||
// is there an error with default dialer?
|
||||
if defaultDialerError != nil {
|
||||
return fmt.Errorf("could not send email: %w", defaultDialerError)
|
||||
}
|
||||
|
||||
return fmt.Errorf("unable to find configured and working SMTP dialer")
|
||||
|
||||
}
|
||||
|
||||
func IsValidAddress(addr string) bool {
|
||||
return addressCheck.MatchString(addr)
|
||||
}
|
||||
Reference in New Issue
Block a user