SMTP
SMTP
SMTP mail transport implementation for sending email via an SMTP server.
class SMTP extends \Hazaar\Mail\Transport\TransportHandles connection, authentication, TLS negotiation, and message delivery using SMTP protocol. Supports DSN, multiple authentication methods, and header encoding.
Properties
server
The SMTP server hostname or IP address.
private string $serverport
The port number to connect to on the SMTP server.
private int $portsocket
The socket resource used for the SMTP connection.
private mixed $socketreadTimeout
Timeout in seconds for reading from the SMTP server.
private int $readTimeoutMethods
init
Initializes the SMTP transport with server, port, and timeout settings.
public init(array $settings): boolMerges provided settings with defaults, validates required parameters, and sets up connection properties for SMTP communication.
Parameters
| Parameter | Type | Description |
|---|---|---|
$settings | array | SMTP configuration options. Must include 'server'. |
send
Sends an email message using the SMTP protocol.
public send(TransportMessage $message): mixedHandles connection, authentication, TLS negotiation, DSN, and message delivery. Throws exceptions on protocol or server errors.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | TransportMessage | the message to send via SMTP transport |
connect
Establishes a socket connection to the SMTP server.
private connect(): boolOpens a stream socket to the configured server and port, with the specified timeout. Throws an exception if connection fails.
close
Closes the SMTP socket connection.
private close(): boolSafely closes the socket resource and resets the property. Returns true if closed, false if not connected.
write
Writes a message to the SMTP socket.
private write(string $msg): intSends a command or data to the SMTP server over the open socket connection.
Parameters
| Parameter | Type | Description |
|---|---|---|
$msg | string | the message to send to the server |
read
Reads a response from the SMTP server and checks for expected status code.
private read(int $code, int $len = 1024, ?string $message, string $response): boolWaits for a response, reads data from the socket, and validates the SMTP status code. Throws on timeout or unexpected code.
Parameters
| Parameter | Type | Description |
|---|---|---|
$code | int | expected SMTP status code |
$len | int | maximum length of response to read |
$message | string | output parameter for the response message |
$response | string |
getMessageCode
Extracts the SMTP status code from a response buffer.
private getMessageCode(string $buf, ?string $message): intParses the response string and returns the status code as integer. Sets the message output parameter.
Parameters
| Parameter | Type | Description |
|---|---|---|
$buf | string | the response buffer from the server |
$message | string | output parameter for the response message |
encodeEmail
Encodes an email address for use in SMTP commands.
private encodeEmail(array $email): stringFormats the email address and name for SMTP headers and commands.
Parameters
| Parameter | Type | Description |
|---|---|---|
$email | array | array with 'email' and optional 'name' keys |
Generated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:23 +0000