Nesting MIME parts

97 views
Skip to first unread message

Paul Samways

unread,
Jul 31, 2013, 7:06:05 AM7/31/13
to golang-nuts
It seems the only way to create nested multipart/* parts with the std lib API is to specify the boundary after creating the nested multipart.Writer, is this correct? 

The CreatePart method contract kind of makes things more difficult than it should be. That is it requires the headers passed in, and it states that the returned io.Writer should be used to write the part's content, even though when looking at the implementation, neither restrictions are necessary(?).

i.e.
b := &bytes.Buffer{}

fmt.Fprint(b, "To: <y...@example.com>\r\n")
fmt.Fprint(b, "From: <m...@example.com>\r\n")
fmt.Fprint(b, "Subject: Test message\r\n")

wOuter := multipart.NewWriter(b)
fmt.Fprintf(b, "Content-Type: multipart/mixed; boundary=%v", wOuter.Boundary())

h := make(textproto.MIMEHeader)
h.Set("Content-Type", "multipart/alternative; boundary=boundary2")
w, _ := wOuter.CreatePart(h)

wInner := multipart.NewWriter(w)
wInner.SetBoundary("boundary2")

wInner.Close()

wOuter.Close()

Brad Fitzpatrick

unread,
Jul 31, 2013, 2:31:42 PM7/31/13
to Paul Samways, golang-nuts
Is this a bug report?

What do you want clarified or simplified?



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paul Samways

unread,
Jul 31, 2013, 5:59:59 PM7/31/13
to Brad Fitzpatrick, golang-nuts
Hi Brad,

I was hoping that I was missing something with Writer, it took me a minute to realise how to actually use it to write nested multiparts. Thought there may of been a better way which I wasn't seeing.

Brad Fitzpatrick

unread,
Jul 31, 2013, 6:25:24 PM7/31/13
to Paul Samways, golang-nuts
Seems nicely composable to me.  I don't think it needs any new API that's specific to nesting.


Reply all
Reply to author
Forward
0 new messages