Wallogit.com
2017 © Pedro Peláez
Client side wrapper to the Fluent Web Service
The PHP client for the Fluent Web Service. Fluent is a simple and elegant way to create and send transactional emails from your web or mobile app., (*1)
To get Fluent for PHP installed simply pull in the composer package:, (*2)
composer require fivesqrd/fluent:4.*
For Laravel projects there is an easy to install package available, (*3)
composer require fivesqrd/fluent-laravel
More info on the Laravel config options and the Fluent facade is available here: https://github.com/fivesqrd/fluent-laravel, (*4)
To send messages you'll first need to register Fluent account. Once registered, you'll receive API key to start sending messages immediately., (*5)
Passing config to the Message object at run time:, (*6)
$config = [
'key' => null, // Fluent access key
'secret' => null, // Fluent access secret
'headers' => null, // Optional default sender
];
$delivery = (new Fluent\Delivery($config))->create();
$events = (new Fluent\Event($config))->find();
To make bootstrapping eaiser, we've provided a way to preload config by assigning options to the static $defaults property of the Factory class:, (*7)
Fluent\Factory::$defaults = [
'key' => null, // Fluent access key
'secret' => null, // Fluent access secret
'headers' => null, // Optional default sender
];
$message = Fluent\Factory::message()->create();
$events = Fluent\Factory::event()->create();
One can easily deliver the message by combining the message body with the Fluent Web Service client., (*8)
$body = (new Fluent\Body())
->title('My little pony')
->paragraph('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
->number(['caption' => 'Today', 'value' => date('j M Y')])
->button('http://www.mypony.com', 'Like my pony')
->paragraph('Pellentesque habitant morbi tristique senectus et netus et malesuada fames.');
$messageId = (new Fluent\Delivery())->create()
->content($body)
->subject('Testing it')
->from('me@myapp.com', 'My App')
->to('user@theirdomain.com')
->send();
The following methods are provided to set up the delivery of the message:, (*9)
/* Add a subject to the message */
$message->subject('Lorem ipsum dolor');
/* Add a header to the message */
$message->header('Reply-To', 'me@myapp.com');
/* Add multiple headers to the message */
$message->headers(array(
'Reply-To', 'me@myapp.com',
'X-Fluent', 'lorem'
));
/* Set the sender address and name */
$message->from('me@myapp.com', 'My App');
Note: only one recipient can be provided per message., (*10)
/* Set the recipient address and name */
$message->to('user@theirdomain.com');
/* Add an attachment to the message */
$message->attach('My-Attachment.pdf', 'application/pdf', file_get_contents($file))
/* Add multiple attachments to the message */
$message->attachments(array(
['name' => 'My-First-File.pdf', 'type' => 'application/pdf', 'content' => file_get_contents($file)],
['name' => 'My-2nd-File.jpg', 'type' => 'image/jpg', 'content' => file_get_contents($file2)],
));
Send is the final method of the chain and should always be called last. It delivers to message to the Fluent Web Service and returns a unique message ID., (*11)
/* Send the message */ $messageId = $message->send();
It's possible to simplify the body construction and message delivery with pre-built template classes:, (*12)
/* Double action: construct and send */ $template = new MyApp\Template\PasswordReset($params); $messageId = (new Fluent\Delivery())->from($template)->send();
Sending with custom headers, (*13)
$messageId = (new Fluent\Delivery())->create()
->content($body)
->header('Reply-To', 'me@myapp.com')
->to('user@theirdomain.com')
->send();
Sending with attachments, (*14)
$messageId = (new Fluent\Delivery())->create()
->content($body)
->subject('Testing it')
->attach('My-Attachment.pdf', 'application/pdf', file_get_contents($file))
->to('user@theirdomain.com')
->send();
In most cases resending an email notification is hard because it has to be recreated again, but the application state has since changed. With Fluent it is possible to simply resend a snapshot of the original message. Optionally specify a different recipient., (*15)
$response = (new Fluent\Delivery())->resend($messageId)
->to('other@theirdomain.com')
->send();
$response = (new Fluent\Message())->get($messageId)->fetch();
$response = (new Fluent\Message())->find()
->from('me@myapp.com')
->to('user@theirdomain.com')
->since(date('Y-m-d H:i:s', strtotime('-2 days ago')))
->fetch();
Get delivery progress updates for a particular recipient address, (*16)
$response = (new Fluent\Event())->find()
->to('user@theirdomain.com')
->since(date('Y-m-d H:i:s', strtotime('-2 days ago')))
->type(['hard_bounce', 'soft_bounce', 'reject'])
->fetch();
Somtimes you need to send plain text or custom HTML emails. Fluent provides a way to do this:, (*17)
$messageId = (new Fluent\Message())->create('This is my plain text message body')
->subject('Testing it')
->header('Reply-To', 'me@myapp.com')
->to('user@theirdomain.com')
->send();
$html = '<html><body>This is my custom HTML message body, (*18)
</html></body>'; $messageId = (new Fluent\Message())->create(new Fluent\Message\Content\Raw($html)) ->subject('Testing it') ->header('Reply-To', 'me@myapp.com') ->to('user@theirdomain.com') ->send();
Instead of delivering the message it is possible to render a HTML document locally. For this we used one of our open source email designs called Musimal and wrapped it inside a PHP library. It accepts a Body object as argument and returns a responsive HTML email body as a string., (*19)
# add the open source HTML wrapper to the project composer require fivesqrd/fluent-musimal
Generate a responsive HTML string from the body object:, (*20)
/* Predefine the HTML rendering options */
$options = array(
'logo' => 'My App', //Plain text or publicly available URL. Image not wider than 200px
'color' => '#ff6600', //Primary colour
'footer' => 'My test generated by Musimal for PHP', //Text at bottom of all messages
);
/* Construct a message body */
$body = (new Fluent\Body())
->title('My little pony')
->paragraph('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
->number(['caption' => 'Today', 'value' => date('j M Y')])
->button('http://www.mypony.com', 'Like my pony')
->paragraph('Pellentesque habitant morbi tristique senectus et netus et malesuada fames.');
/* Render the body to an HTML document */
$html = (new Fluent\Layout($options))->render($body);