Skip to content

Commit

Permalink
Add ability to attach files with specified mime types (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmguitar authored and domodwyer committed Jul 26, 2018
1 parent 03d01b6 commit 6ab6b52
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 32 deletions.
47 changes: 44 additions & 3 deletions attachments.go
Expand Up @@ -23,12 +23,14 @@ type attachment struct {
filename string
content io.Reader
inline bool
mimeType string
}

// Attach adds the contents of r to the email as an attachment with name as the
// filename.
//
// r is not read until Send is called.
// r is not read until Send is called and the MIME type will be detected
// using https://golang.org/pkg/net/http/#DetectContentType
func (m *MailYak) Attach(name string, r io.Reader) {
m.attachments = append(m.attachments, attachment{
filename: name,
Expand All @@ -37,6 +39,20 @@ func (m *MailYak) Attach(name string, r io.Reader) {
})
}

// AttachWithMimeType adds the contents of r to the email as an attachment with
// name as the filename and mimeType as the specified MIME type of the content.
// It is up to the user to ensure the mimeType is correct.
//
// r is not read until Send is called
func (m *MailYak) AttachWithMimeType(name string, r io.Reader, mimeType string) {
m.attachments = append(m.attachments, attachment{
filename: name,
content: r,
inline: false,
mimeType: mimeType,
})
}

// AttachInline adds the contents of r to the email as an inline attachment.
// Inline attachments are typically used within the email body, such as a logo
// or header image. It is up to the user to ensure name is unique.
Expand All @@ -46,7 +62,8 @@ func (m *MailYak) Attach(name string, r io.Reader) {
//
// <img src="cid:myFileName"/>
//
// r is not read until Send is called.
// r is not read until Send is called and the MIME type will be detected
// using https://golang.org/pkg/net/http/#DetectContentType
func (m *MailYak) AttachInline(name string, r io.Reader) {
m.attachments = append(m.attachments, attachment{
filename: name,
Expand All @@ -55,6 +72,26 @@ func (m *MailYak) AttachInline(name string, r io.Reader) {
})
}

// AttachInlineWithMimeType adds the contents of r to the email as an inline attachment
// with mimeType as the specified MIME type of the content. Inline attachments are
// typically used within the email body, such as a logo or header image. It is up to the
// user to ensure name is unique and the specified mimeType is correct.
//
// Files can be referenced by their name within the email using the cid URL
// protocol:
//
// <img src="cid:myFileName"/>
//
// r is not read until Send is called.
func (m *MailYak) AttachInlineWithMimeType(name string, r io.Reader, mimeType string) {
m.attachments = append(m.attachments, attachment{
filename: name,
content: r,
inline: true,
mimeType: mimeType,
})
}

// ClearAttachments removes all current attachments.
func (m *MailYak) ClearAttachments() {
m.attachments = []attachment{}
Expand All @@ -71,7 +108,11 @@ func (m *MailYak) writeAttachments(mixed partCreator, splitter writeWrapper) err
return err
}

ctype := fmt.Sprintf("%s;\n\tfilename=%q", http.DetectContentType(h[:hLen]), item.filename)
if item.mimeType == "" {
item.mimeType = http.DetectContentType(h[:hLen])
}

ctype := fmt.Sprintf("%s;\n\tfilename=%q", item.mimeType, item.filename)

part, err := mixed.CreatePart(getMIMEHeader(item, ctype))
if err != nil {
Expand Down

0 comments on commit 6ab6b52

Please sign in to comment.