Pular para o conteúdo principal

Mail Wrapper

Sponsor Build Status Opensource ByJG GitHub source GitHub license GitHub release

A lightweight wrapper for sending email. The interface is totally decoupled from the sender, providing a single interface for sending mail regardless of the underlying mail service.

Available Wrappers

  • SMTP - SMTP with SSL/TLS support
  • AWS SES - Amazon Simple Email Service (using API directly)
  • Mailgun - Mailgun API (using API directly)
  • SendMail - PHP's built-in mail() function
  • FakeSender - For testing (does nothing)

Install

composer require "byjg/mailwrapper"

Documentation

Quick Start

<?php
require "vendor/autoload.php";

// Create the email envelope
$envelope = new \ByJG\Mail\Envelope();
$envelope->setFrom('[email protected]', 'John Doe');
$envelope->addTo('[email protected]');
$envelope->setSubject('Email Subject');
$envelope->setBody('<h1>Hello World</h1>');

// Register available mailers
\ByJG\Mail\MailerFactory::registerMailer(\ByJG\Mail\Wrapper\PHPMailerWrapper::class);
\ByJG\Mail\MailerFactory::registerMailer(\ByJG\Mail\Wrapper\MailgunApiWrapper::class);

// Create mailer from connection string
$mailer = \ByJG\Mail\MailerFactory::create('smtp://username:[email protected]:587');

// Send the email
$result = $mailer->send($envelope);

Architecture

MailWrapper is organized into three main components:

  • The Envelope: The mail message. Defines the sender, recipients, body, subject, attachments, etc.
  • The Mailer: Responsible for the process of sending the envelope
  • The Factory: Registers and creates the available Mailers in the system

Connection URL Schemes

SchemeDescriptionURI Pattern
smtpSMTP over insecure connectionsmtp://username:password@host:25
tlsSMTP over secure TLS connectiontls://username:password@host:587
sslSMTP over secure SSL connectionssl://username:password@host:465
sendmailPHP's built-in mail() functionsendmail://localhost
mailgunMailgun APImailgun://YOUR_API_KEY@YOUR_DOMAIN
sesAmazon SES APIses://ACCESS_KEY_ID:SECRET_KEY@REGION
fakesenderTesting (does nothing)fakesender://localhost

See Connection Strings for detailed configuration examples.

Running Tests

./vendor/bin/phpunit

Dependencies


Open source ByJG