Skip to content

Commit a5ceaf2

Browse files
authored
Do not use replaceCID if not necessary (#106)
* Only call replaceCID if we have anything to replace * Use contentID directly if provided
1 parent 21c1eba commit a5ceaf2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

message.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ func (msg *message) getCID(text string) (cid string) {
8383

8484
// replaceCIDs replaces the CIDs found in a text string
8585
// with generated ones
86-
func (msg *message) replaceCIDs(text string) string {
86+
func (msg *message) replaceCIDs(input []byte) []byte {
87+
if len(msg.cids) == 0 {
88+
// One process replaceCIDs if we have anything to replace
89+
return input
90+
}
91+
92+
text := string(input)
93+
8794
// regular expression to find cids
8895
re := regexp.MustCompile(`(src|href)="cid:(.*?)"`)
8996
// replace all of the found cids with generated ones
@@ -92,7 +99,7 @@ func (msg *message) replaceCIDs(text string) string {
9299
text = strings.Replace(text, "cid:"+matches[2], "cid:"+cid, -1)
93100
}
94101

95-
return text
102+
return []byte(text)
96103
}
97104

98105
// openMultipart creates a new part of a multipart message
@@ -211,7 +218,7 @@ func (msg *message) writeBody(body []byte, encoding encoding) {
211218
}
212219

213220
func (msg *message) addBody(contentType string, body []byte) {
214-
body = []byte(msg.replaceCIDs(string(body)))
221+
body = msg.replaceCIDs(body)
215222

216223
header := make(textproto.MIMEHeader)
217224
header.Set("Content-Type", contentType+"; charset="+msg.charset)
@@ -240,7 +247,7 @@ func (msg *message) addFiles(files []*File, inline bool) {
240247
if inline {
241248
header.Set("Content-Disposition", "inline;\n \tfilename=\""+encodedFilename+`"`)
242249
if len(file.ContentID) > 0 {
243-
header.Set("Content-ID", "<"+msg.getCID(file.ContentID)+">")
250+
header.Set("Content-ID", "<"+file.ContentID+">")
244251
} else {
245252
header.Set("Content-ID", "<"+msg.getCID(file.Name)+">")
246253
}

0 commit comments

Comments
 (0)