rafaeljusto / shelter

Program to periodically validate and alert domains about DNS or DNSSEC misconfiguration
GNU General Public License v2.0
8 stars 3 forks source link

Adding a "Date" header to notification emails. #129

Closed huguei closed 8 years ago

huguei commented 8 years ago

The sendmail function requires to set all headers for the email. The lack of a "Date" one was originating weird behaviour with some mail clients. I choose to add the header inside the template, just like "From". Hope it helps. Hugo

rafaeljusto commented 8 years ago

The patch is missing the declaration of the new attribute in the protocol.Domain type. Something like the bellow code:

diff --git a/net/mail/notification/protocol/domain.go b/net/mail/notification/protocol/domain.go
index 9c2465d..a769ebb 100644
--- a/net/mail/notification/protocol/domain.go
+++ b/net/mail/notification/protocol/domain.go
@@ -15,4 +15,5 @@ type Domain struct {
        model.Domain        // Domain object
        From         string // E-mails from header
        To           string // List of owner's e-mails to be alerted
+       Date         string // Date used in the e-mail header
 }

But I think that we could make this change simpler, like creating a function to be executed in the template to return the current date in the desired format (date would be always in UTC).

diff --git a/net/mail/notification/templates.go b/net/mail/notification/templates.go
index b838285..4ebe3f3 100644
--- a/net/mail/notification/templates.go
+++ b/net/mail/notification/templates.go
@@ -7,14 +7,15 @@ package notification

 import (
        "fmt"
-       "github.com/rafaeljusto/shelter/config"
-       "github.com/rafaeljusto/shelter/model"
        "io/ioutil"
        "path/filepath"
        "strings"
        "sync"
        "text/template"
        "time"
+
+       "github.com/rafaeljusto/shelter/config"
+       "github.com/rafaeljusto/shelter/model"
 )

 var (
@@ -84,6 +85,9 @@ func LoadTemplates() error {
                        "nsStatusEq":       nameserverStatusEquals,
                        "dsStatusEq":       dsStatusEquals,
                        "isNearExpiration": isNearExpirationDS,
+                       "dateNow": func() string {
+                               return time.Now().UTC().Format(time.RFC1123Z)
+                       },
                }).Parse(string(templateContent))

                if err != nil {
diff --git a/templates/notification/en-us.tmpl b/templates/notification/en-us.tmpl
index 5e8fd6f..01414b5 100644
--- a/templates/notification/en-us.tmpl
+++ b/templates/notification/en-us.tmpl
@@ -1,5 +1,6 @@
 {{$domain := .}}

+Date: {{dateNow}}
 From: {{.From}}
 To: {{.To}}
 Subject: Misconfiguration on domain {{$domain.FQDN}}
diff --git a/templates/notification/es-es.tmpl b/templates/notification/es-es.tmpl
index 3e87c0c..d7692ce 100644
--- a/templates/notification/es-es.tmpl
+++ b/templates/notification/es-es.tmpl
@@ -1,5 +1,6 @@
 {{$domain := .}}

+Date: {{dateNow}}
 From: {{.From}}
 To: {{.To}}
 Subject: Problema de configuración con el dominio {{$domain.FQDN}}
diff --git a/templates/notification/pt-br.tmpl b/templates/notification/pt-br.tmpl
index a4be459..ed2b9b5 100644
--- a/templates/notification/pt-br.tmpl
+++ b/templates/notification/pt-br.tmpl
@@ -1,5 +1,6 @@
 {{$domain := .}}

+Date: {{dateNow}}
 From: {{.From}}
 To: {{.To}}
 Subject: Problema de configuracao com o dominio {{$domain.FQDN}}
huguei commented 8 years ago

Yes, that makes sense. Modern email clients can handle dates in UTC. (I forgot to commit the protocol.Domain change, but the code was deployed and tested :). Thanks!

rafaeljusto commented 8 years ago

Solution in commit daac82602a663c480995be1140c1456505af6b0e

rafaeljusto commented 8 years ago

Deployed in release 0.3-28.