add multipart/mixed formatting, base64 line-wrapped encoding to MessageFormatter
This commit is contained in:
@@ -70,6 +70,82 @@ public enum MessageFormatter: Sendable {
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
/// Format a multipart/mixed message with attachments.
|
||||
public static func formatMultipart(
|
||||
_ message: OutgoingMessage,
|
||||
attachments: [(filename: String, mimeType: String, data: Data)]
|
||||
) -> String {
|
||||
let boundary = "=_MagnumOpus_\(UUID().uuidString)"
|
||||
|
||||
var headers: [(String, String)] = []
|
||||
headers.append(("From", formatAddress(message.from)))
|
||||
headers.append(("To", message.to.map(formatAddress).joined(separator: ", ")))
|
||||
|
||||
if !message.cc.isEmpty {
|
||||
headers.append(("Cc", message.cc.map(formatAddress).joined(separator: ", ")))
|
||||
}
|
||||
|
||||
headers.append(("Subject", message.subject))
|
||||
headers.append(("Date", formatRFC2822Date(Date())))
|
||||
headers.append(("Message-ID", "<\(message.messageId)>"))
|
||||
|
||||
if let inReplyTo = message.inReplyTo {
|
||||
headers.append(("In-Reply-To", "<\(inReplyTo)>"))
|
||||
}
|
||||
if let references = message.references {
|
||||
headers.append(("References", references))
|
||||
}
|
||||
|
||||
headers.append(("MIME-Version", "1.0"))
|
||||
headers.append(("Content-Type", "multipart/mixed; boundary=\"\(boundary)\""))
|
||||
|
||||
var result = ""
|
||||
for (name, value) in headers {
|
||||
result += "\(name): \(value)\r\n"
|
||||
}
|
||||
result += "\r\n"
|
||||
|
||||
// Text body part
|
||||
result += "--\(boundary)\r\n"
|
||||
result += "Content-Type: text/plain; charset=utf-8\r\n"
|
||||
result += "Content-Transfer-Encoding: quoted-printable\r\n"
|
||||
result += "\r\n"
|
||||
result += quotedPrintableEncode(message.bodyText)
|
||||
result += "\r\n"
|
||||
|
||||
// Attachment parts
|
||||
for attachment in attachments {
|
||||
result += "--\(boundary)\r\n"
|
||||
result += "Content-Type: \(attachment.mimeType); name=\"\(attachment.filename)\"\r\n"
|
||||
result += "Content-Disposition: attachment; filename=\"\(attachment.filename)\"\r\n"
|
||||
result += "Content-Transfer-Encoding: base64\r\n"
|
||||
result += "\r\n"
|
||||
result += base64Encode(attachment.data)
|
||||
result += "\r\n"
|
||||
}
|
||||
|
||||
// Closing boundary
|
||||
result += "--\(boundary)--\r\n"
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/// Base64 encode data with line wrapping at 76 characters per RFC 2045.
|
||||
public static func base64Encode(_ data: Data, lineLength: Int = 76) -> String {
|
||||
let encoded = data.base64EncodedString()
|
||||
var result = ""
|
||||
var index = encoded.startIndex
|
||||
while index < encoded.endIndex {
|
||||
let end = encoded.index(index, offsetBy: lineLength, limitedBy: encoded.endIndex) ?? encoded.endIndex
|
||||
result += String(encoded[index..<end])
|
||||
if end < encoded.endIndex {
|
||||
result += "\r\n"
|
||||
}
|
||||
index = end
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// Encode a string using quoted-printable encoding (RFC 2045).
|
||||
public static func quotedPrintableEncode(_ text: String) -> String {
|
||||
var result = ""
|
||||
|
||||
Reference in New Issue
Block a user