From 4ca89c92791afbfaf66816fedd1f153c74074d77 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Wed, 9 Dec 2020 19:50:00 +0400 Subject: [PATCH 1/2] Allow to add inplace in-memory data --- email.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/email.go b/email.go index ce60d31..b600974 100644 --- a/email.go +++ b/email.go @@ -547,6 +547,28 @@ func (email *Email) AddInline(file string, name ...string) *Email { return email } +// AddInlineData allows you to add an inline bytes attachment to the email message. +func (email *Email) AddInlineData(data []byte, filename, mimeType string) *Email { + if email.Error != nil { + return email + } + + if mimeType == "" { + mimeType = mime.TypeByExtension(filepath.Ext(filename)) + if mimeType == "" { + mimeType = "application/octet-stream" + } + } + + email.inlines = append(email.inlines, &file{ + filename: filename, + mimeType: mimeType, + data: data, + }) + + 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 From 96b3aaf5d0c2b75709f3b11d15f6d8f37358bf08 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Thu, 10 Dec 2020 17:18:45 +0400 Subject: [PATCH 2/2] Allow to add in-memory attachments --- email.go | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/email.go b/email.go index b600974..8a51c9e 100644 --- a/email.go +++ b/email.go @@ -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 { @@ -547,24 +558,13 @@ func (email *Email) AddInline(file string, name ...string) *Email { return email } -// AddInlineData allows you to add an inline bytes attachment to the email message. +// 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 } - if mimeType == "" { - mimeType = mime.TypeByExtension(filepath.Ext(filename)) - if mimeType == "" { - mimeType = "application/octet-stream" - } - } - - email.inlines = append(email.inlines, &file{ - filename: filename, - mimeType: mimeType, - data: data, - }) + email.attachData(data, true, filename, mimeType) return email } @@ -608,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 {