How mjml in mail php

MJML (Mailjet Markup Language) is a markup language that allows you to create responsive and customizable email templates using HTML and CSS. To use MJML in a PHP application, you'll need to follow these steps:

  1. Install the MJML PHP library: You can install the MJML PHP library using Composer by running the following command:
    composer require mailjet/mjml-php
  2. Create an MJML template: Create an MJML template file (e.g., template.mjml) with your desired email layout and content. You can use the MJML syntax to define the structure and styling of your email.

Here's an example of a simple MJML template:

<mjml>
  <mj-body>
    <mj-section>
      <mj-column>
        <mj-text font-size="18px" font-family="Arial">Hello, World!</mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
  1. Render the MJML template using PHP: In your PHP code, use the MJML PHP library to render the MJML template and convert it to HTML. You can do this by creating a PHP function that takes the MJML template as input and returns the rendered HTML.

Here's an example of how you can render the MJML template using PHP:

<?php

require 'vendor/autoload.php';

use Mailjet\Mjml\Mjml;

function renderMjmlTemplate($mjmlTemplate) {
  $mjml = new Mjml();
  $html = $mjml->render($mjmlTemplate);
  return $html;
}

// Load the MJML template
$mjmlTemplate = file_get_contents('template.mjml');

// Render the MJML template
$html = renderMjmlTemplate($mjmlTemplate);

// Send the email using your preferred email library (e.g., PHPMailer)
$mail = new PHPMailer();
$mail->setBody($html);
$mail->addAddress('[email protected]');
$mail->send();

?>
  1. Send the email: Finally, use your preferred email library (e.g., PHPMailer) to send the rendered HTML email to the recipient.

That's it! By following these steps, you can use MJML to create responsive and customizable email templates in your PHP application.