sendmail/main.go

123 lines
3.1 KiB
Go

package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
func readFromConsole() string {
var text, line string
var err error
counter := 0
reader := bufio.NewReader(os.Stdin)
for counter < 3 {
line, err = reader.ReadString('\n')
if line == "\n" {
counter += 1
}
text += fmt.Sprintf("%s\n", line)
}
if err != nil {
Error.F("Error in reading text from console\n%s", err)
os.Exit(1)
}
return text
}
func toList(input string) []string {
var result = []string{}
list := strings.Split(input, ",")
for _, element := range list {
if element != "" {
result = append(result, element)
}
}
return result
}
func main() {
var err error
var configPath, section, serverAddress, user, password, to, cc, bcc, from, subject, text string
var encryption, dbg bool
var serverPort_ int
var serverPort int64
flag.StringVar(&configPath, "conf", "/etc/sendmail.toml", "Path to a config file (defaults to /etc/sendmail.toml)")
flag.StringVar(&section, "section", "default", "Section of the conf to read (defaults to \"default\")")
flag.BoolVar(&dbg, "dbg", false, "Enable debugging output")
flag.StringVar(&serverAddress, "server-address", "", "The SMTP server address")
flag.IntVar(&serverPort_, "server-port", 0, "The SMTP server")
flag.BoolVar(&encryption, "force-ssl", false, "Force the use of ssl (defalut: false)")
flag.StringVar(&user, "user", "", "The user to authenticate with to the server")
flag.StringVar(&password, "password", "", "The password to authenticate with to the server")
flag.StringVar(&to, "to", "", "Comma-separated list of recipient(s)")
flag.StringVar(&cc, "cc", "", "Comma-separated list of carbon-copy recipient(s)")
flag.StringVar(&bcc, "bcc", "", "Comma-separated list of blind carbon-copy recipient(s)")
flag.StringVar(&from, "from", "", "Sender of the mail (used as default account user to log in on the SMTP server)")
flag.StringVar(&subject, "sub", "", "Subject of the mail")
flag.Parse()
LogInit(dbg)
if flag.NArg() == 0 {
text = readFromConsole()
} else {
for _, arg := range flag.Args() {
text += fmt.Sprintf("%s\n", arg)
}
}
serverPort = int64(serverPort_)
Debug.F(
`
---
parameters:
conf: %s
dbg: %t
address: %s
port: %d
encryption: %t
user: %s
password: %s
to: %s
from: %s
subject: %s`,
configPath,
dbg,
serverAddress,
serverPort,
encryption,
user,
password,
to,
from,
subject,
)
config := readConfig(configPath, section)
Debug.F("---\nConfig from %s\n%s", configPath, *config)
config.Server.Merge("Address", serverAddress)
config.Server.Merge("Port", serverPort)
config.Server.Merge("Encryption", encryption)
config.Server.Merge("User", user)
config.Server.Merge("Password", password)
config.Merge("From", from)
config.Merge("To", toList(to))
config.Merge("Cc", toList(cc))
config.Merge("Bcc", toList(bcc))
config.Merge("Subject", subject)
config.Merge("Text", text)
Debug.F("---\nPre-validation config\n%s", config)
err = config.Validate()
if err != nil {
Error.F("Config validation failed:\n%s\n", err)
os.Exit(1)
}
Info.F("Sending mail | to: %s", config.To)
SendMail(config)
}