Skip to content

Commit

Permalink
refactor to support MailHog sendmail shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-kent committed May 26, 2015
1 parent cba2dca commit c88cf12
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
52 changes: 52 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/smtp"
"os"
"os/user"
)

func Go() {
smtpAddr := "localhost:1025"

host, err := os.Hostname()
if err != nil {
host = "localhost"
}

username := "nobody"
user, err := user.Current()
if err == nil && user != nil && len(user.Username) > 0 {
username = user.Username
}

fromAddr := username + "@" + host

flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
flag.StringVar(&fromAddr, "from", fromAddr, "SMTP sender")

flag.Parse()

recip := flag.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)
}

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

}
44 changes: 2 additions & 42 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
package main

import (
"flag"
"io/ioutil"
"log"
"net/smtp"
"os"
"os/user"
)
import "github.com/mailhog/mhsendmail/cmd"

func main() {
smtpAddr := "localhost:1025"

host, err := os.Hostname()
if err != nil {
host = "localhost"
}

username := "nobody"
user, err := user.Current()
if err == nil && user != nil && len(user.Username) > 0 {
username = user.Username
}

fromAddr := username + "@" + host

flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
flag.StringVar(&fromAddr, "from", fromAddr, "SMTP sender")

flag.Parse()

recip := flag.Args()
if len(recip) == 0 {
os.Exit(10)
}

body, err := ioutil.ReadAll(os.Stdin)
if err != nil {
os.Exit(11)
}

err = smtp.SendMail(smtpAddr, nil, fromAddr, recip, body)
if err != nil {
log.Fatal(err)
}
cmd.Go()
}

0 comments on commit c88cf12

Please sign in to comment.