jordan-wright / email

Robust and flexible email library for Go
MIT License
2.61k stars 324 forks source link

BUG: HTMLRelated attachments with only HTML #120

Closed martinrode closed 3 years ago

martinrode commented 3 years ago
Stack:
goroutine 804 [running]:
runtime/debug.Stack(0xc001f147e0, 0x5145b60, 0x645ad70)
        /usr/local/go/src/runtime/debug/stack.go:24 +0x9d
github.com/programmfabrik/fylr/pkg/server/middleware.Environment.func1.1(0xc001f147e0, 0x590f560, 0xc001f20540, 0xc00209b500)
        /Users/martin/go/src/github.com/programmfabrik/fylr/pkg/server/middleware/environment.go:32 +0x6b
panic(0x5145b60, 0x645ad70)
        /usr/local/go/src/runtime/panic.go:969 +0x166
mime/multipart.(*Writer).CreatePart(0x0, 0xc002376710, 0x53030ed, 0xc, 0xc0023767f8, 0xc000d55740)
        /usr/local/go/src/mime/multipart/writer.go:98 +0x3a
github.com/jordan-wright/email.(*Email).Bytes(0xc002376c70, 0xc0006d61a0, 0x10, 0x0, 0x0, 0x0)
        /Users/martin/go/pkg/mod/github.com/jordan-wright/email@v0.0.0-20200602115436-fd8a7622303e/email.go:452 +0xb43
github.com/jordan-wright/email.(*Email).Send(0xc002376c70, 0x530c490, 0x12, 0x59069e0, 0xc002834340, 0xc001e43f93, 0x9)
        /Users/martin/go/pkg/mod/github.com/jordan-wright/email@v0.0.0-20200602115436-fd8a7622303e/email.go:519 +0x3ab
jordan-wright commented 3 years ago

Hey there!

Could you please provide a sample email that I can use to reproduce the issue?

martinrode commented 3 years ago

Hi Jordan,

simply set HTML, Attachment (HTMLRelated), and no Text.

The logic in „Bytes“ set isMixed and isAlternative both to false and later when adding the html attachment the subWriter is nil.

I dont know enough about email, but I assume the isMixed or isAlternative need to be turned on in this case where the mail requires an HTML body and an attachment.

If you need code, I can provide this on Monday when I am back in the office.

Best, Martin

sgoldenb commented 3 years ago

Since i stumbled upon the same bug here something to reproduce:

// *Mailer contains the pool and some config stuff
func (m *Mailer) Send(e *email.Email) error {
    d := struct {
        Text          string
    }{
        Text:         "whatever" ,
    }

        // the template references the image via cid
        temp, err := m.parseTemplate("my_template.html", d)
    if err != nil {
             return err
       }
    e := email.NewEmail()
    e.HTML = temp
    e.From = "no-reply@foo.bar"
    e.Subject = "Blarb"
    e.To = []string{"foo@bar.baz"}

    dir, _ := os.Getwd()
    a, _ := e.AttachFile(dir+"/mailer/logo.png")

    // set to multipart to embed image
    a.HTMLRelated = true

    // text needs to be set to something non empty for multipart attachments to work
    // uncommenting this line will cause the crash 
    // e.Text = []byte(" ")

    return m.pool.Send(e, m.timeout)
}