mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
Basic MIME parsing is now integrated, this closes #1
This commit is contained in:
@@ -64,10 +64,11 @@ func (c Mailbox) Show(name string, id string) rev.Result {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return c.RenderError(err)
|
return c.RenderError(err)
|
||||||
}
|
}
|
||||||
_, body, err := message.ReadBody()
|
_, mime, err := message.ReadBody()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.RenderError(err)
|
return c.RenderError(err)
|
||||||
}
|
}
|
||||||
|
body := mime.Text
|
||||||
|
|
||||||
c.Response.Out.Header().Set("Expires", "-1")
|
c.Response.Out.Header().Set("Expires", "-1")
|
||||||
return c.Render(name, message, body)
|
return c.Render(name, message, body)
|
||||||
|
|||||||
@@ -174,8 +174,10 @@ func (m *Message) ReadHeader() (msg *mail.Message, err error) {
|
|||||||
return msg, err
|
return msg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadBody opens the .raw portion of a Message and returns a standard Go mail.Message object
|
// ReadBody opens the .raw portion of a Message and returns a MIMEBody object, along
|
||||||
func (m *Message) ReadBody() (msg *mail.Message, body *string, err error) {
|
// with a free mail.Message containing the Headers, since we had to make one of those
|
||||||
|
// anyway.
|
||||||
|
func (m *Message) ReadBody() (msg *mail.Message, body *MIMEBody, err error) {
|
||||||
file, err := os.Open(m.rawPath())
|
file, err := os.Open(m.rawPath())
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -186,12 +188,11 @@ func (m *Message) ReadBody() (msg *mail.Message, body *string, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
bodyBytes, err := ioutil.ReadAll(reader)
|
mime, err := ParseMIMEBody(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
bodyString := string(bodyBytes)
|
return msg, mime, err
|
||||||
return msg, &bodyString, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadRaw opens the .raw portion of a Message and returns it as a string
|
// ReadRaw opens the .raw portion of a Message and returns it as a string
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"container/list"
|
"container/list"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"mime"
|
"mime"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
@@ -20,7 +21,7 @@ type MIMENode struct {
|
|||||||
Content []byte
|
Content []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type MIMEMessage struct {
|
type MIMEBody struct {
|
||||||
Text string
|
Text string
|
||||||
Html string
|
Html string
|
||||||
Root *MIMENode
|
Root *MIMENode
|
||||||
@@ -79,9 +80,17 @@ func IsMIMEMessage(mailMsg *mail.Message) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseMIMEMessage(mailMsg *mail.Message) (*MIMEMessage, error) {
|
func ParseMIMEBody(mailMsg *mail.Message) (*MIMEBody, error) {
|
||||||
mimeMsg := new(MIMEMessage)
|
mimeMsg := new(MIMEBody)
|
||||||
|
|
||||||
|
if !IsMIMEMessage(mailMsg) {
|
||||||
|
// Parse as text only
|
||||||
|
bodyBytes, err := ioutil.ReadAll(mailMsg.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mimeMsg.Text = string(bodyBytes)
|
||||||
|
} else {
|
||||||
// Parse top-level multipart
|
// Parse top-level multipart
|
||||||
ctype := mailMsg.Header.Get("Content-Type")
|
ctype := mailMsg.Header.Get("Content-Type")
|
||||||
mediatype, params, err := mime.ParseMediaType(ctype)
|
mediatype, params, err := mime.ParseMediaType(ctype)
|
||||||
@@ -118,11 +127,12 @@ func ParseMIMEMessage(mailMsg *mail.Message) (*MIMEMessage, error) {
|
|||||||
if match != nil {
|
if match != nil {
|
||||||
mimeMsg.Html = string(match.Content)
|
mimeMsg.Html = string(match.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mimeMsg, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MIMEMessage) String() string {
|
return mimeMsg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MIMEBody) String() string {
|
||||||
return fmt.Sprintf("----TEXT----\n%v\n----HTML----\n%v\n----END----\n", m.Text, m.Html)
|
return fmt.Sprintf("----TEXT----\n%v\n----HTML----\n%v\n----END----\n", m.Text, m.Html)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,16 +23,20 @@ func TestIdentifyMime(t *testing.T) {
|
|||||||
func TestParseNonMime(t *testing.T) {
|
func TestParseNonMime(t *testing.T) {
|
||||||
msg := readMessage("non-mime.raw")
|
msg := readMessage("non-mime.raw")
|
||||||
|
|
||||||
_, err := ParseMIMEMessage(msg)
|
mime, err := ParseMIMEBody(msg)
|
||||||
assert.NotNil(t, err, "Expected error parsing a non-MIME message")
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to parse non-MIME: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Contains(t, mime.Text, "This is a test mailing")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseInlineText(t *testing.T) {
|
func TestParseInlineText(t *testing.T) {
|
||||||
msg := readMessage("html-mime-inline.raw")
|
msg := readMessage("html-mime-inline.raw")
|
||||||
|
|
||||||
mime, err := ParseMIMEMessage(msg)
|
mime, err := ParseMIMEBody(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to parse mime: %v", err)
|
t.Fatalf("Failed to parse MIME: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, mime.Text, "Test of HTML section")
|
assert.Equal(t, mime.Text, "Test of HTML section")
|
||||||
@@ -41,9 +45,9 @@ func TestParseInlineText(t *testing.T) {
|
|||||||
func TestParseInlineHtml(t *testing.T) {
|
func TestParseInlineHtml(t *testing.T) {
|
||||||
msg := readMessage("html-mime-inline.raw")
|
msg := readMessage("html-mime-inline.raw")
|
||||||
|
|
||||||
mime, err := ParseMIMEMessage(msg)
|
mime, err := ParseMIMEBody(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to parse mime: %v", err)
|
t.Fatalf("Failed to parse MIME: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Contains(t, mime.Html, "<html>")
|
assert.Contains(t, mime.Html, "<html>")
|
||||||
|
|||||||
Reference in New Issue
Block a user