MailManager
class MailManager extends DefaultPluginManager implements MailManagerInterface (View source)
Provides a Mail plugin manager.
Traits
Provides a trait for the messenger service.
Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
Provides methods to use a cache backend while respecting a 'use caches' flag.
Properties
| protected DiscoveryInterface | $discovery | The object that discovers plugins managed by this manager. |
from PluginManagerBase |
| protected FactoryInterface | $factory | The object that instantiates plugins managed by this manager. |
from PluginManagerBase |
| protected MapperInterface|null | $mapper | The object that returns the preconfigured plugin instance appropriate for a particular runtime condition. |
from PluginManagerBase |
| protected array | $definitions | Cached definitions array. |
from DiscoveryCachedTrait |
| protected CacheBackendInterface | $cacheBackend | Cache backend instance. |
from UseCacheBackendTrait |
| protected bool | $useCaches | Flag whether caches should be used or skipped. |
from UseCacheBackendTrait |
| protected string | $cacheKey | The cache key. |
from DefaultPluginManager |
| protected array | $cacheTags | An array of cache tags to use for the cached definitions. |
from DefaultPluginManager |
| protected string | $alterHook | Name of the alter hook if one should be invoked. |
from DefaultPluginManager |
| protected string|bool | $subdir | The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace. |
from DefaultPluginManager |
| protected ModuleHandlerInterface | $moduleHandler | The module handler to invoke the alter hook. |
from DefaultPluginManager |
| protected array | $defaults | A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. |
from DefaultPluginManager |
| protected string | $pluginDefinitionAnnotationName | The name of the annotation that contains the plugin definition. |
from DefaultPluginManager |
| protected string|null | $pluginInterface | The interface each plugin should implement. |
from DefaultPluginManager |
| protected Traversable | $namespaces | An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations. |
from DefaultPluginManager |
| protected string[] | $additionalAnnotationNamespaces | Additional namespaces the annotation discovery mechanism should scan for annotation definitions. |
from DefaultPluginManager |
| protected MessengerInterface | $messenger | The messenger. |
from MessengerTrait |
| protected TranslationInterface | $stringTranslation | The string translation service. |
from StringTranslationTrait |
| protected ConfigFactoryInterface | $configFactory | The config factory. |
|
| protected LoggerChannelFactoryInterface | $loggerFactory | The logger factory. |
|
| protected RendererInterface | $renderer | The renderer. |
|
| protected array | $instances | List of already instantiated mail plugins. |
Methods
Gets a specific plugin definition.
Creates a pre-configured instance of a plugin.
Allows plugin managers to specify custom behavior if a plugin is not found.
Overrides PluginManagerBase::getInstance().
Fetches from the cache backend, respecting the use caches flag.
Stores data in the persistent cache, respecting the use caches flag.
Constructs the MailManager object.
Initialize the cache backend.
Clears static and persistent plugin definition caches.
Returns the cached plugin definitions of the decorated discovery class.
Sets a cache of plugin definitions for the decorated discovery class.
Performs extra processing on plugin definitions.
Extracts the provider from a plugin definition.
Invokes the hook to alter the definitions if the alter hook is set.
Determines if the provider of a definition exists.
The cache contexts associated with this object.
Translates a string to the current language or to a given language.
Formats a string containing a count of items.
Returns the number of plurals supported by a given language.
Gets the string translation service.
Composes and optionally sends an email message.
Composes and optionally sends an email message.
Details
abstract
getDefinitions()
{@inheritdoc}
getDefinition($plugin_id, $exception_on_invalid = TRUE)
{@inheritdoc}
protected array|null
doGetDefinition(array $definitions, string $plugin_id, bool $exception_on_invalid)
Gets a specific plugin definition.
hasDefinition($plugin_id)
{@inheritdoc}
protected DiscoveryInterface
getDiscovery()
Gets the plugin discovery.
protected FactoryInterface
getFactory()
Gets the plugin factory.
object
createInstance(string $plugin_id, array $configuration = [])
Creates a pre-configured instance of a plugin.
protected object
handlePluginNotFound(string $plugin_id, array $configuration)
Allows plugin managers to specify custom behavior if a plugin is not found.
object|false
getInstance(array $options)
Overrides PluginManagerBase::getInstance().
Returns an instance of the mail plugin to use for a given message ID.
The selection of a particular implementation is controlled via the config 'system.mail.interface', which is a keyed array. The default implementation is the mail plugin whose ID is the value of 'default' key. A more specific match first to key and then to module will be used in preference to the default. To specify a different plugin for all mail sent by one module, set the plugin ID as the value for the key corresponding to the module name. To specify a plugin for a particular message sent by one module, set the plugin ID as the value for the array key that is the message ID, which is "${module}_${key}".
For example to debug all mail sent by the user module by logging it to a file, you might set the variable as something like:
protected object|false
cacheGet(string $cid)
Fetches from the cache backend, respecting the use caches flag.
protected
cacheSet(string $cid, mixed $data, int $expire = Cache::PERMANENT, array $tags = [])
Stores data in the persistent cache, respecting the use caches flag.
__construct(Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory, LoggerChannelFactoryInterface $logger_factory, TranslationInterface $string_translation, RendererInterface $renderer)
Constructs the MailManager object.
setCacheBackend(CacheBackendInterface $cache_backend, string $cache_key, array $cache_tags = [])
Initialize the cache backend.
Plugin definitions are cached using the provided cache backend.
protected
alterInfo(string $alter_hook)
Sets the alter hook name.
clearCachedDefinitions()
Clears static and persistent plugin definition caches.
Don't resort to calling \Drupal::cache()->delete() and friends to make Drupal detect new or updated plugin definitions. Always use this method on the appropriate plugin type's plugin manager!
protected array|null
getCachedDefinitions()
Returns the cached plugin definitions of the decorated discovery class.
protected
setCachedDefinitions(array $definitions)
Sets a cache of plugin definitions for the decorated discovery class.
useCaches(bool $use_caches = FALSE)
Disable the use of caches.
Can be used to ensure that uncached plugin definitions are returned, without invalidating all cached information.
This will also remove all local/static caches.
processDefinition($definition, $plugin_id)
Performs extra processing on plugin definitions.
By default we add defaults for the type to the definition. If a type has additional processing logic they can do that by replacing or extending the method.
protected array
findDefinitions()
Finds plugin definitions.
protected string|null
extractProviderFromDefinition(mixed $plugin_definition)
Extracts the provider from a plugin definition.
protected
alterDefinitions($definitions)
Invokes the hook to alter the definitions if the alter hook is set.
protected bool
providerExists($provider)
Determines if the provider of a definition exists.
string[]
getCacheContexts()
The cache contexts associated with this object.
These identify a specific variation/representation of the object.
Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.
string[]
getCacheTags()
The cache tags associated with this object.
When this object is modified, these cache tags will be invalidated.
int
getCacheMaxAge()
The maximum age for which this object may be cached.
setMessenger(MessengerInterface $messenger)
Sets the messenger.
MessengerInterface
messenger()
Gets the messenger.
protected TranslatableMarkup
t(string $string, array $args = [], array $options = [])
Translates a string to the current language or to a given language.
See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.
In order for strings to be localized, make them available in one of the ways supported by the
protected
formatPlural($count, $singular, $plural, array $args = [], array $options = [])
Formats a string containing a count of items.
protected
getNumberOfPlurals($langcode = NULL)
Returns the number of plurals supported by a given language.
protected TranslationInterface
getStringTranslation()
Gets the string translation service.
$this
setStringTranslation(TranslationInterface $translation)
Sets the string translation service to use.
array
mail(string $module, string $key, string $to, string $langcode, array $params = [], string|null $reply = NULL, bool $send = TRUE)
Composes and optionally sends an email message.
Sending an email works with defining an email template (subject, text and possibly email headers) and the replacement values to use in the appropriate places in the template. Processed email templates are requested from hook_mail() from the module sending the email. Any module can modify the composed email message array using hook_mail_alter(). Finally \Drupal::service('plugin.manager.mail')->mail() sends the email, which can be reused if the exact same composed email is to be sent to multiple recipients.
Finding out what language to send the email with needs some consideration. If you send email to a user, use \Drupal\Core\Session\AccountInterface::getPreferredAdminLangcode(). If you send email based on form values filled on the page, there are two additional choices if you are not sending the email to a user on the site. You can either use the language used to generate the page or the site default language. See Drupal\Core\Language\LanguageManagerInterface::getDefaultLanguage(). The former is good if sending email to the person filling the form, the later is good if you send email to an address previously set up (like contact addresses in a contact form).
Taking care of always using the proper language is even more important when sending emails in a row to multiple users. Hook_mail() abstracts whether the mail text comes from an administrator setting or is static in the source code. It should also deal with common mail tokens, only receiving $params which are unique to the actual email at hand.
An example:
array
doMail(string $module, string $key, string $to, string $langcode, array $params = [], string|null $reply = NULL, bool $send = TRUE)
Composes and optionally sends an email message.