A modest little SendGrid Swift SDK, with async/await.
Send a basic html email.
import XSendGrid
let email = SendGrid.Email(
to: "[email protected]",
from: "[email protected]",
subject: "Ms. Fluff's birthday",
html: "<h1>You're invited!</h1>"
)
let client = SendGrid.Client.live
let apiKey = "sg_123sosecret"
let responseData = try await client.send(email, apiKey)
Send a plain text email:
let email = SendGrid.Email(
to: "[email protected]",
from: "[email protected]",
subject: "Ms. Fluff's birthday",
text: "You're invited!" // 👋 `text` instead of `html`
)
// [...]
let responseData = try await client.send(email, apiKey)
Add an attachment:
var email = SendGrid.Email(/* [...] */)
// 👋 optionally add one or more attachments
email.attachments = [.init(data: fileData, filename: "ms-fluff.gif?raw=true")]
// [...]
let responseData = try await client.send(email, apiKey)
This library was designed to be used with the dependency injection approach from pointfree.co:
import XSendGrid
struct Environment {
var sendGridClient: SendGrid.Client
// other dependencies...
}
extension Environment {
static let live = Environment(sendGridClient: SendGrid.Client.live)
static let mock = Environment(sendGridClient: SendGrid.Client.mock)
}
var Current = Environment.live
// you can swap out your own mock implementation:
Current.sendGridClient.send = { _, _ in fatalError("shouldn't be called") }
Use SPM:
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "RadProject",
products: [
.library(name: "RadProject", targets: ["RadProject"]),
],
dependencies: [
+ .package(url: "https://github.com/jaredh159/x-sendgrid.git", from: "1.0.0")
],
targets: [
.target(name: "RadProject", dependencies: [
+ .product(name: "XSendGrid", package: "x-sendgrid"),
]),
]
)
link |
Stars: 0 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics