Skip to content
Permalink
Browse files

10-Email-Support add sendmail function

  • Loading branch information...
bonfy committed Oct 9, 2018
1 parent 4609f2b commit f741e7875dba01644c7fe60faecc76c043fffe1e
Showing with 41 additions and 1 deletion.
  1. +7 −1 config.yml.sample
  2. +9 −0 config/g.go
  3. +25 −0 controller/utils.go
@@ -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

@@ -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
}
@@ -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
@@ -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.
You can’t perform that action at this time.