Skip to content

Commit 908a1a4

Browse files
committed
Updated README.md
1 parent 11d6b4d commit 908a1a4

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

README.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ A library to support (streaming) multiparts.
66

77
### multipart/form-data
88

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:
1010

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.
1313

1414
An example:
1515

@@ -31,23 +31,61 @@ PHP servers require multiple values or files to be sent with a name that ends wi
3131
$multipart->addFile('file[]', 'file.html', '<html>Hello World</html>', 'text/html');
3232
$multipart->finish();
3333

34+
### multipart/related
35+
36+
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
44+
$multipart = new MultipartRelated();
45+
$multipart->addPart(file_get_contents('body.html'), 'text/html');
46+
// the content length is irrelevant because the content is a string
47+
$multipart->addInlineFile('logo', 'logo.png', base64_encode(file_get_contents('logo.png')), 'image/png', -1, 'base64');
48+
$multipart->finish();
49+
50+
To use this inline file in the HTML body, use `cid:logo` as the source of an image.
51+
3452
### multipart/alternative
3553

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
64+
$multipart->addPart(file_get_contents('body.txt'), 'text/plain');
65+
$multipart->addMultipart($related);
66+
$multipart->finish();
67+
68+
### multipart/mixed
69+
70+
To create a multipart/mixed object, create a `MultipartMixed` instance, add the parts, and call `finish()`. There are three methods for adding parts:
3771

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.
3975

4076
An example:
4177

4278
// the multipart object can take an optional pre-existing boundary
4379
$multipart = new MultipartAlternative();
44-
$multipart->addAlternative(file_get_contents('body.txt'), 'text/plain');
45-
$multipart->addAlternative(base64_encode(file_get_contents('body.html')), 'text/html', -1, 'base64');
80+
// $alternative is a MultipartAlternative instance as created above
81+
$multipart->addMultipart($alternative);
82+
// the content length is irrelevant because the content is a string
83+
$multipart->addFile('file.png', base64_encode(file_get_contents('file.png')), 'image/png', -1, 'base64');
4684
$multipart->finish();
4785

48-
## Multipart files
86+
## Multipart content
4987

50-
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:
5189

5290
* As a string. The content length will be ignored.
5391
* 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:
90128

91129
## Non-streaming support
92130

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).
94132

95133
`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

Comments
 (0)