#}
Email Integration
Developer Freedom
GEMVC does not include built-in email components. This is by design β we believe developers should have the freedom to choose the email library that best fits their project requirements.
Why No Built-in Email?
There are many excellent email libraries available in the PHP ecosystem, each with different strengths:
PHPMailer
Most popular PHP email library with SMTP support
composer require phpmailer/phpmailer
Symfony Mailer
Modern, flexible email component
composer require symfony/mailer
AWS SES SDK
For Amazon Simple Email Service
composer require aws/aws-sdk-php
SendGrid / Mailgun
Transactional email services
composer require sendgrid/sendgrid
Example: PHPMailer Integration
PHPMailer Service Example
<?php
// Install: composer require phpmailer/phpmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class EmailService
{
private PHPMailer $mail;
public function __construct()
{
$this->mail = new PHPMailer(true);
// SMTP Configuration
$this->mail->isSMTP();
$this->mail->Host = $_ENV['SMTP_HOST'];
$this->mail->SMTPAuth = true;
$this->mail->Username = $_ENV['SMTP_USER'];
$this->mail->Password = $_ENV['SMTP_PASS'];
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$this->mail->Port = $_ENV['SMTP_PORT'] ?? 587;
// Default sender
$this->mail->setFrom($_ENV['MAIL_FROM'], $_ENV['MAIL_FROM_NAME']);
}
public function send(string $to, string $subject, string $body): bool
{
try {
$this->mail->addAddress($to);
$this->mail->isHTML(true);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
$this->mail->send();
return true;
} catch (Exception $e) {
error_log("Email failed: {$this->mail->ErrorInfo}");
return false;
}
}
}
Environment Configuration
.env Configuration
# .env file
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
MAIL_FROM=noreply@yourdomain.com
MAIL_FROM_NAME="Your App Name"
Tip: For Gmail, use App Passwords instead of your regular password. Enable 2FA first, then generate an App Password.
Recommendations
- Small projects: PHPMailer β simple, well-documented
- Large scale: AWS SES, SendGrid, or Mailgun β reliable delivery
- Symfony integration: Symfony Mailer β modern, async support
- Queue emails: Store in database, process with cron or worker
info: Always store email credentials in .env file, never commit them to version control!