PHP code example of lotfio / mail-templator

1. Go to this page and download the library: Download lotfio/mail-templator 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/ */

    

lotfio / mail-templator example snippets


    composer 

    composer test-unit
    composer test-integration

    composer psalm



namespace MyCustomMilTemplates;

use MailTemplator\Templator;
use MailTemplator\Contracts\TemplateInterface;

final class MyTemplate extends Templator implements TemplateInterface
{
    /**
     * render this template method
     *
     * @param  array|null $variables
     * @return string
     */
    public function render(?array $variables = null): string
    {
        return $this->loadTemplate()->parse($variables);
    }
}




namespace MyCustomMilTemplates;

use MailTemplator\Templator;
use MailTemplator\Contracts\TemplateInterface;

final class MyTemplate extends Templator implements TemplateInterface
{
    // you can use a protected property
    protected string $logo = '<img src="https://mysite.com/logo.png">';

    // or a protected setter
    protected function setLogo(): void
    {
      $this->logo = '<img src="https://mysite.com/logo.png">';
    }

    // all other directives can be updated the same way
}




namespace MyCustomMilTemplates;

use MailTemplator\Templator;
use MailTemplator\Contracts\TemplateInterface;

final class MyTemplate extends Templator implements TemplateInterface
{
    // declare your custom directives
    protected string $content = 'Hello @USERNAME@ how are u ?';
}


final class MyTemplate extends Templator implements TemplateInterface
{
    /**
     * render this template method
     *
     * @param  array|null $variables
     * @return string
     */
    public function render(?array $variables = null): string
    {
        return $this->loadTemplate(
          'link/to/your/static/tepmlate.html'
        )->parse($variables);
    }
}


// your mailer (PHPMailer or swift)
// should implement MailAdapterInterface
$customMailer = new class implements MailTemplator\Contracts\MailAdapterInterface{
  public function send(string $to, string $subject, string $message): bool
  {
     // your mailer send should be wrapped here
     // $subject will be taken from template class name  if no custom subject provided
  }
}

// create an instance of mail class
$mail = new MailTemplator\Mail(
  $customMailer
);

// set your template
$mail->setTemplate(
  new MyCustomMilTemplates\MyTemplate
);

// send mail with the given template
$mail->send('to');


public function register()
{
    // bind custom mailer that implements MailAdapterInterface
    $this->app->bind(MailTemplator\Contracts\MailAdapterInterface::class, function($app){
        return new CustomMailer;
    });
}

public function resetPassword(MailTemplator\Mail $mail)
{
    // send mail
    $mail->setTemplate(
      new App\Mail\Templates\ResetPasswordTemplate
      )->send('[email protected]');
}