Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/kotlin/com/nylas/NylasClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ open class NylasClient(
*/
open fun drafts(): Drafts = Drafts(this)

/**
* Access the Domains API
* @return The Domains API
*/
open fun domains(): Domains = Domains(this)

/**
* Access the Events API
* @return The Events API
Expand Down
150 changes: 150 additions & 0 deletions src/main/kotlin/com/nylas/models/SendTransactionalEmailRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.nylas.models

import com.squareup.moshi.Json

/**
* Class representing a request to send a transactional email from a verified domain.
*/
data class SendTransactionalEmailRequest(
/**
* An array of message recipients.
*/
@Json(name = "to")
val to: List<EmailName>,
/**
* The sender. Must use a verified domain email address.
*/
@Json(name = "from")
val from: EmailName,
/**
* An array of bcc recipients.
*/
@Json(name = "bcc")
val bcc: List<EmailName>? = null,
/**
* An array of cc recipients.
*/
@Json(name = "cc")
val cc: List<EmailName>? = null,
/**
* An array of name and email pairs that override the sent reply-to headers.
* Recommended if there is no Agent Account on the domain to receive replies.
*/
@Json(name = "reply_to")
val replyTo: List<EmailName>? = null,
/**
* An array of files to attach to the message.
*/
@Json(name = "attachments")
override val attachments: List<CreateAttachmentRequest>? = null,
/**
* The message subject.
*/
@Json(name = "subject")
val subject: String? = null,
/**
* The full HTML message body.
* Messages with only plain-text representations are up-converted to HTML.
*/
@Json(name = "body")
val body: String? = null,
/**
* Unix timestamp to send the message at.
*/
@Json(name = "send_at")
val sendAt: Long? = null,
/**
* The ID of the message that you are replying to.
*/
@Json(name = "reply_to_message_id")
val replyToMessageId: String? = null,
/**
* Options for tracking opens, links, and thread replies.
*/
@Json(name = "tracking_options")
val trackingOptions: TrackingOptions? = null,
/**
* Whether or not to use draft support.
* This is primarily used when dealing with large attachments.
*/
@Json(name = "use_draft")
val useDraft: Boolean? = null,
/**
* A list of custom headers to add to the message.
*/
@Json(name = "custom_headers")
val customHeaders: List<CustomHeader>? = null,
/**
* When true, the message body is sent as plain text and the MIME data doesn't include the HTML version of the message.
* When false, the message body is sent as HTML.
*/
@Json(name = "is_plaintext")
val isPlaintext: Boolean? = null,
) : IMessageAttachmentRequest {
/**
* Builder for [SendTransactionalEmailRequest].
* @property to An array of message recipients.
* @property from The sender. Must use a verified domain email address.
*/
data class Builder(
private val to: List<EmailName>,
private val from: EmailName,
) {
private var bcc: List<EmailName>? = null
private var cc: List<EmailName>? = null
private var replyTo: List<EmailName>? = null
private var attachments: List<CreateAttachmentRequest>? = null
private var subject: String? = null
private var body: String? = null
private var sendAt: Long? = null
private var replyToMessageId: String? = null
private var trackingOptions: TrackingOptions? = null
private var useDraft: Boolean? = null
private var customHeaders: List<CustomHeader>? = null
private var isPlaintext: Boolean? = null

fun bcc(bcc: List<EmailName>?) = apply { this.bcc = bcc }

fun cc(cc: List<EmailName>?) = apply { this.cc = cc }

fun replyTo(replyTo: List<EmailName>?) = apply { this.replyTo = replyTo }

fun attachments(attachments: List<CreateAttachmentRequest>?) = apply { this.attachments = attachments }

fun subject(subject: String?) = apply { this.subject = subject }

fun body(body: String?) = apply { this.body = body }

fun sendAt(sendAt: Long?) = apply { this.sendAt = sendAt }

fun sendAt(sendAt: Int?) = apply { this.sendAt = sendAt?.toLong() }

fun replyToMessageId(replyToMessageId: String?) = apply { this.replyToMessageId = replyToMessageId }

fun trackingOptions(trackingOptions: TrackingOptions?) = apply { this.trackingOptions = trackingOptions }

fun useDraft(useDraft: Boolean?) = apply { this.useDraft = useDraft }

fun customHeaders(customHeaders: List<CustomHeader>?) = apply { this.customHeaders = customHeaders }

fun isPlaintext(isPlaintext: Boolean?) = apply { this.isPlaintext = isPlaintext }

fun build() =
SendTransactionalEmailRequest(
to,
from,
bcc,
cc,
replyTo,
attachments,
subject,
body,
sendAt,
replyToMessageId,
trackingOptions,
useDraft,
customHeaders,
isPlaintext,
)
}
}
40 changes: 40 additions & 0 deletions src/main/kotlin/com/nylas/resources/Domains.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.nylas.resources

import com.nylas.NylasClient
import com.nylas.models.*
import com.nylas.util.FileUtils
import com.nylas.util.JsonHelper
import com.squareup.moshi.Types

class Domains(client: NylasClient) : Resource<Message>(client, Message::class.java) {

/**
* Send a transactional email from a verified domain.
* @param domainName The verified domain name to send from
* @param requestBody The values to send the email with
* @param overrides Optional request overrides to apply
* @return The sent message
*/
@Throws(NylasApiError::class, NylasSdkTimeoutError::class)
@JvmOverloads
fun sendTransactionalEmail(
domainName: String,
requestBody: SendTransactionalEmailRequest,
overrides: RequestOverrides? = null,
): Response<Message> {
val path = String.format("v3/domains/%s/messages/send", domainName)
val responseType = Types.newParameterizedType(Response::class.java, Message::class.java)
val adapter = JsonHelper.moshi().adapter(SendTransactionalEmailRequest::class.java)

val attachmentSize = requestBody.attachments?.sumOf { it.size } ?: 0
return if (attachmentSize >= FileUtils.MAXIMUM_JSON_ATTACHMENT_SIZE) {
val attachmentLessPayload = requestBody.copy(attachments = null)
val serializedRequestBody = adapter.toJson(attachmentLessPayload)
val multipart = FileUtils.buildFormRequest(requestBody, serializedRequestBody)
client.executeFormRequest(path, NylasClient.HttpMethod.POST, multipart, responseType, overrides = overrides)
} else {
val serializedRequestBody = adapter.toJson(requestBody)
createResource(path, serializedRequestBody, overrides = overrides)
}
}
}
6 changes: 6 additions & 0 deletions src/test/kotlin/com/nylas/NylasClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ class NylasClientTest {
val result = nylasClient.notetakers()
assertNotNull(result)
}

@Test
fun `domains returns a valid Domains instance`() {
val result = nylasClient.domains()
assertNotNull(result)
}
}

@Nested
Expand Down
Loading
Loading