Coder Social home page Coder Social logo

swift-resend's Introduction

SwiftResend

Swift 5.7 Logo MIT 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

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

import Resend

let email: ResendEmail = .init(
    from: .init(email: "[email protected]", name: "Hadi"),
    to: ["[email protected]"],
    subject: "running xctest",
    replyTo: [
        "[email protected]",
        "[email protected]"
    ],
    text: "sending email from XCTest suit",
    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")
    ]
)

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 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: "[email protected]",
                                                       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
  • Audiences
  • Contacts
  • Domains
  • API Keys

License

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

swift-resend's People

Contributors

hsharghi avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.