Skip to content

Commit

Permalink
10-Email-Support add sendmail function
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfy committed Oct 9, 2018
1 parent 4609f2b commit f741e78
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ mysql:
db: dbname
host: localhost
password: password
user: root
user: root
mail:
smtp: smtp-server
smtp-port: 587
user: user
password: pwd

9 changes: 9 additions & 0 deletions config/g.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ func GetMysqlConnectingString() string {

return fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=%s&parseTime=true&loc=Local", usr, pwd, host, db, charset)
}

// GetSMTPConfig func
func GetSMTPConfig() (server string, port int, user, pwd string) {
server = viper.GetString("mail.smtp")
port = viper.GetInt("mail.smtp-port")
user = viper.GetString("mail.user")
pwd = viper.GetString("mail.password")
return
}
25 changes: 25 additions & 0 deletions controller/utils.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package controller

import (
"crypto/tls"
"errors"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"

"github.com/bonfy/go-mega-code/config"
"github.com/bonfy/go-mega-code/vm"
gomail "gopkg.in/gomail.v2"
)

// PopulateTemplates func
Expand Down Expand Up @@ -209,3 +213,24 @@ func getPage(r *http.Request) int {
}
return page
}

// Email

// sendEmail func
func sendEmail(target, subject, content string) {
server, port, usr, pwd := config.GetSMTPConfig()
d := gomail.NewDialer(server, port, usr, pwd)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

m := gomail.NewMessage()
m.SetHeader("From", usr)
m.SetHeader("To", target)
m.SetAddressHeader("Cc", usr, "admin")
m.SetHeader("Subject", subject)
m.SetBody("text/html", content)

if err := d.DialAndSend(m); err != nil {
log.Println("Email Error:", err)
return
}
}

0 comments on commit f741e78

Please sign in to comment.