Check if Attachments/Embeds can be read. If an error occurs don't add them to the mail parts

This commit is contained in:
Winni Neessen 2022-03-15 11:56:21 +01:00
parent f8ba9ac9c8
commit ce1c37bd16
Signed by: wneessen
GPG key ID: 385AC9889632126E

29
msg.go
View file

@ -415,22 +415,38 @@ func (m *Msg) AddAlternativeWriter(ct ContentType, w func(io.Writer) error, o ..
// AttachFile adds an attachment File to the Msg
func (m *Msg) AttachFile(n string, o ...FileOption) {
m.attachments = m.appendFile(m.attachments, fileFromFS(n), o...)
f := fileFromFS(n)
if f == nil {
return
}
m.attachments = m.appendFile(m.attachments, f, o...)
}
// AttachReader adds an attachment File via io.Reader to the Msg
func (m *Msg) AttachReader(n string, r io.Reader, o ...FileOption) {
m.attachments = m.appendFile(m.attachments, fileFromReader(n, r), o...)
f := fileFromReader(n, r)
if f == nil {
return
}
m.attachments = m.appendFile(m.attachments, f, o...)
}
// EmbedFile adds an embedded File to the Msg
func (m *Msg) EmbedFile(n string, o ...FileOption) {
m.embeds = m.appendFile(m.embeds, fileFromFS(n), o...)
f := fileFromFS(n)
if f == nil {
return
}
m.embeds = m.appendFile(m.embeds, f, o...)
}
// EmbedReader adds an embedded File from an io.Reader to the Msg
func (m *Msg) EmbedReader(n string, r io.Reader, o ...FileOption) {
m.embeds = m.appendFile(m.embeds, fileFromReader(n, r), o...)
f := fileFromReader(n, r)
if f == nil {
return
}
m.embeds = m.appendFile(m.embeds, f, o...)
}
// Reset resets all headers, body parts and attachments/embeds of the Msg
@ -570,6 +586,11 @@ func (m *Msg) setEncoder() {
// fileFromFS returns a File pointer from a given file in the system's file system
func fileFromFS(n string) *File {
_, err := os.Stat(n)
if err != nil {
return nil
}
return &File{
Name: filepath.Base(n),
Header: make(map[string][]string),