这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@ package cmd

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

"github.com/ogier/pflag"
)

import flag "github.com/spf13/pflag"

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

goflag := false
for _, g := range os.Args[1:] {
if strings.HasPrefix(g, "-") && !strings.HasPrefix(g, "--") {
if strings.HasPrefix(g, "-from ") || strings.HasPrefix(g, "-from=") ||
strings.HasPrefix(g, "-smtp-addr ") || strings.HasPrefix(g, "-smtp-addr=") {
goflag = true
break
}
}
}

host, err := os.Hostname()
if err != nil {
host = "localhost"
Expand All @@ -42,21 +27,27 @@ func Go() {
}

fromAddr := username + "@" + host
smtpAddr := "localhost:1025"
var recip []string

if goflag {
flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
flag.StringVar(&fromAddr, "from", fromAddr, "SMTP sender")
// defaults from envars if provided
if len(os.Getenv("MH_SENDMAIL_SMTP_ADDR")) > 0 {
smtpAddr = os.Getenv("MH_SENDMAIL_SMTP_ADDR")
}
if len(os.Getenv("MH_SENDMAIL_FROM")) > 0 {
fromAddr = os.Getenv("MH_SENDMAIL_FROM")
}

// override defaults from cli flags
flag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
flag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")
flag.Parse()

flag.Parse()
recip = flag.Args()
} else {
pflag.StringVar(&smtpAddr, "smtp-addr", smtpAddr, "SMTP server address")
pflag.StringVarP(&fromAddr, "from", "f", fromAddr, "SMTP sender")
// allow recipient to be passed as an argument
recip = flag.Args()

fmt.Fprintln(os.Stderr, smtpAddr, fromAddr)

pflag.Parse()
recip = pflag.Args()
}

body, err := ioutil.ReadAll(os.Stdin)
if err != nil {
Expand Down