diff --git a/config_sample.json b/config_sample.json index 6f63c3f..cbedc6b 100644 --- a/config_sample.json +++ b/config_sample.json @@ -13,5 +13,13 @@ ], "ip_url": "http://members.3322.org/dyndns/getip", "log_path": "./godns.log", - "socks5_proxy": "" + "socks5_proxy": "", + "notify": { + "enabled": false, + "smtp_server": "", + "smtp_username": "", + "smtp_password": "", + "smtp_port": 25, + "send_to": "", + } } diff --git a/handler/dnspod_handler.go b/handler/dnspod_handler.go index 483f11f..cc96816 100644 --- a/handler/dnspod_handler.go +++ b/handler/dnspod_handler.go @@ -76,7 +76,8 @@ func (handler *DNSPodHandler) DomainLoop(domain *godns.Domain, panicChan chan<- // Send mail notification if notify is enabled if handler.Configuration.Notify.Enabled { - godns.SendNotify(handler.Configuration, currentIP) + log.Print("Sending notification to:", handler.Configuration.Notify.SendTo) + godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP) } } else { diff --git a/handler/he_handler.go b/handler/he_handler.go index ce31dd1..d11a2ab 100644 --- a/handler/he_handler.go +++ b/handler/he_handler.go @@ -61,7 +61,8 @@ func (handler *HEHandler) DomainLoop(domain *godns.Domain, panicChan chan<- godn // Send mail notification if notify is enabled if handler.Configuration.Notify.Enabled { - godns.SendNotify(handler.Configuration, currentIP) + log.Print("Sending notification to:", handler.Configuration.Notify.SendTo) + godns.SendNotify(handler.Configuration, fmt.Sprintf("%s.%s", subDomain, domain.DomainName), currentIP) } } } diff --git a/settings.go b/settings.go index 0d35613..fd3cf1f 100644 --- a/settings.go +++ b/settings.go @@ -13,12 +13,12 @@ type Domain struct { } type Notify struct { - Enabled bool `json:"enabled"` - SMTPServer string `json:"smtp_server"` - SendTo string `json:"send_to"` - Port int `json:"port"` - Account string `json:"account"` - Password string `json:"password"` + Enabled bool `json:"enabled"` + SMTPServer string `json:"smtp_server"` + SMTPUsername string `json:"smtp_username"` + SMTPPassword string `json:"smtp_password"` + SMTPPort int `json:"smtp_port"` + SendTo string `json:"send_to"` } // Settings struct diff --git a/template.go b/template.go new file mode 100644 index 0000000..aec9221 --- /dev/null +++ b/template.go @@ -0,0 +1,105 @@ +package godns + +var template = ` + + +
+
+
+
+ +
+ +
+
 
+
+ +
+ +
+
+
+ +
+
+
+ +
+ +
+
 
+
+ +
+
+

+ + Your IP address is changed to + +

+

+ + + %s + + +

+

+ + Domain: %s is updated + +

+
+
+ +
+
 
+
+ +
+
 
+
+ +
+ +
+
+
+ +
 
+ + +
+ +
+
 
+ +
+ +` diff --git a/utils.go b/utils.go index 4e767e7..69d0b6d 100644 --- a/utils.go +++ b/utils.go @@ -2,6 +2,7 @@ package godns import ( "errors" + "fmt" "io/ioutil" "log" "net/http" @@ -105,18 +106,21 @@ func LoadCurrentIP() string { } // SendNotify sends mail notify if IP is changed -func SendNotify(configuration *Settings, currentIP string) error { +func SendNotify(configuration *Settings, domain string, currentIP string) error { m := gomail.NewMessage() - m.SetHeader("From", configuration.Notify.Account) + m.SetHeader("From", configuration.Notify.SMTPUsername) m.SetHeader("To", configuration.Notify.SendTo) m.SetHeader("Subject", "GoDNS Notification") - m.SetBody("text/html", "") + log.Println("currentIP:", currentIP) + log.Println("domain:", domain) + m.SetBody("text/html", fmt.Sprintf(template, currentIP, domain)) - d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.Port, configuration.Notify.Account, configuration.Notify.Password) + d := gomail.NewPlainDialer(configuration.Notify.SMTPServer, configuration.Notify.SMTPPort, configuration.Notify.SMTPUsername, configuration.Notify.SMTPPassword) // Send the email config by sendlist . if err := d.DialAndSend(m); err != nil { + log.Println("Send email notification with error:", err.Error()) return err } return nil