PHP code example of phpbook / email

1. Go to this page and download the library: Download phpbook/email library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpbook / email example snippets



/********************************************
 * 
 *  Declare Configurations
 * 
 * ******************************************/

//Driver connection SMTP

\PHPBook\Email\Configuration\Email::setConnection('main',
	(new \PHPBook\Email\Configuration\Connection)
		->setName('Main')
		->setExceptionCatcher(function(String $message) {
			//the PHPBook Email does not throw exceptions, but you can take it here
			//you can store $message in database or something else
		})
		->setDriver((new \PHPBook\Email\Driver\SMTP)
			->setHost('host')
			->setPort(100)
			->setUser('user')
			->setPassword('password')
			->setSecure('tls')
			->setEmail('[email protected]')
			->setName('Jhon')
			->setEncode('utf8'))
);

//Driver connection AWSSES

\PHPBook\Email\Configuration\Email::setConnection('other',
	(new \PHPBook\Email\Configuration\Connection)
		->setName('Other')
		->setExceptionCatcher(function(String $message) {
			//the PHPBook Email does not throw exceptions, but you can take it here
			//you can store $message in database or something else
		})
		->setDriver((new \PHPBook\Email\Driver\AWSSES)
			->setKey('key')
			->setSecret('secret')
			->setRegion('region')
			->setEmail('[email protected]')
			->setName('Jhon'))
);


//Driver connection MAILGUN

\PHPBook\Email\Configuration\Email::setConnection('important', 
	(new \PHPBook\Email\Configuration\Connection)
		->setName('Important')
		->setExceptionCatcher(function(String $message) {
			//the PHPBook Email does not throw exceptions, but you can take it here
			//you can store $message in database or something else
		})
		->setDriver((new \PHPBook\Email\Driver\MAILGUN)
			->setKey('key')
			->setDomain('domain')
			->setEmail('[email protected]')
			->setName('Jhon'))
);

//Driver connection SPARKPOST

\PHPBook\Email\Configuration\Email::setConnection('backups', 
	(new \PHPBook\Email\Configuration\Connection)
		->setName('Backups')
		->setExceptionCatcher(function(String $message) {
			//the PHPBook Email does not throw exceptions, but you can take it here
			//you can store $message in database or something else
		})
		->setDriver((new \PHPBook\Email\Driver\SPARKPOST)
			->setKey('key')
			->setEmail('[email protected]')
			->setName('Jhon'))
);


//Set default connection by connection alias

\PHPBook\Email\Configuration\Email::setDefault('main');

//Getting connections

$connections = \PHPBook\Email\Configuration\Email::getConnections();

foreach($connections as $code => $connection) {

	$connection->getName(); 

	$connection->getDriver();

};


		

	//Connection code is not ail\Email)
		->setConnectionCode('other')
		->setMessage(
			(new \PHPBook\Email\Message)
				->setFromName('[email protected]') //custom from name. not bject('email subject')
				->setContent('my html body')
				->setAttach([
					(new \PHPBook\Email\Attach)->setFileAlias('myfile')->setFileBuffer('my-file-buffer'),
					(new \PHPBook\Email\Attach)->setFileAlias('myfile')->setFilePath('my/file/path')
				])
		)->dispatch();

	if ($boolean) {
		//sent
	};