Documentation

Attachment extends Part
in package
uses CharsetAwareTrait, PartContentTrait

Attachment message part class

Tags
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
5.0.0

Table of Contents

Properties

$body  : Body|null
Body
$boundary  : string|null
Boundary
$charSet  : string|null
Character set
$headers  : array<string|int, mixed>
Headers
$parts  : array<string|int, mixed>
Nested parts
$sourcePath  : string|null
The original on-disk path this attachment was created from, if any.
$subType  : string|null
Subtype

Methods

__construct()  : mixed
Constructor
__toString()  : string
Render the part
addFile()  : Part
Add file as body
addHeader()  : Part
Add a header
addHeaders()  : Part
Add headers
addPart()  : Part
Add a nested part
addParts()  : Part
Add nested parts
attachment()  : static
Create an attachment part from a file on disk
attachmentFromContent()  : static
Create an attachment part from in-memory content
create()  : static
Create an attachment from a file on disk
createFromContent()  : static
Create an attachment from in-memory content
generateBoundary()  : string
Generate boundary
generateId()  : string
Generate a message/content ID
getAttachments()  : array<string|int, mixed>
Get attachments
getBasename()  : string|null
Get the basename decoded from headers (the parent's original getFilename() behavior)
getBody()  : Body|null
Get body
getBoundary()  : string
Get boundary
getCharSet()  : string|null
Get character set
getContent()  : string|null
Get content
getContents()  : mixed
Get contents (decoded)
getContentType()  : string|null
Get content-type
getFilename()  : string|null
Get the real on-disk path this attachment was created from.
getHeader()  : Header|null
Get headers
getHeaders()  : array<string|int, mixed>
Get headers
getHeadersAsArray()  : array<string|int, mixed>
Get headers as array
getParts()  : array<string|int, mixed>
Get nested parts
getSubType()  : string|null
Get subtype
hasAttachment()  : bool
Has attachment (check via a header)
hasAttachments()  : bool
Does message have attachments (check via parts)
hasBody()  : bool
Has body
hasBoundary()  : bool
Has boundary
hasHeader()  : bool
Has header
hasHeaders()  : bool
Has headers
hasParts()  : bool
Has nested parts
hasSubType()  : bool
Has subtype
html()  : static
Create a text/html part
inferSubType()  : Part
Infer and set the subtype (alternative/mixed) from the current nested parts
removeHeader()  : Part
Remove header
render()  : string
Render the part
renderAsLines()  : array<string|int, mixed>
Render as an array of lines
renderBody()  : string
Render the part body
renderHeaders()  : string
Render the part headers
renderParts()  : string
Render the parts
renderRaw()  : string
Render the part raw (no headers or preamble)
setBody()  : Part
Set body
setBoundary()  : Part
Set boundary
setCharSet()  : static
Set character set
setContent()  : static
Set content
setContentId()  : Part
Set the Content-ID header
setContentType()  : static
Set Content-Type header
setSubType()  : Part
Set subtype
text()  : static
Create a text/plain part
detectContentType()  : string
Detect a content type from a filename's extension

Properties

$boundary

Boundary

protected string|null $boundary = null

$headers

Headers

protected array<string|int, mixed> $headers = []

$parts

Nested parts

protected array<string|int, mixed> $parts = []

$sourcePath

The original on-disk path this attachment was created from, if any.

protected string|null $sourcePath = null

Deliberately diverges from parent::getFilename() (which returns the basename, decoded from headers) - three transports need the real openable path for curl_file_create().

$subType

Subtype

protected string|null $subType = null

Methods

__construct()

Constructor

public __construct() : mixed

Instantiate the mime part object

__toString()

Render the part

public __toString() : string
Return values
string

addFile()

Add file as body

public addFile(string $file[, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : Part
Parameters
$file : string
$disposition : string = 'attachment'
$encoding : Encoding = PartBodyEncoding::BASE64
$split : int|bool = true
Tags
throws
Exception
Return values
Part

addHeader()

Add a header

public addHeader(Header|string $header[, string|null $value = null ]) : Part
Parameters
$header : Header|string
$value : string|null = null
Return values
Part

addHeaders()

Add headers

public addHeaders(array<string|int, mixed> $headers) : Part
Parameters
$headers : array<string|int, mixed>
Return values
Part

addPart()

Add a nested part

public addPart(Part $part) : Part
Parameters
$part : Part
Return values
Part

addParts()

Add nested parts

public addParts(array<string|int, mixed> $parts) : Part
Parameters
$parts : array<string|int, mixed>
Return values
Part

attachment()

Create an attachment part from a file on disk

public static attachment(string $file[, string|null $contentType = null ][, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : static
Parameters
$file : string
$contentType : string|null = null
$disposition : string = 'attachment'
$encoding : Encoding = PartBodyEncoding::BASE64
$split : int|bool = true
Return values
static

attachmentFromContent()

Create an attachment part from in-memory content

public static attachmentFromContent(string $content, string $filename[, string|null $contentType = null ][, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : static
Parameters
$content : string
$filename : string
$contentType : string|null = null
$disposition : string = 'attachment'
$encoding : Encoding = PartBodyEncoding::BASE64
$split : int|bool = true
Return values
static

create()

Create an attachment from a file on disk

public static create(string $file[, string|null $contentType = null ][, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : static
Parameters
$file : string
$contentType : string|null = null
$disposition : string = 'attachment'
$encoding : Encoding = PartBodyEncoding::BASE64
$split : int|bool = true
Tags
throws
Exception
Return values
static

createFromContent()

Create an attachment from in-memory content

public static createFromContent(string $content, string $filename[, string|null $contentType = null ][, string $disposition = 'attachment' ][, Encoding $encoding = PartBodyEncoding::BASE64 ][, int|bool $split = true ]) : static
Parameters
$content : string
$filename : string
$contentType : string|null = null
$disposition : string = 'attachment'
$encoding : Encoding = PartBodyEncoding::BASE64
$split : int|bool = true
Return values
static

generateBoundary()

Generate boundary

public generateBoundary() : string
Return values
string

generateId()

Generate a message/content ID

public generateId([string|null $domain = null ]) : string
Parameters
$domain : string|null = null
Return values
string

getAttachments()

Get attachments

public getAttachments() : array<string|int, mixed>
Return values
array<string|int, mixed>

getBasename()

Get the basename decoded from headers (the parent's original getFilename() behavior)

public getBasename() : string|null
Return values
string|null

getBody()

Get body

public getBody() : Body|null
Return values
Body|null

getBoundary()

Get boundary

public getBoundary() : string
Return values
string

getCharSet()

Get character set

public getCharSet() : string|null
Return values
string|null

getContent()

Get content

public getContent() : string|null
Return values
string|null

getContents()

Get contents (decoded)

public getContents() : mixed

getContentType()

Get content-type

public getContentType() : string|null
Return values
string|null

getFilename()

Get the real on-disk path this attachment was created from.

public getFilename() : string|null

No fallback to parent::getFilename() is used deliberately. The parent method returns a basename decoded from headers; Part::attachmentFromContent() marks the body as file-like and unconditionally sets a Content-Disposition filename= parameter. A fallback would return a fabricated basename (e.g. 'generated.txt') instead of null for in-memory attachments created via createFromContent(). This method guarantees null for in-memory attachments to honor that contract.

Return values
string|null

The real path if created from a file, null if in-memory

getHeader()

Get headers

public getHeader(string $name) : Header|null
Parameters
$name : string
Return values
Header|null

getHeaders()

Get headers

public getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed>

getHeadersAsArray()

Get headers as array

public getHeadersAsArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

getParts()

Get nested parts

public getParts() : array<string|int, mixed>
Return values
array<string|int, mixed>

getSubType()

Get subtype

public getSubType() : string|null
Return values
string|null

hasAttachment()

Has attachment (check via a header)

public hasAttachment() : bool
Return values
bool

hasAttachments()

Does message have attachments (check via parts)

public hasAttachments() : bool
Return values
bool

hasBody()

Has body

public hasBody() : bool
Return values
bool

hasBoundary()

Has boundary

public hasBoundary() : bool
Return values
bool

hasHeader()

Has header

public hasHeader(string $name) : bool
Parameters
$name : string
Return values
bool

hasHeaders()

Has headers

public hasHeaders() : bool
Return values
bool

hasParts()

Has nested parts

public hasParts() : bool
Return values
bool

hasSubType()

Has subtype

public hasSubType() : bool
Return values
bool

html()

Create a text/html part

public static html(string $content) : static
Parameters
$content : string
Return values
static

inferSubType()

Infer and set the subtype (alternative/mixed) from the current nested parts

public inferSubType() : Part
Return values
Part

removeHeader()

Remove header

public removeHeader(string $name) : Part
Parameters
$name : string
Return values
Part

render()

Render the part

public render([bool $preamble = true ]) : string
Parameters
$preamble : bool = true
Return values
string

renderAsLines()

Render as an array of lines

public renderAsLines() : array<string|int, mixed>
Return values
array<string|int, mixed>

renderBody()

Render the part body

public renderBody() : string
Return values
string

renderHeaders()

Render the part headers

public renderHeaders() : string
Return values
string

renderParts()

Render the parts

public renderParts([bool $preamble = true ][, bool $headers = true ]) : string
Parameters
$preamble : bool = true
$headers : bool = true
Return values
string

renderRaw()

Render the part raw (no headers or preamble)

public renderRaw() : string
Return values
string

setBody()

Set body

public setBody(Body|string $body) : Part
Parameters
$body : Body|string
Return values
Part

setBoundary()

Set boundary

public setBoundary(string $boundary) : Part
Parameters
$boundary : string
Return values
Part

setCharSet()

Set character set

public setCharSet([string|null $charSet = null ]) : static
Parameters
$charSet : string|null = null
Return values
static

setContent()

Set content

public setContent(string $content) : static
Parameters
$content : string
Return values
static

setContentId()

Set the Content-ID header

public setContentId([string|null $id = null ][, string|null $domain = null ]) : Part
Parameters
$id : string|null = null
$domain : string|null = null
Return values
Part

setContentType()

Set Content-Type header

public setContentType(string $contentType) : static
Parameters
$contentType : string
Return values
static

setSubType()

Set subtype

public setSubType(string $subType) : Part
Parameters
$subType : string
Return values
Part

text()

Create a text/plain part

public static text(string $content) : static
Parameters
$content : string
Return values
static

detectContentType()

Detect a content type from a filename's extension

protected static detectContentType(string $filename) : string
Parameters
$filename : string
Return values
string

        
On this page

Search results