NAMESPACE SoftMaji\PingMail

Class PingMail

The core driver for the PingMail library. This class handles the creation of a raw TCP socket connection to an SMTP server, manages the authentication handshake (AUTH LOGIN), and delivers the email payload.

__construct Public

Initializes the SMTP client configuration. This method does not open the connection immediately; connection occurs when send() is called.

public function __construct(string $host, int $port, string $user, string $pass, string $secure = 'tls')
Type Parameter Description
string $host The hostname of the SMTP server (e.g., smtp.gmail.com).
int $port The connection port. Usually 587 (TLS) or 465 (SSL).
string $user The SMTP username or email address for authentication.
string $pass The SMTP password or App Password.
string $secure (Optional) Encryption method. Accepts 'tls', 'ssl', or 'none'. Default is 'tls'.

send Public

Establishes the socket connection, performs the handshake, authenticates, and transmits the message data.

public function send(string $to, string $from, string $subject, string $body) : bool
Type Parameter Description
string $to The recipient's email address.
string $from The sender's email address (Must match $user on some servers like Gmail).
string $subject The email subject line.
string $body The HTML content of the email.

Return Values

Returns true if the server accepts the email for delivery (250 OK).

Throws

  • \Exception - If the connection fails (fsockopen error).
  • \Exception - If authentication fails (Wrong password/username).
  • \Exception - If TLS handshake fails (Server does not support encryption).

getDebugLog Public

Retrieves the array of raw communication logs between the Client and the Server. Useful for debugging specific SMTP error codes.

public function getDebugLog() : array

Example Output

Array
(
    [0] => SERVER: 220 smtp.gmail.com ESMTP
    [1] => CLIENT: EHLO localhost
    [2] => SERVER: 250-smtp.gmail.com at your service
    [3] => SERVER: 250-SIZE 35882577
    [4] => SERVER: 250-STARTTLS
    [5] => CLIENT: STARTTLS
)
                

Internal Methods

These methods are private and handled automatically by the class. They are documented here for contributors.

private function cmd($command, $expected_code)

Sends a command string to the socket stream and validates the response code matches expectations.

private function readResponse()

Reads the socket stream. Handles multi-line SMTP responses (e.g., 250-PIPELINING) by checking for hyphens in the 4th character position.