From 3e52620c29e6893ce1cf5e45ec4e00a37184edd6 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Wed, 28 Aug 2013 15:53:04 +0200 Subject: Added mailing interface (from other project) --- mailing/sendmail_mailer.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 mailing/sendmail_mailer.go (limited to 'mailing/sendmail_mailer.go') diff --git a/mailing/sendmail_mailer.go b/mailing/sendmail_mailer.go new file mode 100644 index 0000000..ad864fb --- /dev/null +++ b/mailing/sendmail_mailer.go @@ -0,0 +1,73 @@ +package mailing + +import ( + "errors" + "fmt" + "github.com/kch42/simpleconf" + "os/exec" +) + +// SendmailMailer is a Mailer implementation that sends mail using a sendmail-ish executable. +// +// Sendmail-ish in this context means: +// +// * The executable must accept the from address using the `-f ` switch +// * The executable must accept the to address as a parameter +// * The executable expects a mail (including all headers) on stdin that is terminated by EOF +type SendmailMailer struct { + Exec string + Args []string +} + +func (sm SendmailMailer) Mail(to, from string, msg []byte) (outerr error) { + off := len(sm.Args) + args := make([]string, off+3) + copy(args, sm.Args) + args[off] = "-f" + args[off+1] = from + args[off+2] = to + + cmd := exec.Command(sm.Exec, args...) + pipe, err := cmd.StdinPipe() + if err != nil { + return err + } + + if err = cmd.Start(); err != nil { + return err + } + defer func() { + pipe.Close() + err := cmd.Wait() + if outerr == nil { + outerr = err + } + }() + + _, err = pipe.Write(msg) + return err +} + +// SendmailMailerCreator creates an SendmailMailer using configuration values in the [mail] section. +// +// exec - The name of the executable +// argX - (optional) Additional arguments for the executable. X is a ascending number starting with 1 +func SendmailMailerCreator(conf simpleconf.Config) (Mailer, error) { + rv := SendmailMailer{} + var err error + + if rv.Exec, err = conf.GetString("mail", "exec"); err != nil { + return rv, errors.New("Missing [mail] exec config") + } + + for i := 1; ; i++ { + arg, err := conf.GetString("mail", fmt.Sprintf("arg%d", i)) + if err != nil { + break + } + + rv.Args = append(rv.Args, arg) + } + + return rv, nil +} -- cgit v1.2.3-54-g00ecf