Contents

allowMessageSendForSession(_:completion:)

Confirms that the message is ready for delivery.

Declaration

optional func allowMessageSendForSession(_ session: MEComposeSession, completion: @escaping ((any Error)?) -> Void)
optional func allowMessageSendForSession(_ session: MEComposeSession) async throws

Parameters

  • session:

    The session that represents the properties of the message in the compose window.

  • completion:

    A block you call after determining if the message is ready for delivery.

Discussion

MailKit gives your compose session handler an opportunity to confirm that a message is ready for delivery. If the message is ready for delivery and needs no further attention, call the completion handler and pass nil.

If there’s an issue with the message’s content and you want to prompt the user, call the completion handler and pass an error that contains a localized description of the reason the message isn’t ready. For example, the following code calls the completion handler with an error if a recipient uses the domain @example.com.

enum ComposeSessionError: LocalizedError {
    case invalidRecipientDomain
    
    var errorDescription: String? {
        switch self {
        case .invalidRecipientDomain:
            return "example.com is not a valid recipient domain"
        }
    }
}

func allowMessageSendForSession(_ session: MEComposeSession, completion: @escaping (Error?) -> Void) {
    // Confirm none of the recipients use @example.com.
    if session.mailMessage.allRecipientAddresses.contains(where: { $0.hasSuffix("@example.com")}) {
        completion(ComposeSessionError.invalidRecipientDomain)
    } else {
        completion(nil)
    }
}