Skip to content

Commit

Permalink
Parse To lines in the message itself. Fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
t0xicCode committed Aug 26, 2015
1 parent 68340da commit 5e40bfe
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package cmd

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/smtp"
"os"
"os/user"
"regexp"
"strings"

"github.com/ogier/pflag"
)

// Go runs the MailHog sendmail replacement.
func Go() {
smtpAddr := "localhost:1025"

Expand Down Expand Up @@ -55,17 +58,28 @@ func Go() {
recip = pflag.Args()
}

if len(recip) == 0 {
fmt.Fprintln(os.Stderr, "missing recipient")
os.Exit(10)
}

body, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "error reading stdin")
os.Exit(11)
}

if len(recip) == 0 {
// We only need to parse the message to get a recipient if none where
// provided on the command line.
re := regexp.MustCompile("(?im)^To: (.*)\r\n$")
n := bytes.IndexByte(body, 0)
bodyStr := string(body[:n])
includedRecip := re.FindAllString(bodyStr, -1)
if includedRecip == nil {
fmt.Fprintln(os.Stderr, "missing recipient")
os.Exit(10)
}
newRecip := make([]string, len(recip), len(recip)+len(includedRecip)+1)

This comment has been minimized.

Copy link
@t0xicCode

t0xicCode Oct 6, 2015

Author Contributor

Actually, I think we can drop the +1

This comment has been minimized.

Copy link
@ian-kent

ian-kent Oct 6, 2015

Member

yeah, we can 😄 thanks!

copy(newRecip, recip)
recip = append(newRecip, includedRecip...)
}

err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
if err != nil {
fmt.Fprintln(os.Stderr, "error sending mail")
Expand Down

0 comments on commit 5e40bfe

Please sign in to comment.