diff --git a/attach.go b/attach.go index 9aa48b6..68be9c1 100644 --- a/attach.go +++ b/attach.go @@ -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 @@ -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 @@ -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") } diff --git a/email.go b/email.go index 33acf33..64ba7f9 100644 --- a/email.go +++ b/email.go @@ -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