From 55a83dc929b22c2fde4572371df9674c28914edb Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Fri, 26 Sep 2025 13:42:19 -0400 Subject: [PATCH] handle super long recipient lists in email without breaking on servers with a line length limit --- email.d | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/email.d b/email.d index a0e4542..9d9773f 100644 --- a/email.d +++ b/email.d @@ -179,10 +179,23 @@ struct RecipientList { string toProtocolString(string linesep = "\r\n") { string ret; + int lineLengthCounter = 6; // first line probably includes "Bcc: " or similar and if not, we're allowed to be a wee bit conservative anyway foreach(idx, item; recipients) { - if(idx) + if(idx) { ret ~= ", "; - ret ~= item.toProtocolString(linesep); + lineLengthCounter += 2; + } + + auto itemString = item.toProtocolString(linesep); + + if(lineLengthCounter > 0 && lineLengthCounter + itemString.length > 78) { + ret ~= linesep; + ret ~= " "; + lineLengthCounter = 1; + } + + ret ~= itemString; + lineLengthCounter += itemString.length; } return ret; } @@ -1734,6 +1747,32 @@ unittest { assert(test !is encodeEmailHeaderForTransmit(test, linesep)); // a newline forces encoding } +unittest { + auto message = new EmailMessage(); + //message.to ~= "someusadssssssssssssssssssssssssssssssssssssssssssssssssssssssser@example.com"; + foreach(i; 0 .. 10) + message.to ~= "someuser@example.com"; + //message.to ~= "someusadssssssssssssssssssssssssssssssssssssssssssssssssssssssser@example.com"; + //foreach(i; 0 .. 10) + //message.to ~= "someuser@example.com"; + message.from = "youremail@example.com"; + message.subject = "My Subject"; + message.setTextBody("hi there"); + int lineLength; + + //import arsd.core; writeln(message.toString()); + + foreach(char c; message.toString()) { + if(c == 13) { + assert(lineLength < 78); + lineLength = 0; + } else { + lineLength++; + } + } + assert(lineLength < 78); +} + /+ void main() { import std.file;