2019-04-25 04:52:08 +07:00
package notification
import (
2019-12-21 01:40:01 +07:00
"crypto/tls"
2019-12-30 09:03:51 +07:00
"crypto/x509"
2019-12-21 01:40:01 +07:00
"errors"
2019-04-25 04:52:08 +07:00
"fmt"
"net/smtp"
2019-12-21 01:40:01 +07:00
"strings"
2020-08-21 09:16:23 +07:00
"time"
2019-04-25 04:52:08 +07:00
2019-12-24 09:14:52 +07:00
"github.com/authelia/authelia/internal/configuration/schema"
2020-11-25 05:54:36 +07:00
"github.com/authelia/authelia/internal/logging"
2019-12-21 01:40:01 +07:00
"github.com/authelia/authelia/internal/utils"
2019-04-25 04:52:08 +07:00
)
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// SMTPNotifier a notifier to send emails to SMTP servers.
2019-04-25 04:52:08 +07:00
type SMTPNotifier struct {
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
username string
password string
sender string
2020-11-05 06:22:10 +07:00
identifier string
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
host string
port int
disableRequireTLS bool
address string
subject string
startupCheckAddress string
client * smtp . Client
tlsConfig * tls . Config
2019-04-25 04:52:08 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// NewSMTPNotifier creates a SMTPNotifier using the notifier configuration.
2021-01-04 17:28:55 +07:00
func NewSMTPNotifier ( configuration schema . SMTPNotifierConfiguration , certPool * x509 . CertPool ) * SMTPNotifier {
2019-12-30 09:03:51 +07:00
notifier := & SMTPNotifier {
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
username : configuration . Username ,
password : configuration . Password ,
sender : configuration . Sender ,
2020-11-05 06:22:10 +07:00
identifier : configuration . Identifier ,
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
host : configuration . Host ,
port : configuration . Port ,
disableRequireTLS : configuration . DisableRequireTLS ,
address : fmt . Sprintf ( "%s:%d" , configuration . Host , configuration . Port ) ,
subject : configuration . Subject ,
startupCheckAddress : configuration . StartupCheckAddress ,
2021-01-04 17:28:55 +07:00
tlsConfig : utils . NewTLSConfig ( configuration . TLS , tls . VersionTLS12 , certPool ) ,
2019-04-25 04:52:08 +07:00
}
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
return notifier
2019-04-25 04:52:08 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Do startTLS if available (some servers only provide the auth extension after, and encryption is preferred).
2020-04-23 09:01:24 +07:00
func ( n * SMTPNotifier ) startTLS ( ) error {
2021-01-17 06:23:35 +07:00
logger := logging . Logger ( )
2020-04-09 08:05:17 +07:00
// Only start if not already encrypted
2019-12-30 09:03:51 +07:00
if _ , ok := n . client . TLSConnectionState ( ) ; ok {
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP connection is already encrypted, skipping STARTTLS" )
2020-04-23 09:01:24 +07:00
return nil
2019-12-30 09:03:51 +07:00
}
2020-05-06 07:52:06 +07:00
switch ok , _ := n . client . Extension ( "STARTTLS" ) ; ok {
case true :
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP server supports STARTTLS (disableVerifyCert: %t, ServerName: %s), attempting" , n . tlsConfig . InsecureSkipVerify , n . tlsConfig . ServerName )
2019-12-30 09:03:51 +07:00
2020-04-23 09:01:24 +07:00
if err := n . client . StartTLS ( n . tlsConfig ) ; err != nil {
return err
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP STARTTLS completed without error" )
2020-05-06 07:52:06 +07:00
default :
switch n . disableRequireTLS {
case true :
2021-01-17 06:23:35 +07:00
logger . Warn ( "Notifier SMTP server does not support STARTTLS and SMTP configuration is set to disable the TLS requirement (only useful for unauthenticated emails over plain text)" )
2020-05-06 07:52:06 +07:00
default :
return errors . New ( "Notifier SMTP server does not support TLS and it is required by default (see documentation if you want to disable this highly recommended requirement)" )
}
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2020-04-23 09:01:24 +07:00
return nil
2019-12-30 09:03:51 +07:00
}
2019-12-21 01:40:01 +07:00
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Attempt Authentication.
2020-04-23 09:01:24 +07:00
func ( n * SMTPNotifier ) auth ( ) error {
2021-01-17 06:23:35 +07:00
logger := logging . Logger ( )
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Attempt AUTH if password is specified only.
2019-12-21 01:40:01 +07:00
if n . password != "" {
2019-12-30 09:03:51 +07:00
_ , ok := n . client . TLSConnectionState ( )
if ! ok {
2020-04-21 12:53:47 +07:00
return errors . New ( "Notifier SMTP client does not support authentication over plain text and the connection is currently plain text" )
2019-12-28 09:49:29 +07:00
}
2019-12-21 01:40:01 +07:00
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Check the server supports AUTH, and get the mechanisms.
2019-12-30 09:03:51 +07:00
ok , m := n . client . Extension ( "AUTH" )
if ok {
2020-05-06 02:35:32 +07:00
var auth smtp . Auth
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP server supports authentication with the following mechanisms: %s" , m )
2019-12-21 01:40:01 +07:00
mechanisms := strings . Split ( m , " " )
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Adaptively select the AUTH mechanism to use based on what the server advertised.
2019-12-21 01:40:01 +07:00
if utils . IsStringInSlice ( "PLAIN" , mechanisms ) {
auth = smtp . PlainAuth ( "" , n . username , n . password , n . host )
2020-05-06 02:35:32 +07:00
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP client attempting AUTH PLAIN with server" )
2019-12-21 01:40:01 +07:00
} else if utils . IsStringInSlice ( "LOGIN" , mechanisms ) {
2019-12-30 09:03:51 +07:00
auth = newLoginAuth ( n . username , n . password , n . host )
2020-05-06 02:35:32 +07:00
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP client attempting AUTH LOGIN with server" )
2019-12-21 01:40:01 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Throw error since AUTH extension is not supported.
2019-12-21 01:40:01 +07:00
if auth == nil {
2020-04-21 12:53:47 +07:00
return fmt . Errorf ( "notifier SMTP server does not advertise a AUTH mechanism that are supported by Authelia (PLAIN or LOGIN are supported, but server advertised %s mechanisms)" , m )
2019-12-21 01:40:01 +07:00
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Authenticate.
2020-04-23 09:01:24 +07:00
if err := n . client . Auth ( auth ) ; err != nil {
return err
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP client authenticated successfully with the server" )
2020-05-06 02:35:32 +07:00
2020-04-21 12:53:47 +07:00
return nil
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2020-04-21 12:53:47 +07:00
return errors . New ( "Notifier SMTP server does not advertise the AUTH extension but config requires AUTH (password specified), either disable AUTH, or use an SMTP host that supports AUTH PLAIN or AUTH LOGIN" )
2019-12-21 01:40:01 +07:00
}
2020-05-06 02:35:32 +07:00
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP config has no password specified so authentication is being skipped" )
2020-05-06 02:35:32 +07:00
2020-04-21 12:53:47 +07:00
return nil
2019-12-30 09:03:51 +07:00
}
2019-12-21 01:40:01 +07:00
2020-08-21 09:16:23 +07:00
func ( n * SMTPNotifier ) compose ( recipient , subject , body , htmlBody string ) error {
2021-01-17 06:23:35 +07:00
logger := logging . Logger ( )
logger . Debugf ( "Notifier SMTP client attempting to send email body to %s" , recipient )
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
if ! n . disableRequireTLS {
_ , ok := n . client . TLSConnectionState ( )
if ! ok {
return errors . New ( "Notifier SMTP client can't send an email over plain text connection" )
}
}
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
wc , err := n . client . Data ( )
if err != nil {
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP client error while obtaining WriteCloser: %s" , err )
2019-12-30 09:03:51 +07:00
return err
}
2020-08-21 09:16:23 +07:00
boundary := utils . RandomString ( 30 , utils . AlphaNumericCharacters )
now := time . Now ( )
msg := "Date:" + now . Format ( rfc5322DateTimeLayout ) + "\n" +
"From: " + n . sender + "\n" +
2019-12-30 09:03:51 +07:00
"To: " + recipient + "\n" +
"Subject: " + subject + "\n" +
2020-08-21 09:16:23 +07:00
"MIME-version: 1.0\n" +
"Content-Type: multipart/alternative; boundary=" + boundary + "\n\n" +
"--" + boundary + "\n" +
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"Content-Transfer-Encoding: quoted-printable\n" +
"Content-Disposition: inline\n\n" +
body + "\n"
if htmlBody != "" {
msg += "--" + boundary + "\n" +
"Content-Type: text/html; charset=\"UTF-8\"\n\n" +
htmlBody + "\n"
}
msg += "--" + boundary + "--"
2019-12-30 09:03:51 +07:00
2020-04-09 08:05:17 +07:00
_ , err = fmt . Fprint ( wc , msg )
2019-12-30 09:03:51 +07:00
if err != nil {
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP client error while sending email body over WriteCloser: %s" , err )
2019-04-25 04:52:08 +07:00
return err
}
2019-12-30 09:03:51 +07:00
err = wc . Close ( )
if err != nil {
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP client error while closing the WriteCloser: %s" , err )
2019-04-25 04:52:08 +07:00
return err
}
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
return nil
}
2019-04-25 04:52:08 +07:00
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Dial the SMTP server with the SMTPNotifier config.
2020-04-23 09:01:24 +07:00
func ( n * SMTPNotifier ) dial ( ) error {
2021-01-17 06:23:35 +07:00
logger := logging . Logger ( )
logger . Debugf ( "Notifier SMTP client attempting connection to %s" , n . address )
2020-05-06 02:35:32 +07:00
2020-02-20 08:09:46 +07:00
if n . port == 465 {
2021-07-30 10:15:12 +07:00
logger . Infof ( "Notifier SMTP client using submissions port 465. Make sure the mail server you are connecting to is configured for submissions and not SMTPS." )
2020-05-06 02:35:32 +07:00
2020-02-20 08:09:46 +07:00
conn , err := tls . Dial ( "tcp" , n . address , n . tlsConfig )
if err != nil {
return err
}
2020-05-06 02:35:32 +07:00
2020-02-20 08:09:46 +07:00
client , err := smtp . NewClient ( conn , n . host )
if err != nil {
return err
}
2020-05-06 02:35:32 +07:00
2020-02-20 08:09:46 +07:00
n . client = client
} else {
client , err := smtp . Dial ( n . address )
if err != nil {
return err
}
2020-05-06 02:35:32 +07:00
2020-02-20 08:09:46 +07:00
n . client = client
2019-04-25 04:52:08 +07:00
}
2020-05-06 02:35:32 +07:00
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP client connected successfully" )
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
return nil
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Closes the connection properly.
2020-01-28 11:00:43 +07:00
func ( n * SMTPNotifier ) cleanup ( ) {
2021-01-17 06:23:35 +07:00
logger := logging . Logger ( )
2020-01-28 11:00:43 +07:00
err := n . client . Quit ( )
if err != nil {
2021-01-17 06:23:35 +07:00
logger . Warnf ( "Notifier SMTP client encountered error during cleanup: %s" , err )
2020-01-28 11:00:43 +07:00
}
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// StartupCheck checks the server is functioning correctly and the configuration is correct.
2020-04-23 09:01:24 +07:00
func ( n * SMTPNotifier ) StartupCheck ( ) ( bool , error ) {
if err := n . dial ( ) ; err != nil {
return false , err
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
defer n . cleanup ( )
2020-11-05 06:22:10 +07:00
if err := n . client . Hello ( n . identifier ) ; err != nil {
return false , err
}
2020-04-23 09:01:24 +07:00
if err := n . startTLS ( ) ; err != nil {
return false , err
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
2020-04-23 09:01:24 +07:00
if err := n . auth ( ) ; err != nil {
return false , err
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
2020-04-23 09:01:24 +07:00
if err := n . client . Mail ( n . sender ) ; err != nil {
return false , err
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
2020-04-23 09:01:24 +07:00
if err := n . client . Rcpt ( n . startupCheckAddress ) ; err != nil {
return false , err
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
2020-04-23 09:01:24 +07:00
if err := n . client . Reset ( ) ; err != nil {
return false , err
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
2020-04-23 09:01:24 +07:00
return true , nil
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
}
// Send is used to send an email to a recipient.
2020-08-21 09:16:23 +07:00
func ( n * SMTPNotifier ) Send ( recipient , title , body , htmlBody string ) error {
2021-01-17 06:23:35 +07:00
logger := logging . Logger ( )
2020-04-09 07:21:28 +07:00
subject := strings . ReplaceAll ( n . subject , "{title}" , title )
2020-05-06 02:35:32 +07:00
2020-01-28 11:00:43 +07:00
if err := n . dial ( ) ; err != nil {
2019-04-25 04:52:08 +07:00
return err
}
2019-12-30 09:03:51 +07:00
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Always execute QUIT at the end once we're connected.
2020-01-28 11:00:43 +07:00
defer n . cleanup ( )
2020-11-05 06:22:10 +07:00
if err := n . client . Hello ( n . identifier ) ; err != nil {
return err
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Start TLS and then Authenticate.
2020-04-21 12:53:47 +07:00
if err := n . startTLS ( ) ; err != nil {
2019-12-30 09:03:51 +07:00
return err
}
2020-05-06 02:35:32 +07:00
2020-04-21 12:53:47 +07:00
if err := n . auth ( ) ; err != nil {
2019-12-30 09:03:51 +07:00
return err
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Set the sender and recipient first.
2019-12-30 09:03:51 +07:00
if err := n . client . Mail ( n . sender ) ; err != nil {
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP failed while sending MAIL FROM (using sender) with error: %s" , err )
2019-12-30 09:03:51 +07:00
return err
}
2020-05-06 02:35:32 +07:00
2019-12-30 09:03:51 +07:00
if err := n . client . Rcpt ( recipient ) ; err != nil {
2021-01-17 06:23:35 +07:00
logger . Debugf ( "Notifier SMTP failed while sending RCPT TO (using recipient) with error: %s" , err )
2019-12-30 09:03:51 +07:00
return err
}
[FEATURE] Notifier Startup Checks (#889)
* implement SMTP notifier startup check
* check dial, starttls, auth, mail from, rcpt to, reset, and quit
* log the error on failure
* implement mock
* misc optimizations, adjustments, and refactoring
* implement validate_skip config option
* fix comments to end with period
* fix suites that used smtp notifier without a smtp container
* add docs
* add file notifier startup check
* move file mode into const.go
* disable gosec linting on insecureskipverify since it's intended, warned, and discouraged
* minor PR commentary adjustment
* apply suggestions from code review
Co-Authored-By: Amir Zarrinkafsh <nightah@me.com>
2020-04-21 11:59:38 +07:00
// Compose and send the email body to the server.
2020-08-21 09:16:23 +07:00
if err := n . compose ( recipient , subject , body , htmlBody ) ; err != nil {
2019-04-25 04:52:08 +07:00
return err
}
2021-01-17 06:23:35 +07:00
logger . Debug ( "Notifier SMTP client successfully sent email" )
2020-05-06 02:35:32 +07:00
2019-04-25 04:52:08 +07:00
return nil
}