From 40d3dce5bf3b4f20af154d82faf02577031af433 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Wed, 3 Apr 2024 17:09:33 +0530 Subject: [PATCH] Only call replaceCID if we have anything to replace --- message.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/message.go b/message.go index 828f7c1..5cdf233 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)