SMTP
SMTP
SMTP mail transport implementation for sending email via an SMTP server.
class SMTP extends \Hazaar\Mail\TransportExtends: Hazaar\Mail\Transport
Handles 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 $serverType: string
port
The port number to connect to on the SMTP server.
private int $portType: int
socket
The socket resource used for the SMTP connection.
private mixed $socketType: mixed
readTimeout
Timeout in seconds for reading from the SMTP server.
private int $readTimeoutType: int
Methods
init
Initializes the SMTP transport with server, port, and timeout settings.
public init(array $settings): boolReturns: bool
Merges 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): mixedReturns: mixed
Handles connection, authentication, TLS negotiation, DSN, and message delivery. Throws exceptions on protocol or server errors.
Parameters
| Parameter | Type | Description |
|---|---|---|
$message | Hazaar\Mail\TransportMessage | the message to send via SMTP transport |
connect
Establishes a socket connection to the SMTP server.
private connect(): boolReturns: bool
Opens 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(): boolReturns: bool
Safely 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): false|intReturns: false|int
Sends 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, false|string $response): boolReturns: bool
Waits 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 | false | string |
getMessageCode
Extracts the SMTP status code from a response buffer.
private getMessageCode(string $buf, ?string $message): bool|intReturns: bool|int
Parses 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): stringReturns: string
Formats 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 Fri, 04 Sep 2026 23:32:17 +0000