To use this library in your project, drag Multipart.xcodeproj into your workspace.
Usage
Creating simple multipart messages
import Multipart
var message = Multipart(type: .alternative)
message.append(Part(body: "Lorem ipsum dolor sit amet.", contentType: "text/plain"))
message.append(Part(body: "<p><b>Lorem ipsum</b> dolor sit <i>amet</i>.</p>", contentType: "text/html"))
print(message)
Creating advanced multipart messages
import Multipart
// Construct a multipart/alternative message
var plainTextPart = Part(body: "Lorem ipsum dolor sit amet.".data(using: .ascii)!)
plainTextPart.setValue("text/plain", forHeaderField: "Content-Type")
plainTextPart.setAttribute(attribute: "charset", value: "us-ascii", forHeaderField: "Content-Type")
var htmlPart = Part(body: "<p><b>Lorem ipsum</b> dolor sit <i>amet</i>.</p>")
htmlPart.setValue("text/html", forHeaderField: "Content-Type")
htmlPart.setAttribute(attribute: "charset", value: "utf-8", forHeaderField: "Content-Type")
let textParts = Multipart(type: .alternative, parts: [plainTextPart, htmlPart])
// Add a file by wrapping it in a multipart/mixed message
var filePart = Part(body: "Ut enim ad minim veniam, quis nostrud exercitation ullamco.")
filePart.setValue("text/plain", forHeaderField: "Content-Type")
filePart.setAttribute(attribute: "charset", value: "utf-8", forHeaderField: "Content-Type")
filePart.setValue("attachment", forHeaderField: "Content-Disposition")
filePart.setAttribute(attribute: "filename", value: "attachment.txt", forHeaderField: "Content-Disposition")
var mixedMessage = Multipart(type: .mixed, parts: [textParts, filePart])
mixedMessage.preamble = "This is a multi-part message in MIME format."
print(mixedMessage)
Sending form data
Part.FormData is a helper function for quickly building multipart/form-data messages, while URLRequest.setMultipartBody
provides simple means of sending the data over HTTP.
Multipart
A simple library for creating multipart-encoded message bodies.
Integration
Swift Package Manager
You can use The Swift Package Manager by adding the proper description to your
Package.swift
file:Then run
swift build
.Manually
To use this library in your project, drag Multipart.xcodeproj into your workspace.
Usage
Creating simple multipart messages
Creating advanced multipart messages
Sending form data
Part.FormData
is a helper function for quickly buildingmultipart/form-data
messages, whileURLRequest.setMultipartBody
provides simple means of sending the data over HTTP.Uploading files
Part.FormData
also provides basic file upload functionality.Sending multipart messages manually
If
URLRequest.setMultipartBody
does not suit your needs, you can useMultipart.headers
andMultipart.body
to construct a request yourself.