Skip to content

Commit

Permalink
V0.1 Working on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
superlinkx committed May 4, 2016
1 parent c488c72 commit 1c3cd54
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
#Simple SMTP client written in Go

Usage: quickmailer -server smtp.gmail.com -passwd password -port 587 -from [email protected] -to [email protected] -attachDir "/home/user/someDir"

Or: quickmailer.exe -server smtp.gmail.com -passwd password -port 587 -from [email protected] -to [email protected] -attachDir "C:\Users\Me\someDir"

Additonal parameters:

-body
-subject
-attachFile
83 changes: 48 additions & 35 deletions quickmailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,67 @@ package main
import (
"flag"
"net/smtp"
"path/filepath"

"github.com/jordan-wright/email"
)

type emailParams struct {
server string
sendserver string
port string
passwd string
from string
to string
subject string
body string
attachFile string
attachDir string
}

func main() {
var (
server string
port string
passwd string
from string
to string
subject string
body string
attachFile string
attachDir string
)

flag.StringVar(&server, "server", "", "smtp server without port to use")
flag.StringVar(&port, "port", "587", "smtp port to use")
flag.StringVar(&passwd, "passwd", "", "password for smtp auth")
flag.StringVar(&from, "from", "", "email to send from")
flag.StringVar(&to, "to", "", "email to send to")
flag.StringVar(&subject, "subject", "Your Files", "subject line")
flag.StringVar(&body, "body", "See attached", "some body text")
flag.StringVar(&attachFile, "attach", "", "single file to attach")
flag.StringVar(&attachDir, "attachDir", "", "directory to attach files from")
settings := getArgs()

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

if (server == "") || (port == "") || (passwd == "") || (from == "") || (to == "") {
panic("Required flags: -server, -passwd, -port, -from, -to")
if settings.attachFile != "" {
e.AttachFile(settings.attachFile)
}

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

e := email.NewEmail()
e.From = "<" + from + ">"
e.To = []string{to}
e.Subject = subject
e.Text = []byte(body)
e.Send(settings.sendserver, smtp.PlainAuth("", settings.from, settings.passwd, settings.server))
}

if attachFile != "" {
e.AttachFile(attachFile)
}
func getArgs() emailParams {
var params emailParams

flag.StringVar(&params.server, "server", "", "smtp server without port to use")
flag.StringVar(&params.port, "port", "587", "smtp port to use")
flag.StringVar(&params.passwd, "passwd", "", "password for smtp auth")
flag.StringVar(&params.from, "from", "", "email to send from")
flag.StringVar(&params.to, "to", "", "email to send to")
flag.StringVar(&params.subject, "subject", "Your Files", "subject line")
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")

if attachDir != "" {
flag.Parse()

if (params.server == "") || (params.port == "") || (params.passwd == "") || (params.from == "") || (params.to == "") {
panic("Required flags: -server, -passwd, -port, -from, -to")
}

e.Send(sendserver, smtp.PlainAuth("", from, passwd, server))
params.sendserver = params.server + ":" + params.port

return params
}

0 comments on commit 1c3cd54

Please sign in to comment.