Overview
S-PHP provides a robust email system with SMTP support, template rendering, and job queue integration. The framework includes two main services:SMTPMailer- Low-level SMTP clientSendMail- High-level email service with templates
Configuration
Environment Variables
Configure SMTP settings in your.env file:
Parameters
$to- Recipient email address$subject- Email subject line$templateName- Name of the template file (without .php extension)$data- Associative array of data to pass to the template
Email Templates
Create email templates in:Template Rendering
Templates:- Must be PHP files in
app/views/mail/ - Receive
$dataarray variables viaextract() - Support full HTML markup
- Should use
htmlspecialchars()for XSS protection
Using SMTPMailer Directly
For direct SMTP control without templates:Basic Usage
Features
- Singleton Pattern - Single instance for all email operations
- TLS Encryption - Automatic STARTTLS support
- Email Validation - Validates sender and recipient addresses
- Error Handling - Throws exceptions with detailed error messages
SMTP Connection Process
The SMTPMailer handles the full SMTP protocol:- Connect - Opens socket connection to SMTP server
- HELO - Sends initial greeting
- STARTTLS - Initiates TLS encryption
- Upgrade Connection - Enables crypto on the socket
- HELO - Sends greeting again after TLS
- AUTH LOGIN - Authenticates with username/password
- MAIL FROM - Specifies sender
- RCPT TO - Specifies recipient
- DATA - Sends email content
- QUIT - Closes connection
Email Format
Emails are sent with these headers:Job Queue Integration
TheSendMail service automatically:
- Creates a job for the email
- Adds it to the job queue
- Executes the job immediately
- Deferred email sending
- Better error handling
- Session message management
Error Handling
SMTPMailer Exceptions
The mailer throws exceptions for:Common Errors
- “SMTP configuration is incomplete” - Missing env variables
- “Invalid email address format” - Email validation failed
- “SMTP connection failed” - Cannot connect to server
- “Failed to start TLS encryption” - TLS negotiation failed
- “SMTP error - Expected 235” - Authentication failed
Email Validation
All email addresses are validated using PHP’sFILTER_VALIDATE_EMAIL:
Connection Management
Auto-Disconnect
Connections are automatically closed:- After each email is sent
- When the mailer instance is destroyed
- On errors (via finally block)
Timeout Settings
Default timeout is 30 seconds:Best Practices
- Use Templates - Prefer
SendMailover directSMTPMailerusage - Secure Credentials - Never hardcode SMTP passwords
- App Passwords - Use app-specific passwords for Gmail
- HTML Sanitization - Always escape user data in templates
- Error Logging - Log email errors for debugging
- Test Mode - Use a test SMTP server during development
Gmail Configuration
For Gmail SMTP:- Enable 2-Factor Authentication
- Go to Security > App passwords
- Generate a password for “Mail”
- Use that 16-character password in
.env
Example: Password Reset Email
app/views/mail/password-reset.php):