Browse Source

Add the possibility to send attachments

Blallo 3 years ago
parent
commit
08f79e2e0f
3 changed files with 57 additions and 10 deletions
  1. 25 9
      config.go
  2. 4 0
      mail.go
  3. 28 1
      main.go

+ 25 - 9
config.go

@@ -3,6 +3,7 @@ package main
 import (
 	"errors"
 	"fmt"
+	"os"
 
 	toml "github.com/pelletier/go-toml"
 )
@@ -33,18 +34,19 @@ func (s ServerConfig) String() string {
 }
 
 type Config struct {
-	Server  *ServerConfig `toml:server,omitempty`
-	From    string        `toml:from,omitempty`
-	To      []string      `toml:to,omitempty`
-	Cc      []string      `toml:cc,omitempty`
-	Bcc     []string      `toml:bcc,omitempty`
-	Subject string        `toml:subject,omitempty`
-	Text    string        `toml:text,omitempty`
+	Server      *ServerConfig `toml:server,omitempty`
+	From        string        `toml:from,omitempty`
+	To          []string      `toml:to,omitempty`
+	Cc          []string      `toml:cc,omitempty`
+	Bcc         []string      `toml:bcc,omitempty`
+	Subject     string        `toml:subject,omitempty`
+	Text        string        `toml:text,omitempty`
+	Attachments []string      `toml:attachments,omitempty`
 }
 
 func (c Config) String() string {
-	return fmt.Sprintf(
-		"From: %s\nTo: %s\nCc: %s\nBcc: %s\nSubject: %s\nText:\n%s\nServer:\n%s",
+	msg := fmt.Sprintf(
+		"From: %s\nTo: %s\nCc: %s\nBcc: %s\nSubject: %s\nText:\n%s\nServer:\n%s\n",
 		c.From,
 		c.To,
 		c.Cc,
@@ -53,6 +55,11 @@ func (c Config) String() string {
 		c.Text,
 		c.Server,
 	)
+	msg += "Attachments:"
+	for _, attachment := range c.Attachments {
+		msg += fmt.Sprintf("  - %s\n", attachment)
+	}
+	return msg
 }
 
 func NewConfig() *Config {
@@ -118,6 +125,15 @@ func (c *Config) Validate() error {
 				msg += fmt.Sprintf("%s: pass a value either via command line (-%s) or in configuration file section (%s)\n", v.Param, v.CmdFlag, v.ConfFlag)
 			}
 		}
+	}
+	if len(c.Attachments) > 0 {
+		for _, file := range c.Attachments {
+			if _, err := os.Lstat(file); err != nil {
+				msg += fmt.Sprintf("Error with attachment: %s -> %s\n", file, err)
+			}
+		}
+	}
+	if msg != "" {
 		return errors.New(msg)
 	}
 	return nil

+ 4 - 0
mail.go

@@ -19,6 +19,10 @@ func formatMessage(config *Config) *mail.Message {
 	}
 	m.SetHeader("Subject", config.Subject)
 	m.SetBody("text/plain", config.Text)
+	for _, attachment := range config.Attachments {
+		Debug.F("Attacching: %s", attachment)
+		m.Attach(attachment)
+	}
 	Debug.F("Message to deliver:\n%s", m)
 
 	return m

+ 28 - 1
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"bufio"
+	"errors"
 	"flag"
 	"fmt"
 	"os"
@@ -12,6 +13,27 @@ import (
 var noVersion = "dev"
 var version string
 
+type attachmentList []string
+
+func (a *attachmentList) String() string {
+	var str string
+	for _, attachment := range *a {
+		str += fmt.Sprintf("%s\n", attachment)
+	}
+	return str
+}
+
+func (a *attachmentList) Set(file string) error {
+	if _, err := os.Lstat(file); err != nil {
+		return err
+	}
+	if file == "" {
+		return errors.New("no file provided")
+	}
+	*a = append(*a, file)
+	return nil
+}
+
 func readFromConsole(result chan string) {
 	var text, line string
 	var err error
@@ -73,6 +95,7 @@ func main() {
 	var encryption, dbg, versionFlag, interactive bool
 	var serverPortAux int
 	var serverPort int64
+	var attachments attachmentList
 
 	flag.BoolVar(&versionFlag, "version", false, "Prints the version and exits")
 	flag.StringVar(&configPath, "conf", "", "Path to a config file (defaults to /etc/sendmail.toml)")
@@ -89,6 +112,7 @@ func main() {
 	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.Var(&attachments, "attach", "Attachment to the mail (may be repeated)")
 	flag.Parse()
 
 	LogInit(dbg)
@@ -136,7 +160,8 @@ parameters:
 	password: %s
 	to: %s
 	from: %s
-	subject: %s`,
+	subject: %s
+	attachments: %s`,
 		configPath,
 		dbg,
 		serverAddress,
@@ -147,6 +172,7 @@ parameters:
 		to,
 		from,
 		subject,
+		attachments,
 	)
 
 	config := initializeConfig(configPath, section)
@@ -161,6 +187,7 @@ parameters:
 	config.Merge("Bcc", toList(bcc))
 	config.Merge("Subject", subject)
 	config.Merge("Text", text)
+	config.Merge("Attachments", attachments)
 
 	Debug.F("---\nPre-validation config\n%s", config)
 	err = config.Validate()