SmtpTransport
extends AbstractTransport
in package
Send mail using SMTP protocol
Table of Contents
Constants
- AUTH_LOGIN = 'LOGIN'
- AUTH_PLAIN = 'PLAIN'
- AUTH_XOAUTH2 = 'XOAUTH2'
- SUPPORTED_AUTH_TYPES = [self::AUTH_PLAIN, self::AUTH_LOGIN, self::AUTH_XOAUTH2]
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_content : array<string, string>
- Content of email to return
- $_defaultConfig : array<string, mixed>
- Default config for this class
- $_lastResponse : array<string|int, mixed>
- The response of the last sent SMTP command.
- $_socket : Socket|null
- Socket to SMTP server
- $authType : string|null
- Authentication type.
Methods
- __construct() : mixed
- Constructor
- __destruct() : mixed
- Destructor
- __wakeup() : void
- Unserialize handler.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- connect() : void
- Connect to the SMTP server.
- connected() : bool
- Check whether an open connection to the SMTP server is available.
- disconnect() : void
- Disconnect from the SMTP server.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- getLastResponse() : array<string|int, mixed>
- Returns the response of the last sent SMTP command.
- send() : array{headers: string, message: string}
- Send mail
- setConfig() : $this
- Sets the config.
- _auth() : void
- Send authentication
- _authLogin() : void
- Authenticate using AUTH LOGIN mechanism.
- _authPlain() : string|null
- Authenticate using AUTH PLAIN mechanism.
- _authXoauth2() : void
- Authenticate using AUTH XOAUTH2 mechanism.
- _bufferResponseLines() : void
- Parses and stores the response lines in `'code' => 'message'` format.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _connect() : void
- Connect to SMTP Server
- _disconnect() : void
- Disconnect
- _generateSocket() : void
- Helper method to generate socket
- _parseAuthType() : void
- Parses the last response line and extract the preferred authentication type.
- _prepareFromAddress() : array<string|int, mixed>
- Prepares the `from` email address.
- _prepareFromCmd() : string
- Prepares the `MAIL FROM` SMTP command.
- _prepareMessage() : string
- Prepares the message body.
- _prepareRcptCmd() : string
- Prepares the `RCPT TO` SMTP command.
- _prepareRecipientAddresses() : array<string|int, mixed>
- Prepares the recipient email addresses.
- _sendData() : void
- Send Data
- _sendRcpt() : void
- Send emails
- _smtpSend() : string|null
- Protected method for sending data to SMTP connection
- _socket() : Socket
- Get socket instance.
- checkRecipient() : void
- Check that at least one destination header is set.
Constants
AUTH_LOGIN
public
mixed
AUTH_LOGIN
= 'LOGIN'
AUTH_PLAIN
public
mixed
AUTH_PLAIN
= 'PLAIN'
AUTH_XOAUTH2
public
mixed
AUTH_XOAUTH2
= 'XOAUTH2'
SUPPORTED_AUTH_TYPES
public
mixed
SUPPORTED_AUTH_TYPES
= [self::AUTH_PLAIN, self::AUTH_LOGIN, self::AUTH_XOAUTH2]
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_content
Content of email to return
protected
array<string, string>
$_content
= []
$_defaultConfig
Default config for this class
protected
array<string, mixed>
$_defaultConfig
= ['host' => 'localhost', 'port' => 25, 'timeout' => 30, 'username' => null, 'password' => null, 'client' => null, 'tls' => false, 'keepAlive' => false, 'authType' => null]
$_lastResponse
The response of the last sent SMTP command.
protected
array<string|int, mixed>
$_lastResponse
= []
$_socket
Socket to SMTP server
protected
Socket|null
$_socket
$authType
Authentication type.
protected
string|null
$authType
= null
Methods
__construct()
Constructor
public
__construct([array<string, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string, mixed> = []
-
Configuration options.
__destruct()
Destructor
public
__destruct() : mixed
Tries to disconnect to ensure that the connection is being terminated properly before the socket gets closed.
__wakeup()
Unserialize handler.
public
__wakeup() : void
Ensure that the socket property isn't reinitialized in a broken state.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisconnect()
Connect to the SMTP server.
public
connect() : void
This method tries to connect only in case there is no open connection available already.
connected()
Check whether an open connection to the SMTP server is available.
public
connected() : bool
Return values
booldisconnect()
Disconnect from the SMTP server.
public
disconnect() : void
This method tries to disconnect only in case there is an open connection available.
getConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
getLastResponse()
Returns the response of the last sent SMTP command.
public
getLastResponse() : array<string|int, mixed>
A response consists of one or more lines containing a response code and an optional response message text:
[
[
'code' => '250',
'message' => 'mail.example.com'
],
[
'code' => '250',
'message' => 'PIPELINING'
],
[
'code' => '250',
'message' => '8BITMIME'
],
// etc...
]
Return values
array<string|int, mixed>send()
Send mail
public
send(Message $message) : array{headers: string, message: string}
Parameters
- $message : Message
-
Message instance
Tags
Return values
array{headers: string, message: string}setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$this_auth()
Send authentication
protected
_auth() : void
Tags
_authLogin()
Authenticate using AUTH LOGIN mechanism.
protected
_authLogin(string $username, string $password) : void
Parameters
- $username : string
-
Username.
- $password : string
-
Password.
_authPlain()
Authenticate using AUTH PLAIN mechanism.
protected
_authPlain(string $username, string $password) : string|null
Parameters
- $username : string
-
Username.
- $password : string
-
Password.
Return values
string|null —Response code for the command.
_authXoauth2()
Authenticate using AUTH XOAUTH2 mechanism.
protected
_authXoauth2(string $username, string $token) : void
Parameters
- $username : string
-
Username.
- $token : string
-
Token.
Tags
_bufferResponseLines()
Parses and stores the response lines in `'code' => 'message'` format.
protected
_bufferResponseLines(array<string|int, string> $responseLines) : void
Parameters
- $responseLines : array<string|int, string>
-
Response lines to parse.
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_connect()
Connect to SMTP Server
protected
_connect() : void
Tags
_disconnect()
Disconnect
protected
_disconnect() : void
Tags
_generateSocket()
Helper method to generate socket
protected
_generateSocket() : void
Tags
_parseAuthType()
Parses the last response line and extract the preferred authentication type.
protected
_parseAuthType() : void
_prepareFromAddress()
Prepares the `from` email address.
protected
_prepareFromAddress(Message $message) : array<string|int, mixed>
Parameters
- $message : Message
-
Message instance
Return values
array<string|int, mixed>_prepareFromCmd()
Prepares the `MAIL FROM` SMTP command.
protected
_prepareFromCmd(string $message) : string
Parameters
- $message : string
-
The email address to send with the command.
Return values
string_prepareMessage()
Prepares the message body.
protected
_prepareMessage(Message $message) : string
Parameters
- $message : Message
-
Message instance
Return values
string_prepareRcptCmd()
Prepares the `RCPT TO` SMTP command.
protected
_prepareRcptCmd(string $message) : string
Parameters
- $message : string
-
The email address to send with the command.
Return values
string_prepareRecipientAddresses()
Prepares the recipient email addresses.
protected
_prepareRecipientAddresses(Message $message) : array<string|int, mixed>
Parameters
- $message : Message
-
Message instance
Return values
array<string|int, mixed>_sendData()
Send Data
protected
_sendData(Message $message) : void
Parameters
- $message : Message
-
Message instance
Tags
_sendRcpt()
Send emails
protected
_sendRcpt(Message $message) : void
Parameters
- $message : Message
-
Message instance
Tags
_smtpSend()
Protected method for sending data to SMTP connection
protected
_smtpSend(string|null $data[, string|false $checkCode = '250' ]) : string|null
Parameters
- $data : string|null
-
Data to be sent to SMTP server
- $checkCode : string|false = '250'
-
Code to check for in server response, false to skip
Tags
Return values
string|null —The matched code, or null if nothing matched
_socket()
Get socket instance.
protected
_socket() : Socket
Tags
Return values
SocketcheckRecipient()
Check that at least one destination header is set.
protected
checkRecipient(Message $message) : void
Parameters
- $message : Message
-
Message instance.