Skip to content

Commit

Permalink
Merge pull request #15 from falconandy/master
Browse files Browse the repository at this point in the history
Allow to add attachment in-memory data
  • Loading branch information
xhit committed Dec 10, 2020
2 parents d1e7602 + 96b3aaf commit 549f866
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,17 @@ func (email *Email) AddAttachment(file string, name ...string) *Email {
return email
}

// AddAttachmentData allows you to add an in-memory attachment to the email message.
func (email *Email) AddAttachmentData(data []byte, filename, mimeType string) *Email {
if email.Error != nil {
return email
}

email.attachData(data, false, filename, mimeType)

return email
}

// AddAttachmentBase64 allows you to add an attachment in base64 to the email message.
// You need provide a name for the file.
func (email *Email) AddAttachmentBase64(b64File string, name string) *Email {
Expand Down Expand Up @@ -547,6 +558,17 @@ func (email *Email) AddInline(file string, name ...string) *Email {
return email
}

// AddInlineData allows you to add an inline in-memory attachment to the email message.
func (email *Email) AddInlineData(data []byte, filename, mimeType string) *Email {
if email.Error != nil {
return email
}

email.attachData(data, true, filename, mimeType)

return email
}

// attach does the low level attaching of the files
func (email *Email) attach(f string, inline bool, name ...string) error {
// Get the file data
Expand Down Expand Up @@ -586,6 +608,30 @@ func (email *Email) attach(f string, inline bool, name ...string) error {
return nil
}

// attachData does the low level attaching of the in-memory data
func (email *Email) attachData(data []byte, inline bool, filename, mimeType string) {
if mimeType == "" {
mimeType = mime.TypeByExtension(filepath.Ext(filename))
if mimeType == "" {
mimeType = "application/octet-stream"
}
}

if inline {
email.inlines = append(email.inlines, &file{
filename: filename,
mimeType: mimeType,
data: data,
})
} else {
email.attachments = append(email.attachments, &file{
filename: filename,
mimeType: mimeType,
data: data,
})
}
}

// attachB64 does the low level attaching of the files but decoding base64 instead have a filepath
func (email *Email) attachB64(b64File string, name string) error {

Expand Down

0 comments on commit 549f866

Please sign in to comment.