Skip to content

Commit

Permalink
Allow SMTP LOGIN authentification #53 (Fixed #51) (#55)
Browse files Browse the repository at this point in the history
* Expand section about feedback server

* feedback: add -L option that enables SMTP LOGIN auth
  • Loading branch information
dieechtenilente authored Apr 6, 2018
1 parent 017881f commit ff37afe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ Once the project is cloned to disk you can import into Android Studio:

The feedback server is used by the feedback feature in the app, which allows users to
send a message and general device diagnostics to the developer. It is an HTTPS server
written in [Go](https://golang.org/). It takes messages in JSON (see [below](https://github.com/dieechtenilente/alcoholtestapp#feedback) for the format)
and forwards them to the specified e-mail address.
written in [Go](https://golang.org/). It takes messages in JSON (see [below](https://github.com/dieechtenilente/alcoholtestapp#feedback) for the format) and forwards them to the specified e-mail address.

You need a new file called `config.go` in the `feedback` directory. The file is .gitignored
since it holds the password for the feedback server's mail account.
Expand Down
54 changes: 43 additions & 11 deletions feedback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,39 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/smtp"
"os"
"strings"
"time"
"os"

"gopkg.in/gomail.v1"
)

var sendMail = sendStdMail

var useLoginAuth = flag.Bool("L", false, "Use LOGIN auth")

func main() {
flag.Parse()

if *useLoginAuth {
sendMail = sendLoginMail
}

t := time.Now()
f, err := os.OpenFile("feedback" + t.Format("20060102-1504") + ".log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)

f, err := os.OpenFile("feedback"+t.Format("20060102-1504")+".log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
}
defer f.Close()
log.SetOutput(f)

if FromAddr == "[email protected]" {
log.Fatal("Please set the constants in config.go and recompile")
}
Expand All @@ -33,7 +46,7 @@ func main() {
}

http.HandleFunc("/", rootHandler)

log.Println("Starting server on port", Port)
log.Fatal(http.ListenAndServeTLS(":"+Port, CertificatePath, PrivateKeyPath, nil))
}
Expand Down Expand Up @@ -72,8 +85,8 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(strings.NewReader(fbstr))
var feedback Feedback
err := decoder.Decode(&feedback)
if err != nil {

if err != nil {
log.Println("JSON decode error:", err)
}

Expand All @@ -97,10 +110,10 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
}
}

// sendMail sends a mail with the given subject and content. The content must have Unix
// sendStdMail sends a mail with the given subject and content. The content must have Unix
// newlines (\n) and end in a newline. These newlines will be converted to SMTP's CRLF
// automatically.
func sendMail(subject, content string) error {
func sendStdMail(subject, content string) error {
auth := smtp.PlainAuth("", FromAddr, Passwd, SMTPSrv)
to := []string{ToAddr}
date := time.Now().Format(time.RFC822Z)
Expand All @@ -109,5 +122,24 @@ func sendMail(subject, content string) error {
body := strings.Replace(content, "\n", "\r\n", -1)
msg := []byte(header + "\r\n" + body)

return smtp.SendMail(SMTPSrv+":"+SMTPPort, auth, FromAddr, to, msg)
return smtp.SendMail(SMTPSrv+":"+Port, auth, FromAddr, to, msg)
}

// sendLoginMail uses the LOGIN auth implemented using gomail instead.
func sendLoginMail(subject, content string) error {
auth := gomail.LoginAuth(FromAddr, Passwd, SMTPSrv)
mailer := gomail.NewCustomMailer(SMTPSrv, auth)

body := strings.Replace(content, "\n", "\r\n", -1)

msg := gomail.NewMessage()
msg.SetHeaders(map[string][]string{
"From": {FromAddr},
"To": {ToAddr},
"Subject": {subject},
})
msg.SetDateHeader("Date", time.Now())
msg.SetBody("text/plain", body)

return mailer.Send(msg)
}

0 comments on commit ff37afe

Please sign in to comment.