Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #70 and allows usage of duplicate recipients #71

Merged
merged 2 commits into from
Nov 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ 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
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
DuplicateRecipientAllowed bool
xhit marked this conversation as resolved.
Show resolved Hide resolved
}

/*
Expand Down Expand Up @@ -138,9 +139,10 @@ const (
// NewMSG creates a new email. It uses UTF-8 by default. All charsets: http://webcheatsheet.com/HTML/character_sets_list.php
func NewMSG() *Email {
email := &Email{
headers: make(textproto.MIMEHeader),
Charset: "UTF-8",
Encoding: EncodingQuotedPrintable,
headers: make(textproto.MIMEHeader),
Charset: "UTF-8",
Encoding: EncodingQuotedPrintable,
DuplicateRecipientAllowed: false,
xhit marked this conversation as resolved.
Show resolved Hide resolved
}

email.AddHeader("MIME-Version", "1.0")
Expand Down Expand Up @@ -317,7 +319,7 @@ func (email *Email) AddAddresses(header string, addresses ...string) *Email {
email.returnPath = address.Address
default:
// check that the address was added to the recipients list
email.recipients, err = addAddress(email.recipients, address.Address)
email.recipients, err = addAddress(email.recipients, address.Address, email.DuplicateRecipientAllowed)
if err != nil {
email.Error = errors.New("Mail Error: " + err.Error() + "; Header: [" + header + "] Address: [" + addresses[i] + "]")
return email
Expand All @@ -343,11 +345,13 @@ func (email *Email) AddAddresses(header string, addresses ...string) *Email {
}

// addAddress adds an address to the address list if it hasn't already been added
func addAddress(addressList []string, address string) ([]string, error) {
// loop through the address list to check for dups
for _, a := range addressList {
if address == a {
return addressList, errors.New("Mail Error: Address: [" + address + "] has already been added")
func addAddress(addressList []string, address string, duplicateRecipientAllowed bool) ([]string, error) {
if !duplicateRecipientAllowed {
// loop through the address list to check for dups
for _, a := range addressList {
if address == a {
return addressList, errors.New("Mail Error: Address: [" + address + "] has already been added")
}
}
}

Expand Down