From 24dcd62951b9fa1851f737c30abd9184e48045e0 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Wed, 17 Oct 2012 21:47:48 -0700 Subject: [PATCH] Basic HTML email rendering Messages with a MIME text/html MIME part will now be displayed with an `HTML` button above them that will open another window and render the HTML when clicked. There is no sanitization performed, and inline attachment display is not support. This closes #2 --- app/controllers/mailbox.go | 36 +++++++++++++++++++++++++++++++++++- app/views/Mailbox/Html.html | 1 + app/views/Mailbox/Index.html | 6 ++++++ app/views/Mailbox/Show.html | 3 +++ conf/routes | 1 + 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/views/Mailbox/Html.html diff --git a/app/controllers/mailbox.go b/app/controllers/mailbox.go index c4bc434..389c136 100644 --- a/app/controllers/mailbox.go +++ b/app/controllers/mailbox.go @@ -3,6 +3,7 @@ package controllers import ( "github.com/jhillyerd/inbucket/app/inbucket" "github.com/robfig/revel" + "html/template" ) type Mailbox struct { @@ -69,9 +70,10 @@ func (c Mailbox) Show(name string, id string) rev.Result { return c.RenderError(err) } body := mime.Text + htmlAvailable := mime.Html != "" c.Response.Out.Header().Set("Expires", "-1") - return c.Render(name, message, body) + return c.Render(name, message, body, htmlAvailable) } func (c Mailbox) Delete(name string, id string) rev.Result { @@ -97,9 +99,41 @@ func (c Mailbox) Delete(name string, id string) rev.Result { if err != nil { return c.RenderError(err) } + c.Response.Out.Header().Set("Expires", "-1") return c.RenderText("OK") } +func (c Mailbox) Html(name string, id string) rev.Result { + c.Validation.Required(name).Message("Account name is required") + c.Validation.Required(id).Message("Message ID is required") + + if c.Validation.HasErrors() { + c.Validation.Keep() + c.FlashParams() + return c.Redirect(Application.Index) + } + + ds := inbucket.NewDataStore() + mb, err := ds.MailboxFor(name) + if err != nil { + return c.RenderError(err) + } + message, err := mb.GetMessage(id) + if err != nil { + return c.RenderError(err) + } + _, mime, err := message.ReadBody() + if err != nil { + return c.RenderError(err) + } + // Mark as safe to render HTML + // TODO: It is not really safe to render, need to sanitize. + body := template.HTML(mime.Html) + + c.Response.Out.Header().Set("Expires", "-1") + return c.Render(name, message, body) +} + func (c Mailbox) Source(name string, id string) rev.Result { c.Validation.Required(name).Message("Account name is required") c.Validation.Required(id).Message("Message ID is required") diff --git a/app/views/Mailbox/Html.html b/app/views/Mailbox/Html.html new file mode 100644 index 0000000..1d10587 --- /dev/null +++ b/app/views/Mailbox/Html.html @@ -0,0 +1 @@ +{{.body}} diff --git a/app/views/Mailbox/Index.html b/app/views/Mailbox/Index.html index 800cf25..cb98a0c 100644 --- a/app/views/Mailbox/Index.html +++ b/app/views/Mailbox/Index.html @@ -44,6 +44,12 @@ }) } + function htmlView(id) { + window.open('/mailbox/html/{{$name}}/' + id, '_blank', + 'width=800,height=600,' + + 'menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes') + } + function messageSource(id) { window.open('/mailbox/source/{{$name}}/' + id, '_blank', 'width=800,height=600,' + diff --git a/app/views/Mailbox/Show.html b/app/views/Mailbox/Show.html index c213490..53a5367 100644 --- a/app/views/Mailbox/Show.html +++ b/app/views/Mailbox/Show.html @@ -1,6 +1,9 @@
Delete Source + {{if .htmlAvailable}} + HTML + {{end}}
diff --git a/conf/routes b/conf/routes index 9c5a041..b7633cd 100644 --- a/conf/routes +++ b/conf/routes @@ -8,6 +8,7 @@ GET /mailbox Mailbox.Index GET /mailbox/list/{name} Mailbox.List GET /mailbox/show/{name}/{id} Mailbox.Show GET /mailbox/source/{name}/{id} Mailbox.Source +GET /mailbox/html/{name}/{id} Mailbox.Html POST /mailbox/delete/{name}/{id} Mailbox.Delete # Ignore favicon requests