-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 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
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,47 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/smtp" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
// SendMail is an helper to send mail all over the project | ||
func SendMail(to []string, subject, body string) { | ||
|
||
// Set up authentication information. | ||
auth := smtp.PlainAuth( | ||
"", | ||
viper.GetString("email.username"), | ||
viper.GetString("email.password"), | ||
viper.GetString("email.host"), | ||
) | ||
|
||
header := make(map[string]string) | ||
header["From"] = viper.GetString("email.from") | ||
header["To"] = to[0] | ||
header["Subject"] = subject | ||
header["MIME-Version"] = "1.0" | ||
header["Content-Type"] = "text/plain; charset=\"utf-8\"" | ||
|
||
message := "" | ||
for k, v := range header { | ||
message += fmt.Sprintf("%s: %s\r\n", k, v) | ||
} | ||
message += "\r\n" + body | ||
|
||
// Connect to the server, authenticate, set the sender and recipient, | ||
// and send the email all in one step. | ||
err := smtp.SendMail( | ||
viper.GetString("email.host")+":"+viper.GetString("email.port"), | ||
auth, | ||
viper.GetString("email.from"), | ||
to, | ||
[]byte(message), | ||
) | ||
if err != nil { | ||
log.Fatal(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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ type EmailConfiguration struct { | |
Username string `default:"[email protected]"` | ||
Password string `default:"password"` | ||
From string `default:"[email protected]"` | ||
Admin string `default:"[email protected]"` | ||
} | ||
|
||
// SetupConfigDefaults ... | ||
|
@@ -129,6 +130,7 @@ func bindEnvs() { | |
viper.BindEnv("email.username", "PW_EMAIL_USERNAME") | ||
viper.BindEnv("email.password", "PW_EMAIL_PASSWORD") | ||
viper.BindEnv("email.from", "PW_EMAIL_FROM") | ||
viper.BindEnv("email.admin", "PW_EMAIL_ADMIN") | ||
|
||
viper.BindEnv("backup.folder", "PW_BACKUP_FOLDER") | ||
viper.BindEnv("backup.rotation", "PW_BACKUP_ROTATION") | ||
|
@@ -160,6 +162,7 @@ func setDefaults() { | |
viper.SetDefault("email.username", "[email protected]") | ||
viper.SetDefault("email.password", "password") | ||
viper.SetDefault("email.from", "[email protected]") | ||
viper.SetDefault("email.admin", "[email protected]") | ||
|
||
// Backup defaults | ||
viper.SetDefault("backup.folder", "./store/") | ||
|