Skip to content

Commit 4372d00

Browse files
authored
Fixed panic in deprecated attachment functions (#39)
* Fixed panic in deprecated attachment functions Fixed panic when AddAttachment and AddInline functions are called without name of file. * Add test for attachment without name
1 parent 25d5f5b commit 4372d00

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

attach_old.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ func (email *Email) AddAttachment(file string, name ...string) *Email {
1717
email.Error = errors.New("Mail Error: Attach can only have a file and an optional name")
1818
return email
1919
}
20-
21-
return email.Attach(&File{Name: name[0], FilePath: file})
20+
21+
var nm string
22+
if len(name) == 1 {
23+
nm = name[0]
24+
}
25+
return email.Attach(&File{Name: nm, FilePath: file})
2226
}
2327

2428
// AddAttachmentData. DEPRECATED. Use Attach method. Allows you to add an in-memory attachment to the email message.
@@ -43,8 +47,13 @@ func (email *Email) AddInline(file string, name ...string) *Email {
4347
email.Error = errors.New("Mail Error: Inline can only have a file and an optional name")
4448
return email
4549
}
46-
47-
return email.Attach(&File{Name: name[0], FilePath: file, Inline: true})
50+
51+
var nm string
52+
if len(name) == 1 {
53+
nm = name[0]
54+
}
55+
56+
return email.Attach(&File{Name: nm, FilePath: file, Inline: true})
4857
}
4958

5059
// AddInlineData. DEPRECATED. Use Attach method. Allows you to add an inline in-memory attachment to the email message.

attach_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,18 @@ func TestAttachments(t *testing.T) {
105105
got := msg.attachments[0].Data
106106
checkByteSlice(t, got, want)
107107
})
108+
t.Run("Inline File not name Deprecated", func(t *testing.T) {
109+
msg := NewMSG()
110+
msg.AddInline("testdata/foo.txt")
111+
checkError(t, msg.Error)
112+
got := msg.inlines[0].Data
113+
checkByteSlice(t, got, want)
114+
})
115+
t.Run("Attachment File not name Deprecated", func(t *testing.T) {
116+
msg := NewMSG()
117+
msg.AddAttachment("testdata/foo.txt")
118+
checkError(t, msg.Error)
119+
got := msg.attachments[0].Data
120+
checkByteSlice(t, got, want)
121+
})
108122
}

0 commit comments

Comments
 (0)