You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-10Lines changed: 48 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,10 @@ A library to support (streaming) multiparts.
6
6
7
7
### multipart/form-data
8
8
9
-
To create a multipart/form-data object, create a `MultipartFormData`object, add the form fields, and call `finish()`. There are two methods for adding form fields:
9
+
To create a multipart/form-data object, create a `MultipartFormData`instance, add the form fields, and call `finish()`. There are two methods for adding form fields:
10
10
11
-
*`addValue($name, $value)` adds a string value with the given name.
12
-
*`addFile($name, $filename, $content, $contentType, $contentLength = -1)` adds a file with the given name. The filename, [content](#multipart-files) and content type are required; the content length is optional, and will be ignored if the content is a string.
11
+
*`addValue($name, $value)` adds a string value with the given name. Both arguments are required.
12
+
*`addFile($name, $filename, $content, $contentType, $contentLength = -1)` adds a file with the given name. The name, filename, [content](#multipart-content) and content type are required; the content length is optional, and will be ignored if the content is a string.
13
13
14
14
An example:
15
15
@@ -31,23 +31,61 @@ PHP servers require multiple values or files to be sent with a name that ends wi
To create a multipart/related object, create a `MultipartRelated` instance, add the root part and any inline files, and call `finish()`. There are two methods for adding parts:
37
+
38
+
*`addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')` adds a part without a content disposition. This should be used for the root part. The [content](#multipart-content) and content type are required; the content length is optional, and will be ignored if the content is a string; the content transfer encoding is optional, and will be used for the `Content-Transfer-Encoding` header if it is set.
39
+
*`addInlineFile($contentID, $filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')` adds an inline file. The content ID, filename, [content](#multipart-content) and content type are required; the content length is optional, and will be ignored if the content is a string; the content transfer encoding is optional, and will be used for the `Content-Transfer-Encoding` header if it is set.
40
+
41
+
An example:
42
+
43
+
// the multipart object can take an optional pre-existing boundary
To use this inline file in the HTML body, use `cid:logo` as the source of an image.
51
+
34
52
### multipart/alternative
35
53
36
-
To create a multipart/alternative object, create a `MultipartAlternative` object, add the alternatives, and call `finish()`. There is one method for adding alternatives:
54
+
To create a multipart/alternative object, create a `MultipartAlternative` instance, add the alternatives, and call `finish()`. There are two methods for adding alternatives:
55
+
56
+
*`addMultipart(Multipart $multipart)` adds another multipart as alternative. This is most often used with a multipart/related object.
57
+
*`addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')` adds a part with the given content. The [content](#multipart-content) and content type are required; the content length is optional, and will be ignored if the content is a string; the content transfer encoding is optional, and will be used for the `Content-Transfer-Encoding` header if it is set.
58
+
59
+
An example:
60
+
61
+
// the multipart object can take an optional pre-existing boundary
62
+
$multipart = new MultipartAlternative();
63
+
// $related is a MultipartRelated instance as created above
To create a multipart/mixed object, create a `MultipartMixed` instance, add the parts, and call `finish()`. There are three methods for adding parts:
37
71
38
-
*`addAlternative($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')`. The [content](#multipart-files) and content type are required; the content length is optional, and will be ignored if the content is a string. The optional content transfer encoding will be used for the `Content-Transfer-Encoding` header.
72
+
*`addMultipart(Multipart $multipart)` adds another multipart. This is most often used with a multipart/alternative or multipart/related object.
73
+
*`addPart($content, $contentType, $contentLength = -1, $contentTransferEncoding = '')` adds a part with the given content. This can be used for the bodies of plain text emails. The [content](#multipart-content) and content type are required; the content length is optional, and will be ignored if the content is a string; the content transfer encoding is optional, and will be used for the `Content-Transfer-Encoding` header if it is set.
74
+
*`addAttachment($filename, $content, $contentType, $contentLength = -1, $contentTransferEncoding = '')` adds a part with content disposition `attachment`. The filename, [content](#multipart-content) and content type are required; the content length is optional, and will be ignored if the content is a string; the content transfer encoding is optional, and will be used for the `Content-Transfer-Encoding` header if it is set.
39
75
40
76
An example:
41
77
42
78
// the multipart object can take an optional pre-existing boundary
For multiparts that support files, the content can be given in one of three ways:
88
+
The content of a part or file can be given in one of three ways:
51
89
52
90
* As a string. The content length will be ignored.
53
91
* As a resource that can be read using `fread`. It is up to the caller to close this resource.
@@ -90,6 +128,6 @@ For instance:
90
128
91
129
## Non-streaming support
92
130
93
-
If streaming is not possible (e.g. because a string is required), you can buffer a multipart object in-memory by calling the `buffer` method. This method takes an optional buffer size, and returns the buffered contents. The content length will be set accordingly. Note that you should do this before calling `read` (or `curl_read`), otherwise the buffered contents may not contain all desired contents (especially if you're using resources or callables).
131
+
If streaming is not possible (e.g. because a string is required, like in the `mail` function), you can buffer a multipart object in-memory by calling the `buffer` method. This method takes an optional buffer size, and returns the buffered contents. The content length will be set accordingly. Note that you should do this before calling `read` (or `curl_read`), otherwise the buffered contents may not contain all desired contents (especially if you're using resources or callables).
94
132
95
133
`Multipart.__toString()` has been overridden to buffer the multipart object as well, so you can achieve the same by casting a multipart object to `string`. The difference is that `buffer` requires the multipart object to be finished.
0 commit comments