Skip to content

Commit

Permalink
Add option to allow empty attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Dec 19, 2023
1 parent abcd63a commit 6628ef0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
8 changes: 6 additions & 2 deletions attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (email *Email) Attach(file *File) *Email {
}
}

attachTy, err := getAttachmentType(file)
attachTy, err := getAttachmentType(file, email.AllowEmptyAttachments)
if err != nil {
email.Error = errors.New("Mail Error: Failed to add attachment with following error: " + err.Error())
return email
Expand All @@ -80,7 +80,7 @@ func (email *Email) Attach(file *File) *Email {
return email
}

func getAttachmentType(file *File) (attachType, error) {
func getAttachmentType(file *File, allowEmptyAttachments bool) (attachType, error) {
// 1- data
// 2- base64
// 3- file
Expand Down Expand Up @@ -108,6 +108,10 @@ func getAttachmentType(file *File) (attachType, error) {
return attachFile, nil
}

if allowEmptyAttachments && len(file.Name) != 0 {
return attachData, nil
}

return 0, errors.New("empty attachment")
}

Expand Down
34 changes: 19 additions & 15 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ import (

// Email represents an email message.
type Email struct {
from string
sender string
replyTo string
returnPath string
recipients []string
headers textproto.MIMEHeader
parts []part
attachments []*File
inlines []*File
Charset string
Encoding encoding
Error error
SMTPServer *smtpClient
DkimMsg string
AllowDuplicateAddress bool
from string
sender string
replyTo string
returnPath string
recipients []string
headers textproto.MIMEHeader
parts []part
attachments []*File
inlines []*File
Charset string
Encoding encoding
Error error
SMTPServer *smtpClient
DkimMsg string
AllowDuplicateAddress bool

// AllowEmptyAttachments if enabled, allows you you attach empty
// items, a file without any associated data
AllowEmptyAttachments bool
AddBccToHeader bool
preserveOriginalRecipient bool
dsn []DSN
Expand Down

0 comments on commit 6628ef0

Please sign in to comment.