Skip to content

shklyarik/psa-mailer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

psa-mailer

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.

Installation

Install the package via Composer:

composer require shklyarik/psa-mailer

Usage

Basic Usage

To 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.";
}

Advanced Usage

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();

Available Methods

The MailBuilder class provides the following methods:

from(string $address, string $name = '')

Set the sender of the email.

to(string $address, string $name = '')

Add a recipient to the email.

cc(string $address)

Add a CC recipient to the email.

bcc(string $address)

Add a BCC recipient to the email.

subject(string $subject)

Set the subject of the email.

body(string $html, ?string $alt = null)

Set the body of the email. The $alt parameter is optional and provides plain text alternative content.

attach(string $filePath, ?string $name = null)

Attach a file to the email. The $name parameter is optional and allows you to specify the name of the attached file.

replyTo(string $address, string $name = '')

Add a reply-to address to the email.

returnPath(string $address)

Set the return path (bounce) address for the email.

send(): bool

Send the email. Returns true if successful, false otherwise.

Requirements

  • PHP 8.0 or higher
  • PHPMailer ^7.0.0 (automatically installed with the package)

License

This package is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages