Skip to content

Commit

Permalink
Added error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
superlinkx committed May 7, 2016
1 parent 089fcf0 commit a758671
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions quickmailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"net/smtp"
"net/textproto"
"path/filepath"

"github.com/jordan-wright/email"
Expand All @@ -14,6 +15,7 @@ type emailParams struct {
port string
passwd string
from string
fromname string
to string
subject string
body string
Expand All @@ -24,24 +26,42 @@ type emailParams struct {
func main() {
settings := getArgs()

e := email.NewEmail()
e.From = "<" + settings.from + ">"
e.To = []string{settings.to}
e.Subject = settings.subject
e.Text = []byte(settings.body)
fromline := "<" + settings.from + ">"

if settings.fromname != "" {
fromline = settings.fromname + " " + fromline
}

e := &email.Email{
To: []string{settings.to},
From: fromline,
Subject: settings.subject,
Text: []byte(settings.body),
Headers: textproto.MIMEHeader{},
}

if settings.attachFile != "" {
e.AttachFile(settings.attachFile)
_, err := e.AttachFile(settings.attachFile)
if err != nil {
panic(err)
}
}

if settings.attachDir != "" {
files, _ := filepath.Glob(settings.attachDir + "/*")
for _, elem := range files {
e.AttachFile(elem)
_, err := e.AttachFile(elem)
if err != nil {
panic(err)
}
}
}

e.Send(settings.sendserver, smtp.PlainAuth("", settings.from, settings.passwd, settings.server))
err := e.Send(settings.sendserver, smtp.PlainAuth("", settings.from, settings.passwd, settings.server))

if err != nil {
panic(err)
}
}

func getArgs() emailParams {
Expand All @@ -56,6 +76,7 @@ func getArgs() emailParams {
flag.StringVar(&params.body, "body", "See attached", "some body text")
flag.StringVar(&params.attachFile, "attach", "", "single file to attach")
flag.StringVar(&params.attachDir, "attachDir", "", "directory to attach files from")
flag.StringVar(&params.fromname, "fromName", "", "name to send email as")

flag.Parse()

Expand Down

1 comment on commit a758671

@superlinkx
Copy link
Owner Author

@superlinkx superlinkx commented on a758671 May 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1

Please sign in to comment.