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:
@@ -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) }
|
||||
|
||||
@@ -331,6 +331,7 @@ class EpMailPlugin : Plugin() {
|
||||
.put("cc", input.optString("cc").take(4000))
|
||||
.put("subject", input.optString("subject").take(998))
|
||||
.put("text", input.optString("text").take(500000))
|
||||
.put("attachments", input.optJSONArray("attachments") ?: JSONArray())
|
||||
.put("updatedAt", System.currentTimeMillis())
|
||||
AndroidAccountVault.saveDraft(account.optString("id"), draft)
|
||||
call.resolve(draft.toJSObject())
|
||||
|
||||
Reference in New Issue
Block a user