Skip to content

Commit

Permalink
Ability to add attachment in base64 instead a path with new public fu…
Browse files Browse the repository at this point in the history
…nction AddAttachmentBase64()

-AddAttachmentBase64() b64File and name arguments are mandatory
-New private function attachB64() that decode the base64 string

This functions are useful if you have a webservice and receive the file in base64 in the body, instead create the file and attach it you only need to pass the base64 and filename to AddAttachmentBase64()
  • Loading branch information
xhit committed Sep 19, 2019
1 parent cada17e commit 51e7e0b
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mail
import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -468,6 +469,23 @@ func (email *Email) AddAttachment(file string, name ...string) *Email {
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 {
if email.Error != nil {
return email
}

if len(name) < 1 || len(b64File) < 1 {
email.Error = errors.New("Mail Error: Attach Base64 need have a base64 string and name")
return email
}

email.Error = email.attachB64(b64File, name)

return email
}

// AddInline allows you to add an inline attachment to the email message.
// You can optionally provide a different name for the file.
func (email *Email) AddInline(file string, name ...string) *Email {
Expand Down Expand Up @@ -524,6 +542,50 @@ func (email *Email) attach(f string, inline bool, name ...string) error {
return nil
}

// attachB64 does the low level attaching of the files
func (email *Email) attachB64(b64File string, name string) error {
// Get the file data
// data, err := ioutil.ReadFile(f)
// if err != nil {
// return errors.New("Mail Error: Failed to add file with following error: " + err.Error())
// }

dec, err := base64.StdEncoding.DecodeString(b64File)
if err != nil {
return errors.New("Mail Error: Failed to decode base64 attachment with following error: " + err.Error())
}

// get the file mime type
mimeType := mime.TypeByExtension(name)
if mimeType == "" {
mimeType = "application/octet-stream"
}

// get the filename
// _, filename := filepath.Split(f)

// if an alternative filename was provided, use that instead
// if len(name) == 1 {
// filename = name[0]
// }

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

return nil
}

// getFrom returns the sender of the email, if any
func (email *Email) getFrom() string {
from := email.returnPath
Expand Down

0 comments on commit 51e7e0b

Please sign in to comment.