Include body MIME terminator in multipart messages
Before, the text/plain part of the multipart MIME message was not being correctly terminated with its boundary. The multipart writer writes the terminator when its Close is called, but since the call to Close() was deferred, it was not being called until after the attachments were being written resulting in the boundary not being included at all.
This commit is contained in:
parent
3424c36d52
commit
bb620e0900
|
@ -335,14 +335,7 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
|
||||||
|
|
||||||
if len(c.attachments) == 0 {
|
if len(c.attachments) == 0 {
|
||||||
// don't create a multipart email if we only have text
|
// don't create a multipart email if we only have text
|
||||||
header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
|
return writeInlineBody(header, body, writer)
|
||||||
w, err := mail.CreateSingleInlineWriter(writer, *header)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "CreateSingleInlineWriter")
|
|
||||||
}
|
|
||||||
defer w.Close()
|
|
||||||
|
|
||||||
return writeBody(body, w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise create a multipart email,
|
// otherwise create a multipart email,
|
||||||
|
@ -353,6 +346,34 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
|
||||||
}
|
}
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
|
|
||||||
|
if err := writeMultipartBody(body, w); err != nil {
|
||||||
|
return errors.Wrap(err, "writeMultipartBody")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, a := range c.attachments {
|
||||||
|
if err := writeAttachment(a, w); err != nil {
|
||||||
|
return errors.Wrap(err, "writeAttachment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeInlineBody(header *mail.Header, body io.Reader, writer io.Writer) error {
|
||||||
|
header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
|
||||||
|
w, err := mail.CreateSingleInlineWriter(writer, *header)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "CreateSingleInlineWriter")
|
||||||
|
}
|
||||||
|
defer w.Close()
|
||||||
|
if _, err := io.Copy(w, body); err != nil {
|
||||||
|
return errors.Wrap(err, "io.Copy")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// write the message body to the multipart message
|
||||||
|
func writeMultipartBody(body io.Reader, w *mail.Writer) error {
|
||||||
bh := mail.InlineHeader{}
|
bh := mail.InlineHeader{}
|
||||||
bh.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
|
bh.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
|
||||||
|
|
||||||
|
@ -367,23 +388,9 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
|
||||||
return errors.Wrap(err, "CreatePart")
|
return errors.Wrap(err, "CreatePart")
|
||||||
}
|
}
|
||||||
defer bw.Close()
|
defer bw.Close()
|
||||||
|
if _, err := io.Copy(bw, body); err != nil {
|
||||||
if err := writeBody(body, bw); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, a := range c.attachments {
|
|
||||||
writeAttachment(a, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeBody(body io.Reader, w io.Writer) error {
|
|
||||||
if _, err := io.Copy(w, body); err != nil {
|
|
||||||
return errors.Wrap(err, "io.Copy")
|
return errors.Wrap(err, "io.Copy")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue