Overview
TheSMTPMailer service provides a lightweight SMTP email client with TLS encryption support. It’s implemented as a singleton and handles direct SMTP communication for sending emails.
This is a singleton service. Always use
SMTPMailer::getInstance() to access it. Configuration is loaded from environment variables.Configuration
The SMTPMailer service requires the following environment variables:Getting Instance
getInstance()
Retrieves the singleton instance of SMTPMailer.Example
Behavior
- Loads configuration from environment variables or fallback config file
- Validates all required SMTP settings are present
- Throws
Exceptionif any configuration is missing - Returns singleton instance on success
Methods
sendEmail()
Sends an email via SMTP with HTML support.Sender’s email address (must be valid email format)
Recipient’s email address (must be valid email format)
Email subject line
Email body content (supports HTML)
Returns
true on successful sendExample
Email Flow
- Validates email address formats
- Establishes SMTP connection to configured host:port
- Upgrades connection to TLS encryption
- Authenticates with SMTP credentials
- Sends email with HTML content type
- Disconnects after sending
- Returns
trueon success - Throws
Exceptionon any failure
Content Type
Emails are sent with:- Content-Type:
text/html; charset=UTF-8 - Date header with RFC 2822 format
- From, To, and Subject headers
Connection Management
The service handles SMTP connections automatically:TLS Encryption
- Initial connection via TCP socket
- SMTP handshake with
HELO STARTTLScommand to upgrade connection- TLS encryption enabled via
stream_socket_enable_crypto() - Second
HELOafter TLS upgrade
Authentication
Uses SMTP AUTH LOGIN method:- Sends
AUTH LOGINcommand - Sends base64-encoded username
- Sends base64-encoded password
- Validates 235 response code for success
Automatic Cleanup
Connections are automatically closed:- After successful email send
- On exception/error
- On object destruction (
__destruct())
Error Handling
Exception Scenarios
The service throwsException in these cases:
Configuration Errors
Configuration Errors
getInstance() is called without required environment variables.Connection Errors
Connection Errors
TLS Errors
TLS Errors
SMTP Protocol Errors
SMTP Protocol Errors
Validation Errors
Validation Errors
Email Validation
All email addresses are validated using PHP’sFILTER_VALIDATE_EMAIL filter:
Usage Examples
Simple Text Email
HTML Email with Styling
With Error Handling
Technical Details
Connection Timeout
SMTP Commands Used
HELO- Initial handshakeSTARTTLS- Upgrade to TLSAUTH LOGIN- AuthenticationMAIL FROM- Sender addressRCPT TO- Recipient addressDATA- Begin message contentQUIT- Close connection
Response Codes Validated
220- Service ready / STARTTLS ready235- Authentication successful250- Command successful334- Authentication continue354- Start mail input221- Service closing
Security Considerations
TLS Encryption
All connections are upgraded to TLS before authentication and data transmission.
Secure Credentials
SMTP credentials should use app-specific passwords, not main account passwords.
Email Validation
All email addresses are validated before attempting to send.
Error Logging
Errors are logged via
error_log() for debugging without exposing details to users.Common SMTP Providers
Gmail
SendGrid
Mailgun
Source Reference
Location:~/workspace/source/Sphp/Services/SMTPMailer.php