Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ddrv/php-mailer

PHP library for sending email
https://github.com/ddrv/php-mailer

email email-attachment email-php email-sender email-templates mailer php php-smtp

Last synced: about 2 months ago
JSON representation

PHP library for sending email

Awesome Lists containing this project

README

        

[![Latest Stable Version](https://img.shields.io/packagist/v/ddrv/mailer.svg?style=flat-square)](https://packagist.org/packages/ddrv/mailer)
[![Total Downloads](https://img.shields.io/packagist/dt/ddrv/mailer.svg?style=flat-square)](https://packagist.org/packages/ddrv/mailer/stats)
[![License](https://img.shields.io/packagist/l/ddrv/mailer.svg?style=flat-square)](https://github.com/ddrv/php-mailer/blob/master/LICENSE)
[![PHP](https://img.shields.io/packagist/php-v/ddrv/mailer.svg?style=flat-square)](https://php.net)

# Mailer

PHP library for sending email.

# Install

1. Run in console:
```bash
composer require ddrv/mailer:^5
```

1. Include autoload file
```php
require_once('vendor/autoload.php');
```

# Usage

## Creating transport instance

### Sendmail

```php
HTML', // HTML body
'Simple text.' // Text plain body
);
```

## Sending message

```php
send($message);
```

## Message methods

```php
setSubject('Welcome to Fight Club!');
$message->setText('Welcome to Fight Club!' . PHP_EOL . 'Please, read our rules in attachments.');
$html = '

Welcome to Fight Club!

Please, read our rules in attachments.

';
$html .= 'PosterYour ticket';
$message->setHtml($html);

$message->setSender('[email protected]', 'Support od Fight Club'); // The SMTP transport will set its value.

$message->addRecipient('[email protected]', 'Tyler Durden', Ddrv\Mailer\Message::RECIPIENT_TO);
$message->addRecipient('[email protected]', 'Robert Paulson', Ddrv\Mailer\Message::RECIPIENT_CC);
$message->addRecipient('[email protected]', 'Angel Face', Ddrv\Mailer\Message::RECIPIENT_BCC);
$message->addRecipient('[email protected]', 'Richard Chesler', Ddrv\Mailer\Message::RECIPIENT_TO);

$message->getRecipientName('[email protected]'); // Returns 'Robert Paulson'.
$message->getRecipientName('[email protected]'); // Returns null.

$message->removeRecipient('[email protected]'); // If you change your mind.

// You may remove recipients by type
$message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_TO);
$message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_CC);
$message->removeRecipients(Ddrv\Mailer\Message::RECIPIENT_BCC);
// Or all
$message->removeRecipients();

$message->addRecipient('[email protected]', 'Tyler Durden', Ddrv\Mailer\Message::RECIPIENT_TO);
$message->addRecipient('[email protected]', 'Robert Paulson', Ddrv\Mailer\Message::RECIPIENT_CC);
$message->addRecipient('[email protected]', 'Angel Face', Ddrv\Mailer\Message::RECIPIENT_BCC);

$rules = <<attachFromString(
'rules.txt', // Attachment name (REQUIRED)
$rules, // Contents (REQUIRED)
'text/plain; charset=UTF-8' // Mime Type
);

$path = '/home/tyler/docs/projects/mayhem/rules.txt';

$message->attachFromFile(
'project-mayhem.txt', // Attachment name (REQUIRED)
$path, // Path to attached file (REQUIRED)
'text/plain; charset=UTF-8' // Mime Type
);

$message->detach('project-mayhem.txt'); // If you change your mind.

$message->setHtmlContentFromString(
'ticket', // HTML Content ID
file_get_contents('/home/tyler/tickets/038994.jpg'), // Contents (REQUIRED)
'image/jpeg' // Mime Type
);

$message->setHtmlContentFromString(
'script', // HTML Content ID
'alert("ok");', // Contents (REQUIRED)
'application/javascript' // Mime Type
);

$message->setHtmlContentFromFile(
'poster', // HTML Content ID
'/home/tyler/images/fight-club.jpg', // Path to file (REQUIRED)
'image/jpeg' // Mime Type
);

$message->unsetBodyHtmlContent('script'); // If you change your mind.

$message->hasHeader('X-Some-Header'); // Returns false.
$message->setHeader('X-Some-Header', 'Value'); // Header set.
$message->hasHeader('X-Some-Header'); // Returns true.
$message->getHeader('X-Some-Header'); // Returns 'Value'.
$message->removeHeader('X-Some-Header'); // Header removed.
$message->hasHeader('X-Some-Header'); // Returns false.

$message->getRecipients(); // Returns array if recipients emails.
$message->getSubject(); // Returns mail subject.
$message->getHeadersRaw(); // Returns string of mail headers.
$message->getBodyRaw(); // Returns string of mail body.

```

You can implement Ddrv\Mailer\Contract\Message interface for work with custom messages.

## Spool

This package allows you to use mail spool.

This requires:
* create a spool object (instance of `Ddrv\Mailer\Contract\Spool`)
* create a transport object (instance of `Ddrv\Mailer\Contract\Transport`)
* wrap them up in special transport `Ddrv\Mailer\Transport\SpoolTransport`

```php
send($message1);
$mailer->send($message2);

// Now the letters will only be added to the queue.
// To send them, you need to execute:

$wrapper->flush(
100, // Number of emails sent.
5 // Number of attempts to send emails.
);
```

## Personal mailing

If you have a copy of a message with many recipients, but you want to send separate emails to each recipient (no copies)

```php
personal($message);
```

## Transport factory

If you use native library transport, you can use `Ddrv\Mailer\TransportFactory`.

```php