authelia/internal/notification/file_notifier.go
James Elliott a92b0bff1d
[FEATURE] Plain Text Email Notifications (#1238)
* add a plain text email template
* use plain text email template for file based emails
* add config option to SMTP emails named disable_html_emails
  * config option is a boolean that when set to true will only send plain text emails
* add docs for more complex SMTP notifier options
* update template
* add rfc1341 multipart logic to notifier
* check for errors after identity_verification

* * fix nil ptr
* go mod tidy
* remove needless checks

* * use multipart/atlernative instead

* * add rfc5322 compliant date header

* * fix linting issues
2020-08-21 12:16:23 +10:00

65 lines
1.5 KiB
Go

package notification
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/authelia/authelia/internal/configuration/schema"
)
// FileNotifier a notifier to send emails to SMTP servers.
type FileNotifier struct {
path string
}
// NewFileNotifier create an FileNotifier writing the notification into a file.
func NewFileNotifier(configuration schema.FileSystemNotifierConfiguration) *FileNotifier {
return &FileNotifier{
path: configuration.Filename,
}
}
// StartupCheck checks the file provider can write to the specified file.
func (n *FileNotifier) StartupCheck() (bool, error) {
dir := filepath.Dir(n.path)
if _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(dir, fileNotifierMode); err != nil {
return false, err
}
} else {
return false, err
}
} else if _, err = os.Stat(n.path); err != nil {
if !os.IsNotExist(err) {
return false, err
}
} else {
if err = os.Remove(n.path); err != nil {
return false, err
}
}
if err := ioutil.WriteFile(n.path, []byte(""), fileNotifierMode); err != nil {
return false, err
}
return true, nil
}
// Send send a identity verification link to a user.
func (n *FileNotifier) Send(recipient, subject, body, _ string) error {
content := fmt.Sprintf("Date: %s\nRecipient: %s\nSubject: %s\nBody: %s", time.Now(), recipient, subject, body)
err := ioutil.WriteFile(n.path, []byte(content), fileNotifierMode)
if err != nil {
return err
}
return nil
}