Smart mailer is a simple library which assists sending of emails with use of symfony/mailer component. At the moment it requires SMTP connection, but grants features like easy embedding of images, easier attachments adding, exceptions tracking and use of string templates instead of files.
- Twig Template Support: Use Twig syntax in both HTML and text email content
- Easy File Attachments: Simple attachment management with validation
- Embedded Images: Support for inline images with automatic MIME type detection
- Multiple Recipients: Support for To, CC, and BCC recipients
- DSN Configuration: Flexible transport configuration via DSN strings
- Gmail Integration: Built-in Gmail transport support
- Testing Support: File-based mailer for development and testing
- Exception Handling: Comprehensive exception hierarchy for error handling
<?php
declare(strict_types=1);
use GryfOSS\Mailer\Attachment;
use GryfOSS\Mailer\EmailAddress;
use GryfOSS\Mailer\Message;
use GryfOSS\Mailer\SmartMailer;
use GryfOSS\Mailer\Dsn\Smtp;
include 'vendor/autoload.php';
// Configure SMTP transport
$dsn = new Smtp();
$dsn->setHost('smtp.sample.com')
->setPort(465)
->setUsername('username')
->setPassword('password');
// Create message
$message = new Message();
$message->setFrom(new EmailAddress('sender@example.com', 'Sender Name'))
->addTo(new EmailAddress('recipient@example.com', 'Recipient Name'))
->setSubject('Test Email')
->setHtml('<h1>Hello {{ name }}!</h1><img src="cid:logo.png" alt="Logo"/>')
->setText('Hello {{ name }}!')
->setContext(['name' => 'World']);
// Add attachments and images
$attachment = new Attachment('/path/to/file.pdf', 'Document.pdf');
$message->addAttachment($attachment);
$logo = new Attachment('/path/to/logo.png', 'logo.png');
$message->addImage($logo);
// Send email
$mailer = new SmartMailer($dsn);
$mailer->send($message);Main email sending implementation with Twig template support. Integrates with Symfony Mailer for actual email delivery.
File-based implementation for testing and development. Writes email data to JSON files instead of sending actual emails.
Represents an email message with all its components including recipients, content, attachments, and embedded images.
Encapsulates an email address with optional display name and validation.
Represents a file attachment with path validation and optional custom naming.
Interface for creating DSN strings compatible with Symfony Mailer.
SMTP server DSN implementation with full configuration options.
Simplified Gmail DSN implementation using Symfony's Gmail transport.
Defines available SMTP encryption methods (SSL/TLS, STARTTLS, none).
Defines available SMTP authentication methods (LOGIN, PLAIN, CRAM-MD5, etc.).
Base exception class for all library-specific errors.
Thrown when email address format validation fails.
Thrown when attachment file doesn't exist or isn't readable.
Thrown when message validation fails (missing required fields).
Thrown when attempting to embed a non-image file.
Thrown when trying to add an embedded image with a duplicate name.
Thrown when email sending fails.
use GryfOSS\Mailer\Dsn\Gmail;
$gmail = new Gmail();
$gmail->setUsername('your-email@gmail.com')
->setPassword('your-app-password');
$mailer = new SmartMailer($gmail);You can easily embed any image by using content id of the resource. If your
filename is sample.png then to embed just place cid:sample.png in contents.
You can also define a custom name:
$image = new Attachment('/path/to/image.png');
$image->setName('custom-name');
$message->addImage($image);
// In HTML: <img src="cid:custom-name" alt="Custom Image"/>Warning: Embedded image names must be unique within a message!
Both HTML and text content support Twig templating:
$message->setHtml('
<h1>Welcome {{ user.name }}!</h1>
<p>You have {{ notifications|length }} new notifications.</p>
{% for notification in notifications %}
<p>{{ notification.message }}</p>
{% endfor %}
')
->setContext([
'user' => ['name' => 'John Doe'],
'notifications' => [
['message' => 'New message received'],
['message' => 'Profile updated']
]
]);use GryfOSS\Mailer\FakeFileSmartMailer;
$mailer = new FakeFileSmartMailer('/tmp/test-email.json');
$mailer->send($message);
// Email data will be written to /tmp/test-email.json as JSONuse GryfOSS\Mailer\Exception\SmartMailerException;
try {
$mailer->send($message);
} catch (SmartMailerException $e) {
// Handle any SmartMailer-specific error
echo "Email error: " . $e->getMessage();
}- PHP 8.1+
- Symfony Mailer 6.0+ or 7.0+
- Twig 3.0+
composer require gryfoss/twig-easy-mailer1.0: tests, comments ✅ 2.0: other transports than SMTP Servers 3.0: Pub/Sub queues support
Any pull requests or issues reported are more than welcome.
GPL 3+