Skip to content

Commit

Permalink
Revert "Allow SMTP LOGIN authentification (Fixed #53)" (#54)
Browse files Browse the repository at this point in the history
This reverts commit 462e68f.
  • Loading branch information
dieechtenilente authored Apr 6, 2018
1 parent 462e68f commit 017881f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 44 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ 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: 11 additions & 43 deletions feedback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,26 @@ package main

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

"gopkg.in/gomail.v1"
"os"
)

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 @@ -46,7 +33,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 @@ -85,8 +72,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 @@ -110,10 +97,10 @@ func rootHandler(w http.ResponseWriter, r *http.Request) {
}
}

// sendStdMail sends a mail with the given subject and content. The content must have Unix
// sendMail 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 sendStdMail(subject, content string) error {
func sendMail(subject, content string) error {
auth := smtp.PlainAuth("", FromAddr, Passwd, SMTPSrv)
to := []string{ToAddr}
date := time.Now().Format(time.RFC822Z)
Expand All @@ -122,24 +109,5 @@ func sendStdMail(subject, content string) error {
body := strings.Replace(content, "\n", "\r\n", -1)
msg := []byte(header + "\r\n" + body)

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+":"+SMTPPort, 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)
return smtp.SendMail(SMTPSrv+":"+SMTPPort, auth, FromAddr, to, msg)
}

0 comments on commit 017881f

Please sign in to comment.