2017 © Pedro Peláez
 

project mailer-client

Flash mailer client

image

fei/mailer-client

Flash mailer client

  • Friday, March 16, 2018
  • by rwellens
  • Repository
  • 7 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 24 Versions
  • 0 % Grown

The README.md

Mailer client

GitHub licensecontinuousphpGitHub issues, (*1)

This is the client you should use to send email to Mail Api., (*2)

The client can use two kind of transports to send emails:, (*3)

  • Asynchronous transport implemented by BeanstalkProxyTransport
  • Synchronous transport implemented by BasicTransport

BeanstalkProxyTransport delegate the API consumption to workers by sending email properties to a Beanstalkd queue., (*4)

BasicTransport use the classic HTTP layer to send emails., (*5)

If asynchronous transport is set, it will act as default transport. Synchronous transport will be a fallback in case when asynchronous transport fails., (*6)

Installation

Add this requirement to your composer.json: "fei/mailer-client": : "^1.0.0", (*7)

Or execute composer.phar require fei/mailer-client in your terminal., (*8)

Quick start

Let's start with a simple client :, (*9)

<?php

require __DIR__ . '/vendor/autoload.php';

use Fei\Service\Mailer\Client\Mailer;
use Fei\ApiClient\Transport\BasicTransport;
use Fei\Service\Mailer\Entity\Mail;

// The client
$mailer = new Mailer([
    Mailer::OPTION_BASEURL => 'https://api_host',
    Mailer::OPTION_HEADER_AUTHORIZATION => 'key'
]);
// Optional: a BasicTransport will be instantiate if no synchronous transport was found
$mailer->setTransport(new BasicTransport());

// The mail to send
$message = new Mail();
$message->setSubject('Test subject');
$message->setTextBody('This is a example message');
$message->addRecipient('to@test.com');
$message->setSender(array('sender@test.com'));

// And send !
$return = $mailer->transmit($message);

if ($return) {
    echo 'Mail transmit success' . PHP_EOL;
} else {
    echo 'Mail transmit failed' . PHP_EOL;
}

Keep in mind that you should always initialize a mailer client by a dependency injection component, since it requires at least one dependency, which is the transport. Moreover, the OPTION_BASEURL parameter should also depends on environment., (*10)

Real world example

Below a more robust example which use the BeanstalkProxyTransport as default transport., (*11)

<?php

require __DIR__ . '/vendor/autoload.php';

use Fei\ApiClient\Transport\BasicTransport;
use Fei\ApiClient\Transport\BeanstalkProxyTransport;
use Fei\Service\Mailer\Client\Mailer;
use Fei\Service\Mailer\Entity\Mail;
use Pheanstalk\Pheanstalk;

$mailer = new Mailer([
    Mailer::OPTION_BASEURL => 'https://api_host',
    Mailer::OPTION_HEADER_AUTHORIZATION => 'key'
]);

$async = new BeanstalkProxyTransport();
$async->setPheanstalk(new Pheanstalk('host'));
// Async transport will be the default transport. 
$mailer->setAsyncTransport($async);
// If async transport fails, then BasicTransport will take over.
$mailer->setTransport(new BasicTransport());

$message = new Mail();
$message->setSubject('Test subject');
$message->setTextBody('This is a example message');
$message->addRecipient('to@test.com');
$message->setSender(array('sender@test.com'));

$return = $mailer->transmit($message);

if ($return) {
    echo 'Mail transmit success' . PHP_EOL;
} else {
    echo 'Mail transmit failed' . PHP_EOL;
}

To work properly, BeanstalkProxyTransport needs a instance of a Beanstalkd server able to listen to its requests. Workers will consume Beanstalkd messages tube (or queue) and will send email data to Mail API server., (*12)

The message workflow:, (*13)

Client -> Pheanstalkd -> Workers -> Mail API server

Use the logger

Mailer client is Logger client aware. You could set a logger instance like this example below in order to activate logging functionality., (*14)

<?php

$logger = new Logger([
    Logger::OPTION_BASEURL => 'http://127.0.0.1:8082',
    Logger::OPTION_FILTER => Notification::LVL_INFO,
    Logger::OPTION_HEADER_AUTHORIZATION => 'key'
]);
$logger->setTransport(new BasicTransport());

$mailer = new Mailer([
    Mailer::OPTION_BASEURL => 'https://api_host',
    Mailer::OPTION_HEADER_AUTHORIZATION => 'key'
]);
$mailer->setTransport(new BasicTransport());
$mailer->setLogger($logger); // Set and activate the the logger functionality

As is each mail sent will be recorded with logger service., (*15)

Email and attachments

Here a example if you need to send email with attachments :, (*16)

<?php

require __DIR__ . '/vendor/autoload.php';

use Fei\Service\Mailer\Client\Mailer;
use Fei\ApiClient\Transport\BasicTransport;
use Fei\Service\Mailer\Entity\Mail;
use Fei\Service\Mailer\Entity\Attachment;

