psa-mailer is a simple and fluent email sending library built on top of PHPMailer. It provides an easy-to-use interface for sending emails with a fluent API that allows for method chaining.
Install the package via Composer:
composer require shklyarik/psa-mailerTo send an email, first create a Mailer instance with your SMTP configuration and then use the MailBuilder to build and send your email:
<?php
use Psa\Mailer\Mailer;
// Create a mailer instance with SMTP configuration
$mailer = new Mailer(
host: 'smtp.example.com',
username: 'your-username',
password: 'your-password',
port: 587
);
// Build and send an email using the fluent interface
$result = $mailer->build()
->from('sender@example.com', 'Sender Name')
->to('recipient@example.com', 'Recipient Name')
->subject('Test Email')
->body('<h1>Hello World!</h1><p>This is a test email.</p>')
->send();
if ($result) {
echo "Email sent successfully!";
} else {
echo "Failed to send email.";
}You can chain multiple methods to configure your email:
<?php
use Psa\Mailer\Mailer;
$mailer = new Mailer(
host: 'smtp.gmail.com',
username: 'your-email@gmail.com',
password: 'your-app-password',
port: 587
);
$result = $mailer->build()
->from('sender@example.com', 'Sender Name')
->to('primary@example.com', 'Primary Recipient')
->cc('cc@example.com')
->bcc('bcc@example.com')
->replyTo('reply@example.com', 'Reply To Name')
->subject('Important Message')
->body(
'<h1>HTML Content</h1><p>This email has HTML content.</p>',
'Plain text alternative content'
)
->attach('/path/to/file.pdf', 'document.pdf')
->returnPath('bounce@example.com')
->send();The MailBuilder class provides the following methods:
Set the sender of the email.
Add a recipient to the email.
Add a CC recipient to the email.
Add a BCC recipient to the email.
Set the subject of the email.
Set the body of the email. The $alt parameter is optional and provides plain text alternative content.
Attach a file to the email. The $name parameter is optional and allows you to specify the name of the attached file.
Add a reply-to address to the email.
Set the return path (bounce) address for the email.
Send the email. Returns true if successful, false otherwise.
- PHP 8.0 or higher
- PHPMailer ^7.0.0 (automatically installed with the package)
This package is licensed under the MIT License.