PHP code example of kachar / zend-mjml

1. Go to this page and download the library: Download kachar/zend-mjml 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/ */

    

kachar / zend-mjml example snippets


    'modules' => [
        'ZendMjml',
    ],
    

    'mjml' => [
        'transportAdapter' => [
            'type' => 'sendmail',
            'options' => [
                // see http://www.sendmail.org/~ca/email/man/sendmail.html
            ],
        ],
    ],

    'mjml' => [
        'transportAdapter' => 'Zend\Mail\Transport\Sendmail',
    ],
    'service_manager' => [
        'invokables' => [
            'Zend\Mail\Transport\Sendmail' => 'Zend\Mail\Transport\Sendmail',
        ],
    ],


    $service = $this->getServiceLocator()->get('Service\Mjml');
    $mjml = '
    <mj-body>
      <mj-section>
        <mj-column>
          <mj-image width="100" src="https://mjml.io/assets/img/logo-small.png"></mj-image>
          <mj-divider border-color="#F45E43"></mj-divider>
          <mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello {{ name }}</mj-text>
        </mj-column>
      </mj-section>
    </mj-body>
    ';

    // This is the final html to be sent to the recipients
    $body = $service->renderMjml($mjml);

    // You can replace simple variables inside the final output
    $body = $service->renderMjml($mjml, [
        'name' => 'direct example',
    ]);

    // Sending the email
    $email = $service->composeEmail($body);
    $email->setTo('[email protected]');
    $email->setSubject('Sample responsive MJML email');
    $service->sendEmail($email);
    // or
    echo $body;


    $service = $this->getServiceLocator()->get('Service\Mjml');

    $view = new ViewModel();
    $view->name = 'John Doe';
    $view->setTemplate('mjml/plain.mjml');
    // This is the final html to be sent to the recipients
    $body = $service->renderView($view);

    // Sending the email
    $email = $service->composeEmail($body);
    $email->setTo('[email protected]');
    $email->setSubject('Sample responsive MJML email using ViewModel');
    $service->sendEmail($email);


    $view = new ViewModel();
    // This will be replaced using str_replace in the final template
    $view->name = 'PHP MJML Template';

    // This will be replaced using php in the initial mjml template
    $view->products = [
        [
            'name' => 'Ham',
            'price' => '1.50',
            'quantity' => 5,
        ],
        [
            'name' => 'Cheese',
            'price' => '3.75',
            'quantity' => 3,
        ],
        [
            'name' => 'Bread',
            'price' => '5.00',
            'quantity' => 10,
        ],
    ];
    $view->setTemplate('mjml/php-mjml.pmjml');
    // This is the final html to be sent to the recipients
    $body = $service->renderView($view);

    // Sending the email
    $email = $service->composeEmail($body);
    $email->setTo('[email protected]');
    $email->setSubject('Sample responsive MJML email using PhpRenderer');
    $service->sendEmail($email);
bash
    $ php composer.phar update