PHP code example of semhoun / slim-mailer

1. Go to this page and download the library: Download semhoun/slim-mailer 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/ */

    

semhoun / slim-mailer example snippets


$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions([
	'mailer' => function (ContainerInterface $container) {
            $settings = $container->get('settings');
            $view = $container->get('view');
			$mailer = new \Semhoun\Mailer\Mailer($view, [
                'host'      => '',  // SMTP Host
                'port'      => '',  // SMTP Port
                'username'  => '',  // SMTP Username
                'password'  => '',  // SMTP Password
                'protocol'  => ''   // SSL or TLS
            ]);
        
    		// Set the details of the default sender
    		$mailer->setDefaultFrom('[email protected]', 'Webmaster');
    
    		return $mailer;
    }
};
   
$app->run();

$app->get('/email', function (Request $request, Response $response, $args)  use($app) {
	$user = new stdClass;
    $user->name = 'Paul Muaddib';
    $user->email = '[email protected]';
    
    $container = $app->getContainer();
    $container->get('mailer')->sendMessage('emails/welcome.html.twig', ['user' => $user], function($message) use($user) {
        $message->setTo($user->email, $user->name);
        $message->setSubject('Welcome to the Team!');
    });
    
    $response->getBody()->write('Mail sent!');
    
    return $response;
});

use Semhoun\Mailer\Mailable;

class WelcomeMailable extends Mailable
{
    
    protected $user;
    
    public function __construct($user)
    {
        $this->user = $user;
    }
    
    public function build()
    {
        $this->setSubject('Welcome to the Team!');
        $this->setView('emails/welcome.html.twig', [
            'user' => $this->user
        ]);
        
        return $this;
    }
    
}

$app->get('/email', function (Request $request, Response $response, $args)  use($app) {
	$user = new stdClass;
    $user->name = 'Paul Muaddib';
    $user->email = '[email protected]';
    
    $container = $app->getContainer();
    $container->get('mailer')->->setTo($user->email, $user->name)->sendMessage(new WelcomeMailable($user));
    
    $response->getBody()->write('Mail sent!');
    
    return $response;
});