EmailService
in package
Service for sending emails.
Uses PHPMailer for SMTP email delivery. Configuration is loaded from environment variables. When email is not configured, tokens are logged instead of emailed (useful for development).
Tags
Table of Contents
Properties
- $config : array{enabled: bool, host: string, port: int, username: string, password: string, encryption: string, from_address: string, from_name: string}
Methods
- __construct() : mixed
- Create a new EmailService.
- isEnabled() : bool
- Check if email is enabled and configured.
- sendPasswordResetEmail() : bool
- Send a password reset email.
- sendVerificationEmail() : bool
- Send an email verification email.
- buildPasswordResetHtml() : string
- Build HTML email body.
- buildPasswordResetPlainText() : string
- Build plain text email body.
- buildResetUrl() : string
- Build the reset URL.
- buildVerificationHtml() : string
- Build HTML email body for verification.
- buildVerificationPlainText() : string
- Build plain text email body for verification.
- buildVerifyUrl() : string
- Build the email verification URL.
- loadConfigFromEnv() : array{enabled: bool, host: string, port: int, username: string, password: string, encryption: string, from_address: string, from_name: string}
- Load email configuration from environment variables.
- send() : bool
- Send an email via SMTP.
Properties
$config
private
array{enabled: bool, host: string, port: int, username: string, password: string, encryption: string, from_address: string, from_name: string}
$config
Methods
__construct()
Create a new EmailService.
public
__construct([array{enabled?: bool, host?: string, port?: int, username?: string, password?: string, encryption?: string, from_address?: string, from_name?: string}|null $config = null ]) : mixed
Parameters
- $config : array{enabled?: bool, host?: string, port?: int, username?: string, password?: string, encryption?: string, from_address?: string, from_name?: string}|null = null
-
Optional config array. If null, loads from environment.
isEnabled()
Check if email is enabled and configured.
public
isEnabled() : bool
Return values
boolsendPasswordResetEmail()
Send a password reset email.
public
sendPasswordResetEmail(string $email, string $username, string $token, DateTimeImmutable $expires) : bool
Parameters
- $email : string
-
Recipient email
- $username : string
-
Recipient username
- $token : string
-
Reset token (plaintext)
- $expires : DateTimeImmutable
-
Token expiration time
Tags
Return values
bool —True if sent successfully
sendVerificationEmail()
Send an email verification email.
public
sendVerificationEmail(string $email, string $username, string $token, DateTimeImmutable $expires) : bool
Parameters
- $email : string
-
Recipient email
- $username : string
-
Recipient username
- $token : string
-
Verification token (plaintext)
- $expires : DateTimeImmutable
-
Token expiration time
Return values
bool —True if sent successfully
buildPasswordResetHtml()
Build HTML email body.
private
buildPasswordResetHtml(string $username, string $resetUrl, string $expiry) : string
Parameters
- $username : string
-
Username for greeting
- $resetUrl : string
-
The password reset URL
- $expiry : string
-
Expiration time formatted
Return values
string —HTML email body
buildPasswordResetPlainText()
Build plain text email body.
private
buildPasswordResetPlainText(string $username, string $resetUrl, string $expiry) : string
Parameters
- $username : string
-
Username for greeting
- $resetUrl : string
-
The password reset URL
- $expiry : string
-
Expiration time formatted
Return values
string —Plain text email body
buildResetUrl()
Build the reset URL.
private
buildResetUrl(string $token) : string
Parameters
- $token : string
-
The reset token
Return values
stringbuildVerificationHtml()
Build HTML email body for verification.
private
buildVerificationHtml(string $username, string $verifyUrl, string $expiry) : string
Parameters
- $username : string
-
Username for greeting
- $verifyUrl : string
-
The verification URL
- $expiry : string
-
Expiration time formatted
Return values
string —HTML email body
buildVerificationPlainText()
Build plain text email body for verification.
private
buildVerificationPlainText(string $username, string $verifyUrl, string $expiry) : string
Parameters
- $username : string
-
Username for greeting
- $verifyUrl : string
-
The verification URL
- $expiry : string
-
Expiration time formatted
Return values
string —Plain text email body
buildVerifyUrl()
Build the email verification URL.
private
buildVerifyUrl(string $token) : string
Parameters
- $token : string
-
The verification token
Return values
stringloadConfigFromEnv()
Load email configuration from environment variables.
private
loadConfigFromEnv() : array{enabled: bool, host: string, port: int, username: string, password: string, encryption: string, from_address: string, from_name: string}
Return values
array{enabled: bool, host: string, port: int, username: string, password: string, encryption: string, from_address: string, from_name: string}send()
Send an email via SMTP.
private
send(string $to, string $subject, string $body, string $altBody) : bool
Parameters
- $to : string
-
Recipient email
- $subject : string
-
Email subject
- $body : string
-
HTML body
- $altBody : string
-
Plain text alternative
Tags
Return values
bool —True if sent successfully