choonkeat / elm-multipart / Multipart


type Multipart

use the string function to convert a Multipart value into a String


type alias ContentType =
String

e.g. text/html; charset="utf-8"


type Boundary

7-bit ASCII like e.g. z84d86635fa691af3d9d7d9436cc5d44018a7bc8

boundary : String -> Boundary

clamps given String to 7-bit ASCII


type Header

header : String -> String -> Header

header "Content-Type" "application/json"

Empty initial values

mixed : Boundary -> Multipart

An empty Content-Type: multipart/mixed parts container.

The primary subtype for multipart, "mixed", is intended for use when the body parts are independent and intended to be displayed serially. Any multipart subtypes that an implementation does not recognize should be treated as being of subtype "mixed". https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

alternative : Boundary -> Multipart

An empty Content-Type: multipart/alternative parts container.

each of the parts is an "alternative" version of the same information. User agents should recognize that the content of the various parts are interchangeable. The user agent should either choose the "best" type based on the user's environment and preferences, or offer the user the available alternatives. In general, choosing the best type means displaying only the LAST part that can be displayed. https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html

In the following multipart, the "best" type is text/html

Multipart.alternative (Multipart.boundary "boundary-string")
    |> Multipart.addStringPart "text/plain; charset=\"utf-8\"" textHeaders textContent
    |> Multipart.addStringPart "text/html; charset=\"utf-8\"" htmlHeaders htmlContent
    |> Multipart.string

subtype : String -> Boundary -> Multipart

An empty Content-Type: multipart/{{subtype}} parts container. Where {{subtype}} is any String you supply

See https://en.wikipedia.org/wiki/MIME#Multipart_subtypes

Multipart.subtype "related" (Multipart.boundary "boundary-string")
    |> Multipart.addStringPart "text/css; charset=\"utf-8\"" cssHeaders cssContent
    |> Multipart.addStringPart "text/html; charset=\"utf-8\"" htmlHeaders htmlContent
    |> Multipart.string

Adding parts inside

addStringPart : ContentType -> List Header -> String -> Multipart -> Multipart

Add a section to a Multipart value

Multipart.mixed (Multipart.boundary "boundary-string")
    |> Multipart.addStringPart "text/plain" [] "hello world!"
    |> Multipart.string
--> "Content-Type: text/plain\r\n\r\nhello world!"

A Multipart with more than 1 part carries Content-Type header of multipart/alternative

Multipart.alternative (Multipart.boundary "boundary-string")
    |> Multipart.addStringPart "text/plain" [] "hello world!"
    |> Multipart.addStringPart "text/html" [] "<p>hello world!</p>"
    |> Multipart.string
--> "Content-Type: multipart/alternative; boundary=\"boundary-string\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n--boundary-string\r\nContent-Type: text/plain\r\n\r\nhello world!\r\n\r\n--boundary-string\r\nContent-Type: text/html\r\n\r\n<p>hello world!</p>\r\n\r\n--boundary-string--"

addMultipart : Multipart -> Multipart -> Multipart

Embeds another multipart into an outer multipart. The

Multipart.mixed (Multipart.boundary "boundary-string111")
    |> Multipart.addStringPart "text/plain" [] "hello world!"
    |> Multipart.addMultipart
        (Multipart.alternative (Multipart.boundary "boundary-string222")
            |> Multipart.addStringPart "text/plain" [] "hello world!"
            |> Multipart.addStringPart "text/html" [] "<p>hello world!</p>"
        )
    |> Multipart.string
--> "Content-Type: multipart/mixed; boundary=\"boundary-string111\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n--boundary-string111\r\nContent-Type: text/plain\r\n\r\nhello world!\r\n\r\n--boundary-string111\r\nContent-Type: multipart/alternative; boundary=\"boundary-string222\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n--boundary-string222\r\nContent-Type: text/plain\r\n\r\nhello world!\r\n\r\n--boundary-string222\r\nContent-Type: text/html\r\n\r\n<p>hello world!</p>\r\n\r\n--boundary-string222--\r\n\r\n--boundary-string111--"

Return a useful value

string : Multipart -> String

Returns a Multipart value as String so you can use it, e.g. as an email body