From a5ceaf25bef4f7f306c3eddaab1232afa4c15f9c Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Thu, 4 Apr 2024 21:08:45 +0530 Subject: [PATCH] Do not use replaceCID if not necessary (#106) * Only call replaceCID if we have anything to replace * Use contentID directly if provided --- message.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/message.go b/message.go index 828f7c1..ceb7fca 100644 --- a/message.go +++ b/message.go @@ -83,7 +83,14 @@ func (msg *message) getCID(text string) (cid string) { // replaceCIDs replaces the CIDs found in a text string // with generated ones -func (msg *message) replaceCIDs(text string) string { +func (msg *message) replaceCIDs(input []byte) []byte { + if len(msg.cids) == 0 { + // One process replaceCIDs if we have anything to replace + return input + } + + text := string(input) + // regular expression to find cids re := regexp.MustCompile(`(src|href)="cid:(.*?)"`) // replace all of the found cids with generated ones @@ -92,7 +99,7 @@ func (msg *message) replaceCIDs(text string) string { text = strings.Replace(text, "cid:"+matches[2], "cid:"+cid, -1) } - return text + return []byte(text) } // openMultipart creates a new part of a multipart message @@ -211,7 +218,7 @@ func (msg *message) writeBody(body []byte, encoding encoding) { } func (msg *message) addBody(contentType string, body []byte) { - body = []byte(msg.replaceCIDs(string(body))) + body = msg.replaceCIDs(body) header := make(textproto.MIMEHeader) header.Set("Content-Type", contentType+"; charset="+msg.charset) @@ -240,7 +247,7 @@ func (msg *message) addFiles(files []*File, inline bool) { if inline { header.Set("Content-Disposition", "inline;\n \tfilename=\""+encodedFilename+`"`) if len(file.ContentID) > 0 { - header.Set("Content-ID", "<"+msg.getCID(file.ContentID)+">") + header.Set("Content-ID", "<"+file.ContentID+">") } else { header.Set("Content-ID", "<"+msg.getCID(file.Name)+">") }