$message = new Mail();
$message->setSubject('Test subject');
$message->setTextBody('This is a example message');
$message->setHtmlBody('

This is a example message!, (*17)

'); $message->setRecipients(array('to@test.com' => 'Name', 'other@test.com' => 'Other Name')); $message->addCc('cc@example.com', 'CC'); $message->addBcc('bcc@example.com', 'CC'); $message->setSender(array('sender@test.com' => 'The sender')); $message->setReplyTo(array('steve@app.com' => 'Steve')); // Add a attachment with a \SplObjectFile $message->addAttachment(new \SplFileObject('/to/file/path/image.png')); // Or add a attachment with a "generated string" $attachment = array( 'filename' => 'document.txt', 'mime_type' => 'text/plain', // Note base64_encode 'contents' => base64_encode('Hello world!') . PHP_EOL ); $message->addAttachment($attachment); // Or add a attachment object $attachment = new Attachment('/to/file/path/document.pdf'); $attachment->setAttachmentFilename('another-filename.txt'); $attachment->setMimeType('text/plain'); $message->addAttachment($attachment); $mailer = new Mailer([ Mailer::OPTION_BASEURL => 'https://api_host', Mailer::OPTION_HEADER_AUTHORIZATION => 'key' ]); $mailer->setTransport(new BasicTransport()); $mailer->transmit($message);

Embedding attachment

Sometime, you need to include image (or other media) inline in your message. You could use a resource URL in order to linking the media but this approach is usually blocked by mail clients. A another approach is to embed your media directly into your message., (*18)

<?php

use Fei\Service\Mailer\Entity\Mail;
use Fei\Service\Mailer\Entity\Attachment;

$message = new Mail();

$embedded = (new Attachment('/my/picture.png', true));
$message->addAttachment($embedded);

$message->setTextBody('This is a example message');
$message->setHtmlBody(<<<HTML
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example</title>
</head>
<body>
    <p>
        <img src="{$embedded->getCid()}" style="display: block; width: 100px; height: 100px; float: right;">
        This is example with a embedded image
    </p>
</body>
</html>
HTML
);

And that's it., (*19)

Catch them all

In non production environment, you often don't need to send email to the real recipient. For testing purpose, you could initialize the client with the option OPTION_CATCHALL_ADDRESS and all email will be forwarded to email address passed to this option., (*20)

<?php

use Fei\Service\Mailer\Client\Mailer;

$mailer = new Mailer([
    Mailer::OPTION_BASEURL => 'https://api_host',
    Mailer::OPTION_CATCHALL_ADDRESS => 'testing@email.com',
    Mailer::OPTION_HEADER_AUTHORIZATION => 'key'
]);

Add callback functions before Mail instance validation

For different needs, you could registered a callback to apply on Mail to be send before its state validation., (*21)

You can see this as a another way to extends the "mailer-client" functionalities., (*22)

<?php

use Fei\Service\Mailer\Client\Mailer;
use Fei\Service\Mailer\Entity\Mail;

$mailer = new Mailer();
$mailer->addCallbackBeforeValidation(function (Mail $mail) {
    $mail->setRecipients(['another@email.com']);
});

With this example, all Mail send with this client will have theirs recipients changed by another@email.com., (*23)

We provide a couple of another method to manage callbacks:, (*24)

  • Mailer::addFirstCallbackBeforeValidation: append a callback on the first place of the stack to be executed in contrast to addCallbackBeforeValidation which place the callback in the end of the chain execution.
  • Mailer::clearCallbackBeforeValidation: remove all callback registered in the stack

Pricer integration: meet the PricerMailer class

We provide a child class of Mailer for the Pricer need: PricerMailer., (*25)

PricerMailer extends functionalities Mailer plus a callback which apply email filtering on the Mail instance to be send. Naturally, you can remove this filter or add yours own., (*26)

Client option

Only one option is available which can be passed to the __construct() or setOptions() methods:, (*27)

Option Description Type Possible Values Default
OPTION_BASEURL This is the server to which send the requests. string Any URL, including protocol but excluding path -
OPTION_CATCHALL_ADDRESS Recipient to substitute to any other. When used, original recipients, ccs, bccs are prepended to mail body. string Any valid email address -
OPTION_HEADER_AUTHORIZATION Api Key for authentification string Any string value ''

The Versions

10/01 2018

dev-feature/update_composer_api_client

dev-feature/update_composer_api_client

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

13/12 2017

v1.0.18

1.0.18.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

26/06 2017

dev-feature/4532_wrongDelimiter_dev

dev-feature/4532_wrongDelimiter_dev

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

02/02 2017

dev-develop

dev-develop

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

02/02 2017

v1.0.17

1.0.17.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

02/11 2016

v1.0.16

1.0.16.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

31/10 2016

v1.0.15

1.0.15.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

20/10 2016

v1.0.14

1.0.14.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

07/10 2016

v1.0.13

1.0.13.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

05/10 2016

v1.0.12

1.0.12.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

04/10 2016

v1.0.11

1.0.11.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

30/09 2016

v1.0.10

1.0.10.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

19/09 2016

v1.0.9

1.0.9.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

14/09 2016

v1.0.8

1.0.8.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

30/08 2016

v1.0.7

1.0.7.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

28/07 2016

v1.0.6

1.0.6.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

27/07 2016

v1.0.5

1.0.5.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

27/07 2016

v1.0.4

1.0.4.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

25/07 2016

v1.0.3

1.0.3.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

25/07 2016

v1.0.2

1.0.2.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

25/07 2016

v1.0.1

1.0.1.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client

23/07 2016

v1.0.0

1.0.0.0

Flash mailer client

  Sources   Download

The Requires

 

The Development Requires

by Gauthier Delamarre
by Vincent Simonin

mailer client