Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sending E-mail and reading a config for that
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
secret-data.config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
|