Skip to content

hsharghi/swift-resend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftResend

Version

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.1.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

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

import Resend

let email = ResendEmail(
                from: EmailAddress(email: "hadi@example.com", name: "Hadi"),
                to: ["hadi@domain.com"],
                subject: "Sent via Resend",
                replyTo: [
                    "hadi@example.com",
                    "<Mike>manager@example.com",
                    EmailAddress(email: "sales@example.com", name: "John")
                ],
                text: "sending email using Swift Resend SDK",
                headers: [
                    EmailHeaders(name: "X-Entity-Ref-ID", value: "234H3-44"),
                    EmailHeaders(name: "X-Entity-Dep-ID", value: "SALE-03"),
                ],
                attachments: [
                    EmailAttachment(filePath: "path/to/a/file")
                ],
                tags: [
                    EmailTags(name: "priority", value: "medium"),
                    EmailTags(name: "department", value: "sales")
                ]
        )

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

ResendEmail supports both text and html content.

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 Swift Resend SDK",
    )

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

Scheduled emails can be updated to new schedule or be canceled.

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

// update schedule 
_ = try await resendClient.emails.update(emailId: id, scheduledAt: "in 5 hours")

// cancel 
_ = try await resendClient.emails.cancel(emailId: id)

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 Email Information

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

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

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)

Managing Broadcasts

Access the BroadcastClient for managing broadcasts via the API. Broadcasts allow you to send emails to entire audiences. Refer to the Resend Broadcast API for complete details.

Creating a Broadcast

let broadcast = BroadcastCreate(
    audienceId: audience.id,
    from: EmailAddress(email: "sender@example.com", name: "Sender Name"),
    subject: "Welcome to our newsletter",
    replyTo: [
        EmailAddress(email: "support@example.com", name: "Support Team")
    ],
    html: "<h1>Welcome {{{FIRST_NAME|there}}}!</h1><p>Thank you for subscribing.</p>",
    text: "Welcome! Thank you for subscribing.",
    name: "Welcome Newsletter"
)
let broadcastId = try await resendClient.broadcasts.create(broadcast: broadcast)

Listing Broadcasts

let broadcasts = try await resendClient.broadcasts.list()
for broadcast in broadcasts.data {
    print("Broadcast: \(broadcast.id) - Status: \(broadcast.status)")
}

Getting Broadcast Details

let broadcast = try await resendClient.broadcasts.get(broadcastId: broadcastId)
print("Broadcast subject: \(broadcast.subject)")
print("From: \(broadcast.from.name) <\(broadcast.from.email)>")

Updating a Broadcast

let update = BroadcastUpdate(
    id: broadcastId,
    subject: "Updated Subject",
    html: "<h1>Updated Content</h1>"
)
let updatedBroadcast = try await resendClient.broadcasts.update(update: update)

Sending a Broadcast

let sentBroadcast = try await resendClient.broadcasts.send(broadcastId: broadcastId)
print("Broadcast sent with ID: \(sentBroadcast.id)")

Deleting a Broadcast

let deletedBroadcast = try await resendClient.broadcasts.delete(broadcastId: broadcastId)
print("Broadcast deleted: \(deletedBroadcast.id)")

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
  • Audiences
  • Contacts
  • Domains
  • API Keys
  • Broadcasts

License

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

About

Swift SDK for Resend email service

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages