Skip to content

Commit

Permalink
added SendEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
yakuter committed Jul 18, 2020
1 parent 017eb3d commit 4e0bc93
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# PASSWALL CHANGELOG

## Version: [1.1.-] (2020-07-**)
### Add
- Email configuration
- SendEmail function to app package
### Changed
- Github organization name pass-wall to passwall

Expand Down
47 changes: 47 additions & 0 deletions internal/app/send_email.go
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)
}
}
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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/")
Expand Down

0 comments on commit 4e0bc93

Please sign in to comment.