Add outgoing mail attachments

Support multiple attachments in desktop and Android compose, persist them in drafts and offline queues, and include them in desktop OpenPGP protection. Update docs and tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-10-10 21:05:32 +03:00
co-authored by Copilot
parent acba5d641e
commit 5cd8d545e9
13 changed files with 335 additions and 20 deletions
@@ -18,6 +18,8 @@ import java.util.Date
import java.util.Locale
import java.util.Properties
import java.util.UUID
import android.util.Base64
import javax.activation.DataHandler
import javax.mail.Address
import javax.mail.Authenticator
import javax.mail.Flags
@@ -28,6 +30,7 @@ import javax.mail.PasswordAuthentication
import javax.mail.Session
import javax.mail.Store
import javax.mail.Transport
import javax.mail.util.ByteArrayDataSource
import javax.mail.UIDFolder
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
@@ -272,10 +275,37 @@ internal object AndroidMailService {
message.subject = input.optString("subject", "(no subject)")
val body = input.optString("text")
val signature = account.optString("signature").take(5000)
message.setText(
if (signature.isBlank()) body else "$body${if (body.isBlank()) "" else "\n\n"}$signature",
"UTF-8"
)
val fullBody = if (signature.isBlank()) body else "$body${if (body.isBlank()) "" else "\n\n"}$signature"
val attachments = input.optJSONArray("attachments") ?: JSONArray()
if (attachments.length() == 0) {
message.setText(fullBody, "UTF-8")
} else {
val multipart = MimeMultipart("mixed")
multipart.addBodyPart(MimeBodyPart().apply { setText(fullBody, "UTF-8") })
for (index in 0 until attachments.length()) {
val file = attachments.optJSONObject(index)
?: throw IllegalArgumentException("An attachment could not be read.")
val encoded = file.optString("contentBase64")
require(encoded.length % 4 == 0 && encoded.matches(Regex("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"))) {
"The contents of an attachment are invalid."
}
val filename = file.optString("filename", "attachment")
.replace(Regex("[\\\\/\\r\\n\\u0000]"), "")
.take(255)
.ifBlank { "attachment" }
val contentType = file.optString("contentType", "application/octet-stream")
.lowercase(Locale.ROOT)
require(contentType.matches(Regex("^[a-z0-9!#$&^_.+-]+/[a-z0-9!#$&^_.+-]+$"))) {
"The content type for $filename is invalid."
}
val part = MimeBodyPart()
part.dataHandler = DataHandler(ByteArrayDataSource(Base64.decode(encoded, Base64.DEFAULT), contentType))
part.fileName = MimeUtility.encodeText(filename, "UTF-8", null)
part.disposition = javax.mail.Part.ATTACHMENT
multipart.addBodyPart(part)
}
message.setContent(multipart)
}
message.sentDate = Date()
message.saveChanges()
input.optString("messageId").takeIf(String::isNotBlank)?.let { message.setHeader("Message-ID", it) }