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
Every To, Cc and Bcc address is given to the server as an envelope recipient, but only To and Cc appear in the message headers.
STARTTLS is used whenever the server offers it, unless starttls is false. With starttls set to true the send fails if the server doesn't offer it. Credentials are never sent over an unencrypted connection unless starttls is explicitly false.
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 |
parseModules
Reads the extension keywords (STARTTLS, AUTH ..., DSN) from an EHLO response.
private parseModules(string $response): arrayReturns: array
Parameters
| Parameter | Type | Description |
|---|---|---|
$response | 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 |
Generated by Hazaar API Doc Generator on Sun, 08 Nov 2026 03:36:15 +0000