Skip to content

Commit

Permalink
Sending E-mail and reading a config for that
Browse files Browse the repository at this point in the history
  • Loading branch information
budden committed Mar 5, 2019
1 parent 616da3c commit 42b4bfa
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
secret-data.config.json
21 changes: 21 additions & 0 deletions email.go
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"github.com/go-mail/mail"
)

func playWithEmail() {
scd := &SecretConfigData
m := mail.NewMessage()
m.SetHeader("From", scd.SenderEMail)
m.SetHeader("To", scd.RecieverEMail)
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello world!")

d := mail.NewDialer(scd.SMTPServer, 587, scd.SMTPUser, scd.SMTPPassword)

if err := d.DialAndSend(m); err != nil {
fmt.Printf("Failed to send an E-mail, err = %#v\n", err)
}
}
9 changes: 9 additions & 0 deletions main.go
Expand Up @@ -11,11 +11,20 @@ package main
import (
"fmt" )

const ActuallySendEmailP = false

func main() {
playWithDb()
playWithPanic()
playWithNonce(16)
playWithHashAndSalt()
/// Uncomment next line to create secret-data.config.json.example
//saveSecretConfigDataExample()
loadSecretConfigData()
if (ActuallySendEmailP) {
playWithEmail()
} else {
fmt.Println("Bypassing sending E-mail due to ActuallySendEmailP == false") }
return }


Expand Down
8 changes: 8 additions & 0 deletions secret-data.config.json.example
@@ -0,0 +1,8 @@
{
"Comment": "Example config file. Copy this one to the secret-data.config.json and edit",
"RecieverEMail": "world@example.net",
"SMTPServer": "smtp.example.net",
"SMTPUser": "Кирилл",
"SMTPPassword": "bla-bla-bla",
"SenderEMail": "den@example.net"
}
68 changes: 68 additions & 0 deletions secret-data.go
@@ -0,0 +1,68 @@
package main

import (
"strings"
"fmt"
"encoding/json"
"io/ioutil"
"os"
// "github.com/flynn/json5"
)

type SecretConfigDataStruct struct {
Comment string
RecieverEMail string
SMTPServer string
SMTPUser string
SMTPPassword string
SenderEMail string
}

var SecretConfigData SecretConfigDataStruct

func (sds *SecretConfigDataStruct) SaveToFile(filename string) (err error) {
var text []byte
text, err = json.MarshalIndent(sds,""," ")
if err != nil {
return
}
err = ioutil.WriteFile(filename, text, 0600)
return
}

const ConfigFileName = "secret-data.config.json"

// for development
func saveSecretConfigDataExample() {
sds := SecretConfigDataStruct{
Comment: "Example config file. Copy this one to the secret-data.config.json and edit",
SenderEMail: "den@example.net",
RecieverEMail: "world@example.net",
SMTPServer: "smtp.example.net",
SMTPUser: "Кирилл",
SMTPPassword: "bla-bla-bla"}
err := sds.SaveToFile(ConfigFileName + ".example")
if err != nil {
panic(err) }}

func loadSecretConfigData() (err error) {
sds := &SecretConfigData
fn := ConfigFileName
if _, err = os.Stat(fn); os.IsNotExist(err) {
fmt.Printf("No config file %s found. Create one by copying from %s.example\n",
fn, fn)
return }
var bytes []byte
bytes, err = ioutil.ReadFile(fn)
if err != nil {
fmt.Printf("Unable to read config %s\n", fn)
return }
dec := json.NewDecoder(strings.NewReader(string(bytes)))
dec.DisallowUnknownFields()
err = dec.Decode(sds)
if err != nil {
fmt.Printf("Error reading config file %s: %#v\n", fn, err)
return }
fmt.Printf("playWithSecretConfigData returned %#v\n", sds)
return }

0 comments on commit 42b4bfa

Please sign in to comment.