jhillyerd / enmime

MIME mail encoding and decoding package for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MailBuilder.Build() requires non-empty subject

zeldrinn opened this issue · comments

What I did: Tried to call MailBuilder.Build() on a MailBuilder with an empty string for the subject field.

msg := enmime.Builder().
	From("example@example.com", "example@example.com").
	Subject("")

rootPart, err := msg.Build()
if err != nil {
	return nil, err
}

What I expected: I expected to be able to build an email object with an empty subject, as plenty of real world emails have empty subjects.

What I got: Received error subject not set (https://github.com/jhillyerd/enmime/blob/v0.6.0/builder.go#L220)

Release or branch I am using: v0.6.0

It's not clear to me why the validation logic in Build() would require subject to be non-empty.

The goal of the builder was to help people create well formed emails. While an email without a subject is valid, it is not well formed in my opinion.

Is there a use case that would help me understand why you want to do this?

@jhillyerd Thanks for the quick reply!

Not sure what the distinction is between "valid" and "well formed"...

The use case I ran into is needing to reconstruct an Office 365 (Outlook) envelope. When I do this for Gmail, I can simply call enmime.ReadEnvelope(bytes.NewReader(emailRaw)) on the raw email bytes (which is provided by the Gmail API), but I don't believe the Office 365 API provides the raw byte representation of an email. So in order to construct an envelope I'm using Build() to create it from scratch, using the body, subject and some of the headers, etc. As my service is ingesting emails from real inboxes, it's inevitable that some of these emails have empty subjects.

It seems reasonable that you'd be able to use Build() to construct any syntactically valid email object, rather than having it impose checks that I'd argue fall into the realm of "style".

The builder has many other "style" limitations you may not be aware of, so this is inline with its design philosophy.

Adding a Send() method to Envelope (see also #158) would probably be a better approach to allowing generation of more specialized emails.

I've run into this trying to do a similar thing as @zeldrinn . Perhaps builder could have a flag to make this check optional for folks who know they do in fact want to send an email with an empty subject line.

Given this use case is more common than I expected, I think we can just remove the subject check.