hlfw.ca

webbing

Download patch

ref: a3e3db54c9b9ea7d2a24030b539ebd98cb230017
parent: 3f83ff6570efee6e4b8f08371ea74bdd7712e30e
parent: c455683cc4d9755fd6e30b5a8f4dc507f049cbf8
author: halfwit <michaelmisch1985@gmail.com>
date: Thu Aug 6 05:32:00 PDT 2020

Merge remote-tracking branch 'email/master' into master

--- /dev/null
+++ b/form.go
@@ -1,0 +1,89 @@
+package email
+
+import (
+	"bytes"
+	"log"
+	"mime/multipart"
+	"net/mail"
+	"net/smtp"
+	"text/template"
+
+	"github.com/scorredoira/email"
+	"golang.org/x/text/message"
+)
+
+const tmpl = `
+<!DOCTYPE html>
+<html>
+	<title>{{index .pagetitle 0}}</title>
+<head>
+	
+</head>
+<body>
+	<h2>{{index .pagetitle 0}}</h2>
+	<ul>
+	{{range $key, $value := . }}
+		{{if eq $key "pagetitle"}}
+		{{else if eq $key "country" }}
+			<li>countries: {{range $value}}{{.}} {{end}}</li>
+		{{else if eq $key "specialty" }}
+			<li>specialties: {{range $value}}{{.}} {{end}}</li>
+		{{else}}
+			{{$val := len $value}}
+			{{if eq $val 2}}
+				<li>{{$key}}: {{index $value 1 }}</li>
+			{{else}}
+				<li>{{$key}}: {{index $value 0 }}</li>
+			{{end}}
+		{{end}}
+	{{end}}
+	</ul>
+</body>
+</html>
+`
+
+var t *template.Template
+
+func init() {
+	t = template.Must(template.New("email").Parse(tmpl))
+}
+
+// SendForm - Fill in the template and send it out from our email address
+func SendForm(form map[string][]string, p *message.Printer, attachments map[string]multipart.File) {
+	var body bytes.Buffer
+
+	// Stash the address we want to send to
+	address := form["sendto"][0]
+	delete(form, "sendto")
+
+	if e := t.Execute(&body, form); e != nil {
+		log.Println(e)
+		return
+	}
+
+	m := email.NewHTMLMessage("Form contents", body.String())
+	m.From = mail.Address{
+		Name:    "From",
+		Address: gmail,
+	}
+
+	m.AddTo(mail.Address{
+		Name:    "To",
+		Address: address,
+	})
+
+	for name, buff := range attachments {
+		var attc bytes.Buffer
+
+		attc.ReadFrom(buff)
+
+		if err := m.AttachBuffer(name, attc.Bytes(), false); err != nil {
+			log.Println(err)
+		}
+	}
+
+	auth := smtp.PlainAuth("", gmail, pw, addr)
+	if err := email.Send(addr+":587", auth, m); err != nil {
+		log.Println(err)
+	}
+}
--- /dev/null
+++ b/go.mod
@@ -1,0 +1,9 @@
+module github.com/olmaxmedical/email
+
+go 1.14
+
+require (
+	github.com/olmaxmedical/database v0.0.2-tokens
+	github.com/scorredoira/email v0.0.0-20191107070024-dc7b732c55da
+	golang.org/x/text v0.3.2
+)
--- /dev/null
+++ b/go.sum
@@ -1,0 +1,11 @@
+github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
+github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
+github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
+github.com/olmaxmedical/database v0.0.2-tokens h1:Q5Aav9hayTHrdO4/eXy5MUgq/udpmVBeN/5ENJI3Njw=
+github.com/olmaxmedical/database v0.0.2-tokens/go.mod h1:fma8D7DaW61q+TX7RdYThh5tjbPCZLiqdvAnTVuyvbg=
+github.com/scorredoira/email v0.0.0-20191107070024-dc7b732c55da h1:hhmnjfzz7szp75AyXxn8tDfEA0oU4REQLmpuW6zNAOY=
+github.com/scorredoira/email v0.0.0-20191107070024-dc7b732c55da/go.mod h1:Q5ljvYIBpukMH+wgB8kcPV1i9NX8TqU++8GgBKq3pt0=
+golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
+golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
--- /dev/null
+++ b/reset.go
@@ -1,0 +1,44 @@
+package email
+
+import (
+	"bytes"
+	"context"
+	"fmt"
+	"log"
+	"net/smtp"
+
+	"github.com/olmaxmedical/database"
+	"golang.org/x/text/message"
+)
+
+// SendReset - Wrapper for resetmail and timeout
+// These all need context
+func SendReset(ctx context.Context, db *database.Database, user *database.User, email, password string, p *message.Printer) {
+	token := db.CreateTempEntry(ctx, user, email, password)
+	resetemail(token, []string{email}, p)
+}
+
+// NextResetToken - A user has replied to the email about a password
+// Give them a new token and invalidate the old one
+func NextResetToken(ctx context.Context, db *database.Database, token string) string {
+	return db.ForwardToken(ctx, token)
+}
+
+func resetemail(token string, to []string, p *message.Printer) {
+	var msg bytes.Buffer
+
+	msg.WriteString("From: ")
+	msg.WriteString(gmail + "\n")
+	msg.WriteString("To: ")
+	msg.WriteString(to[0] + "\n")
+	msg.WriteString(p.Sprintf("Subject: Olmax Medical - Reset Your Password\n\n"))
+	msg.WriteString(p.Sprintf("Please click the following link to reset your password "))
+	msg.WriteString(fmt.Sprintf("%s/reset/%s\n", url, token))
+
+	auth := smtp.PlainAuth("", gmail, pw, addr)
+	data := msg.Bytes()
+
+	if e := smtp.SendMail(addr+":587", auth, gmail, to, data); e != nil {
+		log.Printf("smtp error: %v", e)
+	}
+}
--- /dev/null
+++ b/signup.go
@@ -1,0 +1,49 @@
+package email
+
+import (
+	"bytes"
+	"context"
+	"fmt"
+	"log"
+	"net/http"
+	"net/smtp"
+
+	"github.com/olmaxmedical/database"
+	"golang.org/x/text/message"
+)
+
+// These all need context
+// SendSignup - email our prospective clients and create tokens
+func SendSignup(ctx context.Context, db *database.Database, user *database.User, email, password string, p *message.Printer) {
+	token := db.CreateTempEntry(ctx, user, email, password)
+	signupemail(token, []string{email}, p)
+}
+
+// ValidateSignupToken - Make sure token is good
+func ValidateSignupToken(ctx context.Context, db *database.Database, w http.ResponseWriter, r *http.Request, token string) {
+	if e := db.UserFromTemp(ctx, token); e != nil {
+		http.Error(w, "Bad Request", 400)
+	}
+
+	http.Redirect(w, r, "/login.html", 302)
+	return
+}
+
+func signupemail(token string, to []string, p *message.Printer) {
+	var msg bytes.Buffer
+
+	msg.WriteString("From: ")
+	msg.WriteString(gmail + "\n")
+	msg.WriteString("To: ")
+	msg.WriteString(to[0] + "\n")
+	msg.WriteString(p.Sprintf("Subject: Olmax Medical - Verify your new account\n\n"))
+	msg.WriteString(p.Sprintf("Please click the following link to finalize your account creation "))
+	msg.WriteString(fmt.Sprintf("%s/activate/%s\n", url, token))
+
+	auth := smtp.PlainAuth("", gmail, pw, addr)
+	data := msg.Bytes()
+
+	if e := smtp.SendMail(addr+":587", auth, gmail, to, data); e != nil {
+		log.Printf("smtp error: %v", e)
+	}
+}
--- /dev/null
+++ b/url.go
@@ -1,0 +1,10 @@
+package email
+
+import "os"
+
+var (
+	addr = "smtp.gmail.com"
+	pw   = os.Getenv("[redacted]")
+	gmail = "[redacted]"
+	url  = "[redacted]"
+)