MailMimeParser
in package
Parses a MIME message into an {@see IMessage} object.
The class sets up the dependency injection container (using PHP-DI) with the ability to override and/or provide specialized classes. To override you can:
- Provide an array|string|DefinitionSource to the constructor to affect classes used on a single instance of MailMimeParser
- Call MailMimeParser::addGlobalContainerDefinition with an array|string|DefinitionSource to to override it globally on all instances of MailMimeParser
- Call MailMimeParser::getGlobalContainer(), and use set() to override individual definitions globally.
You may also provide a LoggerInterface on the constructor for a single instance, or override it globally by calling setGlobalLogger. This is the same as setting up Psr\Log\LoggerInterface with your logger class in a Php-Di configuration in one of the above methods.
To invoke the parser, call parse on a MailMimeParser object.
$parser = new MailMimeParser();
// the resource is attached due to the second parameter being true and will
// be closed when the returned IMessage is destroyed
$message = $parser->parse(fopen('path/to/file.txt'), true);
// use $message here
Tags
Table of Contents
- DEFAULT_CHARSET = 'UTF-8'
- DEFAULT_DEFINITIONS_FILE = __DIR__ . '/di_config.php'
- PLUGIN_EXTRA_KEY = 'mail-mime-parser'
- The key in a package's composer.json "extra" section that MMP looks for to auto-discover plugin DI configurations.
- $container : Container
- $messageParser : MessageParserService
- $globalContainer : Container|null
- $globalDefinitions : array<string|int, mixed>
- __construct() : mixed
- Provide custom php-di configuration to customize dependency injection, or provide a custom logger for the instance only.
- addGlobalPhpDiContainerDefinition() : void
- getGlobalContainer() : Container
- Returns the global php-di container instance.
- parse() : IMessage
- Parses the passed stream handle or string into an {@see IMessage} object and returns it.
- parsePluginConfigs() : array<string|int, string>
- Parses an installed.json file and returns absolute paths to plugin DI config files.
- resetGlobalPhpDiContainerDefinitions() : void
- setFallbackCharset() : void
- Sets the fallback charset used for text/* content parts that don't declare a charset. Defaults to 'ISO-8859-1' per RFC 2045.
- setGlobalLogger() : void
- Registers the provided logger globally.
- setGlobalPhpDiConfigurations() : void
- Sets global configuration for php-di. Overrides all previously set definitions. You can optionally not use the default MMP definitions file by passing 'false' to the $useDefaultDefinitionsFile argument.
- discoverPluginConfigs() : array<string|int, string>
- Discovers plugin DI config files from installed Composer packages.
- getGlobalContainerBuilder() : ContainerBuilder<string|int, Container>
- Returns the default ContainerBuilder with default loaded definitions.
Constants
DEFAULT_CHARSET
public
string
DEFAULT_CHARSET
= 'UTF-8'
the default charset used to encode strings (or string content like streams) returned by MailMimeParser (for e.g. the string returned by calling $message->getTextContent()).
DEFAULT_DEFINITIONS_FILE
private
string
DEFAULT_DEFINITIONS_FILE
= __DIR__ . '/di_config.php'
the default definition file.
PLUGIN_EXTRA_KEY
The key in a package's composer.json "extra" section that MMP looks for to auto-discover plugin DI configurations.
private
mixed
PLUGIN_EXTRA_KEY
= 'mail-mime-parser'
Properties
$container
protected
Container
$container
The instance's dependency injection container.
$messageParser
protected
MessageParserService
$messageParser
for parsing messages
$globalContainer
private
static Container|null
$globalContainer
= null
The static global container
$globalDefinitions
private
static array<string|int, mixed>
$globalDefinitions
= [self::DEFAULT_DEFINITIONS_FILE]
an array of global definitions being used.
Methods
__construct()
Provide custom php-di configuration to customize dependency injection, or provide a custom logger for the instance only.
public
__construct([LoggerInterface|null $logger = null ][, array<string, mixed>|string|DefinitionSource|null $phpDiContainerConfig = null ][, bool $useGlobalDefinitions = true ]) : mixed
Note: this only affects instances created through this instance of the MailMimeParser, or the container itself. Calling 'new MimePart()' directly for instance, would use the global service locator to setup any dependencies MimePart needs. This applies to a provided $logger too -- it would only affect instances of objects created through the provided MailMimeParser.
Passing false to $useGlobalDefinitions will cause MMP to not use any global definitions. The default definitions file MailMimeParser::DEFAULT_DEFINITIONS_FILE will still be added though.
Parameters
- $logger : LoggerInterface|null = null
- $phpDiContainerConfig : array<string, mixed>|string|DefinitionSource|null = null
- $useGlobalDefinitions : bool = true
Tags
Return values
mixed —addGlobalPhpDiContainerDefinition()
public
static addGlobalPhpDiContainerDefinition(array<string, mixed>|string|DefinitionSource $phpDiConfig) : void
Parameters
- $phpDiConfig : array<string, mixed>|string|DefinitionSource
Return values
void —getGlobalContainer()
Returns the global php-di container instance.
public
static getGlobalContainer() : Container
Return values
Container —parse()
Parses the passed stream handle or string into an {@see IMessage} object and returns it.
public
parse(resource|StreamInterface|string $resource, bool $autoClose) : IMessage
If the passed $resource is a resource handle or StreamInterface, the resource must remain open while the returned IMessage object exists. Pass true as the second argument to have the resource automatically closed when the returned IMessage is destroyed, or pass false to manage the resource lifecycle yourself.
Parameters
- $resource : resource|StreamInterface|string
-
The resource handle to the input stream of the mime message, or a string containing a mime message.
- $autoClose : bool
-
pass true to have the resource closed automatically when the returned IMessage is destroyed.
Return values
IMessage —parsePluginConfigs()
Parses an installed.json file and returns absolute paths to plugin DI config files.
public
static parsePluginConfigs(string $installedJsonPath) : array<string|int, string>
Looks for packages with an "extra.mail-mime-parser.di_config" entry pointing to a DI config file relative to the package root.
Parameters
- $installedJsonPath : string
Return values
array<string|int, string> —Absolute paths to discovered config files
resetGlobalPhpDiContainerDefinitions()
public
static resetGlobalPhpDiContainerDefinitions() : void
Return values
void —setFallbackCharset()
Sets the fallback charset used for text/* content parts that don't declare a charset. Defaults to 'ISO-8859-1' per RFC 2045.
public
static setFallbackCharset(string $charset) : void
Many modern messages omit the charset and are actually UTF-8, so you may want to set this to 'UTF-8'.
Parameters
- $charset : string
Return values
void —setGlobalLogger()
Registers the provided logger globally.
public
static setGlobalLogger(LoggerInterface $logger) : void
Parameters
- $logger : LoggerInterface
Return values
void —setGlobalPhpDiConfigurations()
Sets global configuration for php-di. Overrides all previously set definitions. You can optionally not use the default MMP definitions file by passing 'false' to the $useDefaultDefinitionsFile argument.
public
static setGlobalPhpDiConfigurations(array<string|int, array|string|\DI\Definition\Source\DefinitionSource> $phpDiConfigs[, bool $useDefaultDefinitionsFile = true ]) : void
Parameters
-
$phpDiConfigs
: array<string|int, array
|string|\DI\Definition\Source\DefinitionSource> -
array of definitions
- $useDefaultDefinitionsFile : bool = true
Return values
void —discoverPluginConfigs()
Discovers plugin DI config files from installed Composer packages.
private
static discoverPluginConfigs() : array<string|int, string>
Locates vendor/composer/installed.json via the Composer ClassLoader and delegates to parsePluginConfigs() for the actual parsing.
Return values
array<string|int, string> —Absolute paths to discovered config files
getGlobalContainerBuilder()
Returns the default ContainerBuilder with default loaded definitions.
private
static getGlobalContainerBuilder() : ContainerBuilder<string|int, Container>