|
| 1 | +# MailjetKit |
| 2 | + |
| 3 | +A Swift package for sending emails through the [Mailjet API](https://dev.mailjet.com/email/guides/send-api-V3-1/). |
| 4 | +Currently supports the `/send` endpoint (v3.1). |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +**MailjetKit** is a lightweight, type-safe Swift SDK for sending emails using Mailjet’s REST API. |
| 9 | +It focuses on simplicity, correctness, and modern Swift features such as: |
| 10 | + |
| 11 | +- `async/await` |
| 12 | +- `Codable`-based request/response models |
| 13 | +- Strong typing for messages, recipients, and attachments |
| 14 | +- Fluent, immutable “with/adding” helpers for constructing messages |
| 15 | + |
| 16 | +This package is ideal for server-side Swift (e.g. Vapor, Hummingbird, SwiftNIO apps), command-line tools, or macOS/iOS utilities that need to send transactional or templated emails. |
| 17 | + |
| 18 | +## Features |
| 19 | + |
| 20 | +- Fully asynchronous (using Swift Concurrency) |
| 21 | +- Type-safe Mailjet `/send` v3.1 endpoint |
| 22 | +- Strongly typed `Message`, `Envelope`, `Content`, and `Attachment` models |
| 23 | +- Immutable builder-style API (`addAttachment`, `addTo`, `addHeader`, etc.) |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +### Swift Package Manager |
| 28 | + |
| 29 | +Add **MailjetKit** to your `Package.swift` dependencies: |
| 30 | + |
| 31 | +```swift |
| 32 | +dependencies: [ |
| 33 | + .package(url: "https://github.com/adborbas/MailjetKit.git", from: "0.1.0") |
| 34 | +] |
| 35 | +``` |
| 36 | + |
| 37 | +Then add `"MailjetKit"` as a dependency to your target: |
| 38 | + |
| 39 | +```swift |
| 40 | +.target( |
| 41 | + name: "MyApp", |
| 42 | + dependencies: [ |
| 43 | + .product(name: "MailjetKit", package: "MailjetKit") |
| 44 | + ] |
| 45 | +) |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage and Examples |
| 49 | + |
| 50 | +💡 Before using MailjetKit, you’ll need a Mailjet account and API credentials. |
| 51 | +You can [sign up for a Mailjet account](https://app.mailjet.com/signup) and then [create an API key](https://app.mailjet.com/account/apikeys) from your account dashboard. |
| 52 | + |
| 53 | +### Create a Mailjet client |
| 54 | + |
| 55 | +```swift |
| 56 | +import MailjetKit |
| 57 | + |
| 58 | +let mailjet = MailjetKit( |
| 59 | + apiKey: "YOUR_API_KEY", |
| 60 | + apiSecret: "YOUR_API_SECRET" |
| 61 | +) |
| 62 | +``` |
| 63 | + |
| 64 | +### Send a simple email |
| 65 | + |
| 66 | +```swift |
| 67 | +let message = Message( |
| 68 | + from: Recipient( email: "[email protected]", name: "Your App"), |
| 69 | + to: Recipient( email: "[email protected]", name: "Test User"), |
| 70 | + subject: "Welcome to MailjetKit!", |
| 71 | + textPart: "Hello there! This is a plain text email from MailjetKit." |
| 72 | +) |
| 73 | + |
| 74 | +do { |
| 75 | + let response = try await mailjet.send(message: message) |
| 76 | + print("Message sent: \(response.status)") |
| 77 | +} catch { |
| 78 | + print("Error sending email: \(error)") |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Send an HTML email with attachments |
| 83 | + |
| 84 | +```swift |
| 85 | +let htmlMessage = Message( |
| 86 | + from: Recipient( email: "[email protected]"), |
| 87 | + to: Recipient( email: "[email protected]"), |
| 88 | + subject: "Hello, HTML!", |
| 89 | + htmlPart: "<h1>Hello</h1><p>This is an <strong>HTML</strong> message.</p>" |
| 90 | +) |
| 91 | +.addAttachment( |
| 92 | + Attachment( |
| 93 | + contentType: "text/plain", |
| 94 | + filename: "example.txt", |
| 95 | + base64Content: Data("Hello world!".utf8).base64EncodedString() |
| 96 | + ) |
| 97 | +) |
| 98 | + |
| 99 | +do { |
| 100 | + let response = try await mailjet.send(message: htmlMessage) |
| 101 | + print("✅ Sent successfully: \(response)") |
| 102 | +} catch { |
| 103 | + print("❌ Failed: \(error)") |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Using a templated email |
| 108 | + |
| 109 | +```swift |
| 110 | +let templatedMessage = Message( |
| 111 | + envelope: Envelope( |
| 112 | + from: Recipient( email: "[email protected]"), |
| 113 | + to: [ Recipient( email: "[email protected]")] |
| 114 | + ), |
| 115 | + template: TemplateOptions( |
| 116 | + templateID: 123456, |
| 117 | + templateLanguage: true, |
| 118 | + variables: [ |
| 119 | + "name": "Jane Doe", |
| 120 | + "product": "MailjetKit" |
| 121 | + ] |
| 122 | + ) |
| 123 | +) |
| 124 | + |
| 125 | +try await mailjet.send(message: templatedMessage) |
| 126 | +``` |
0 commit comments