1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

compile template

This commit is contained in:
Timothy 2017-11-06 17:26:40 +08:00
parent 7a65d4e0ea
commit 0155dee427
2 changed files with 29 additions and 6 deletions

View File

@ -1,6 +1,6 @@
package godns package godns
var template = ` var MailTemplate = `
<html> <html>
<body> <body>
<div role="section"> <div role="section">
@ -42,14 +42,14 @@ var template = `
lang="x-size-48"> lang="x-size-48">
<span class="font-avenir"> <span class="font-avenir">
<strong> <strong>
<span style="color:#ffffff">%s</span> <span style="color:#ffffff">{{ .CurrentIP }}</span>
</strong> </strong>
</span> </span>
</h1> </h1>
<h2 class="size-28" style="Margin-top: 20px;Margin-bottom: 16px;font-style: normal;font-weight: normal;color: #e31212;font-size: 24px;line-height: 32px;font-family: Avenir,sans-serif;text-align: center;" <h2 class="size-28" style="Margin-top: 20px;Margin-bottom: 16px;font-style: normal;font-weight: normal;color: #e31212;font-size: 24px;line-height: 32px;font-family: Avenir,sans-serif;text-align: center;"
lang="x-size-28"> lang="x-size-28">
<font color="#ffffff"> <font color="#ffffff">
<strong>Domain: %s is updated</strong> <strong>Domain {{ .Domain }} is updated</strong>
</font> </font>
</h2> </h2>
</div> </div>

View File

@ -1,8 +1,9 @@
package godns package godns
import ( import (
"bytes"
"errors" "errors"
"fmt" "html/template"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -106,7 +107,7 @@ func LoadCurrentIP() string {
} }
// SendNotify sends mail notify if IP is changed // SendNotify sends mail notify if IP is changed
func SendNotify(configuration *Settings, domain string, currentIP string) error { func SendNotify(configuration *Settings, domain, currentIP string) error {
m := gomail.NewMessage() m := gomail.NewMessage()
m.SetHeader("From", configuration.Notify.SMTPUsername) m.SetHeader("From", configuration.Notify.SMTPUsername)
@ -114,7 +115,7 @@ func SendNotify(configuration *Settings, domain string, currentIP string) error
m.SetHeader("Subject", "GoDNS Notification") m.SetHeader("Subject", "GoDNS Notification")
log.Println("currentIP:", currentIP) log.Println("currentIP:", currentIP)
log.Println("domain:", domain) log.Println("domain:", domain)
m.SetBody("text/html", fmt.Sprintf(template, currentIP, domain)) m.SetBody("text/html", buildTemplate(currentIP, domain))
d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.SMTPPort, configuration.Notify.SMTPUsername, configuration.Notify.SMTPPassword) d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.SMTPPort, configuration.Notify.SMTPUsername, configuration.Notify.SMTPPassword)
@ -125,3 +126,25 @@ func SendNotify(configuration *Settings, domain string, currentIP string) error
} }
return nil return nil
} }
func buildTemplate(currentIP, domain string) string {
t := template.New("notification template")
t.Parse(MailTemplate)
data := struct {
CurrentIP string
Domain string
}{
currentIP,
domain,
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
log.Println(err.Error())
return ""
}
log.Println("result:", tpl.String())
return tpl.String()
}