Skip to content

hsharghi/swift-resend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftResend

Version License

SwiftResend is a Swift package used to communicate with the Resend email sending platform API for Server Side Swift Apps.

Setup

Add the dependency to your Package.swift:

dependencies: [
    ...
    .package(url: "https://github.com/hsharghi/swift-resend.git", from: "1.0.0")
],
targets: [
    .target(name: "App", dependencies: [
        .product(name: "Resend", package: "swift-resend"),
    ]),

Register Configuration and Provider

Configure the HTTP client and the Resend client:

let httpClient = HTTPClient(...)
let resendClient = ResendClient(httpClient: httpClient, apiKey: "YOUR_API_KEY")

Usage

Email client

Sending Emails

You can send a single email by creating a ResendEmail object and retrieving the email ID in return.

import Resend

let email = ResendEmail(
        from: .init(email: "hadi@example.com", name: "Hadi"),
        to: ["hadi@domain.com"],
        subject: "running xctest",
        replyTo: [
            "hadi@example.com",
            "manager@example.com"
        ],
        text: "sending email via Resend API",
        headers: [
            .init(name: "X-Entity-Ref-ID", value: "234H3-44"),
            .init(name: "X-Entity-Dep-ID", value: "SALE-03"),
        ],
        attachments: [
            .init(content: .init(data: .init(contentsOf: .init(filePath: "path/to/a/file"))),
                filename: "sales.xlsx")
        ],
        tags: [
            .init(name: "priority", value: "medium"),
            .init(name: "department", value: "sales")
        ]
    )

Emails can be scheduled to be sent later. The date should be a Date object or a string in natural language (e.g.: in 1 min)

let email = ResendEmail(
        to: ["hadi@domain.com"],
        subject: "sending scheduled email",
        scheduledAt: "in an hour",
        text: "sending email via Resend API",
    )

let email = ResendEmail(
        to: ["hadi@domain.com"],
        subject: "sending scheduled email",
        scheduledAt: .date(Date(timeIntervalSinceNow: 60\*60)),
        text: "sending email via Resend API",
    )

Now the email can be sent using resend client

let id = try await resendClient.emails.send(email)

ResendEmail supports both text and html content.

You can send multiple emails at once by creating a ResendBatchEmail object. Attachments and Tags are not supported for batch sending. An array of email IDs will be returned.

let emails = ResendBatchEmail(...)
let ids = try await resendClient.emails.sendBatch(emails)

Retrieving Sent Emails List

You can retrieve list of emails sent by your team.

let sentMails = try await resendClient.emails.list(limit: 10, before: lastEmailId)

Note

Look up Resend API reference for using limit, before or after parameters.

Retrieving Email Information

You can retrieve information about a sent email by providing the email ID.

let emailInfo = try await resendClient.emails.get(emailId: id)

Retrieving List of Attachments for an Email

Retrieve a list of attachments of a sent email.

let attachmentList = try await resendClient.emails.attachments.list(emailId: id)

Retrieving Attachments Details for an Attachment of an Email

Retrieve a single attachment of a sent email.

let attachment = try await resendClient.emails.attachments.get(attachmentId: attachmentId, emailId: id)

attachmentId can be retreived from attachment list

Receiving

Important

For setting up receiving email address, refer to Receiving Emails section of Resend documentation.

Retrieving Received Emails List

Retrieve a list of received emails for the authenticated user.

let receivedEmails = try await resendClient.emails.receiving(limit: 10, before: lastEmailId)

Note

Look up Resend API reference for using limit, before or after parameters.

Retrieving Received Emails Information

Retrieve a single received email for the authenticated user.

let receivedEmails = try await resendClient.emails.receiving.get(emailId: id)

Retrieving List of Attachments for a Received Email

Retrieve a list of attachments of a received email.

let attachmentList = try await resendClient.emails.receiving.attachments.list(emailId: id)

Retrieving Attachments Details for an Attachment of a Received Email

Retrieve a single attachment from a received email.

let attachment = try await resendClient.emails.receiving.attachments.get(attachmentId: attachmentId, emailId: id)

attachmentId can be retreived from attachment list

Managing Audiences

Access the AudienceClient for managing audiences via the API. Refer to the Resend Audience API for complete details.

let audience = try await resendClient.audiences.create(name: "marketing")

Managing Contacts

Access the ContactClient for managing contacts via the API. Refer to the Resend Contact API for complete details.

let contactId = try await resendClient.contacts.create(audienceId: audience.id,
                                                       email: "john@apple.com",
                                                       firstName: "John",
                                                       subscriptionStatus: true)

Error handling

If a request to the API fails for any reason, a ResendError is thrown. Ensure you catch errors like any other throwing function.

do {
    try await resendClient.emails.send(...)
}
catch let error as ResendError {
    print(error.message)
    print(error.suggestion)
}

APIs supported by the current SDK

  • Emails
    • Sending
    • Receiving
  • Audiences
  • Contacts
  • Domains
  • API Keys
  • Broadcasts

License

This package is released under the MIT license. See LICENSE for details.