A Swift implementation of RFC 6068 - The 'mailto' URI Scheme.
RFC 6068 defines the mailto URI scheme for designating email addresses. This package provides type-safe parsing and serialization of mailto URIs with full support for header fields (subject, body, cc, bcc, etc.) and percent-encoding per RFC 3986.
This RFC obsoletes RFC 2368 and adds support for Internationalized Resource Identifiers (IRIs) per RFC 3987.
- Complete mailto URI parsing from ASCII bytes
- Header field (hfield) extraction and construction
- Percent-encoding/decoding per RFC 3986
- Convenience accessors for common headers (subject, body, cc, bcc)
- Full
UInt8.ASCII.Serializableconformance - Sendable and Codable types
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/swift-standards/swift-rfc-6068", from: "0.1.0")
]And add the dependency to your target:
.target(
name: "YourTarget",
dependencies: [
.product(name: "RFC 6068", package: "swift-rfc-6068")
]
)import RFC_6068
// Parse a mailto URI
let mailto = try RFC_6068.Mailto(ascii: "mailto:user@example.com?subject=Hello".utf8)
print(mailto.to.first?.rawValue) // "user@example.com"
print(mailto.subject) // "Hello"
// Create a mailto URI programmatically
let addr = try RFC_5322.EmailAddress("user@example.com")
let mailto = RFC_6068.Mailto(
to: [addr],
headers: [.subject("Hello World"), .body("Message content")]
)
print(String(mailto)) // "mailto:user@example.com?subject=Hello%20World&body=Message%20content"// Simple mailto
let mailto = try RFC_6068.Mailto(ascii: "mailto:chris@example.com".utf8)
// With multiple recipients
let mailto = try RFC_6068.Mailto(ascii: "mailto:user1@example.com,user2@example.com".utf8)
print(mailto.to.count) // 2
// With percent-encoded values
let mailto = try RFC_6068.Mailto(ascii: "mailto:user@example.com?subject=Hello%20World".utf8)
print(mailto.subject) // "Hello World"
// Headers-only (no recipients in path)
let mailto = try RFC_6068.Mailto(ascii: "mailto:?to=user@example.com&subject=Test".utf8)let mailto = try RFC_6068.Mailto(
ascii: "mailto:list@example.com?subject=Subscribe&cc=admin@example.com".utf8
)
// Convenience accessors
mailto.subject // "Subscribe"
mailto.body // nil
mailto.cc // [RFC_5322.EmailAddress]
mailto.bcc // [RFC_5322.EmailAddress]
// All recipients (path + header To addresses)
mailto.allTo // Combined listlet mailto = RFC_6068.Mailto(
to: [try RFC_5322.EmailAddress("recipient@example.com")],
headers: [
.subject("Meeting Request"),
.body("Please confirm your attendance."),
.cc("manager@example.com")
]
)
// Serialize to string
let uriString = String(mailto)
// Serialize to bytes
let bytes = [UInt8](mailto)| Package | Description |
|---|---|
| swift-rfc-3986 | URI Generic Syntax |
| swift-rfc-3987 | Internationalized Resource Identifiers (IRIs) |
| swift-rfc-5322 | Internet Message Format |
| swift-rfc-2369 | URLs for Mailing List Management |
| swift-rfc-8058 | One-Click Unsubscribe for List Email |
| swift-incits-4-1986 | US-ASCII character operations |
This project is licensed under the Apache License, Version 2.0. See LICENSE for details.
Contributions are welcome. Please open an issue or submit a pull request.