mailslurp-examples - swift-email-smtp-examples

https://github.com/mailslurp/examples

Table of Contents

swift-email-smtp-examples/README.md

# Swift email examples (SMTP and HTTP API)

Example Package Manager project demonstrating use of MailSlurp email APIs in Swift.

See 

swift-email-smtp-examples/Package.swift

// swift-tools-version:5.5

import PackageDescription

let package = Package(
        name: "swift-email-smtp-examples",
        products: [
        ],
        dependencies: [
            .package(url: "https://github.com/mailslurp/mailslurp-client-swift", revision: "15.12.9"),
        ],
        targets: [
            .testTarget(
                    name: "swift-email-smtp-examplesTests",
                    dependencies: [
                        .product(name: "mailslurp", package: "mailslurp-client-swift"),
                    ]),
        ]
)

swift-email-smtp-examples/Package.resolved

{
  "object": {
    "pins": [
      {
        "package": "AnyCodable",
        "repositoryURL": "https://github.com/Flight-School/AnyCodable",
        "state": {
          "branch": null,
          "revision": "38b05fc9f86501ef8018aa90cf3d83bd97f74067",
          "version": "0.4.0"
        }
      },
      {
        "package": "mailslurp",
        "repositoryURL": "https://github.com/mailslurp/mailslurp-client-swift",
        "state": {
          "branch": "15.12.9",
          "revision": "203230305b32c4f33ba7bbca3ee8674c956d57de",
          "version": null
        }
      }
    ]
  },
  "version": 1
}

swift-email-smtp-examples/Makefile

-include ../.env

Package.resolved:
	swift package update

test: Package.resolved
	API_KEY=$(API_KEY) swift test

test-ci: Package.resolved test

swift-email-smtp-examples/Tests/swift-email-smtp-examplesTests/swift_email_smtp_examplesTests.swift

import XCTest
import mailslurp

import class Foundation.Bundle

final class swift_email_smtp_examplesTests: XCTestCase {


        func testSmtpEmailSending() async throws {
        let apiKey = ProcessInfo.processInfo.environment["API_KEY"]
        XCTAssertNotNil(apiKey)
        XCTAssertGreaterThan(apiKey?.count ?? 0, 0, "Expecting API_KEY environment variable to be set and not empty.")

        let ex = self.expectation(description: "Can create inbox")
            DispatchQueue.main.async{

                let apiKey = ProcessInfo.processInfo.environment["API_KEY"] ?? ""
                InboxControllerAPI.createInboxWithDefaultsWithRequestBuilder()
                        .addHeader(name: "x-api-key", value: apiKey)
                        .execute()
                        .done { response in
                            XCTAssertTrue(response.statusCode == 201)
                            XCTAssertTrue(response.body?.emailAddress?.contains("@mailslurp") ?? false)
                            expectation.fulfill()
                        }.catch { errorType in
                            XCTAssertFalse(true, "Should not throw exception")
                        }
                waitForExpectations(timeout: 5, handler: nil)

    }
}