I have created a custom form, after submit send email to recipient with JFactory::getMailer With this class can add body, for example:

$mailer = JFactory::getMailer();
$mailer->addRecipient('test-em[email protected]');
$mailer->setBody('Hello world simple text');

In my case, my template email have many div, styles css, etc, and is pretty ugly include entire html into setBody method. Is there an alternative?

  • You can just save all of the body content as a concatenated string written over multiple lines. This should help to keep things clean and readable. I'm not sure I understand the challenge. Can you provide some sample input to give your question context? Jan 10, 2019 at 12:48
  • Hi mickmackusa, yes, it's possible do this, but isn't there any more elegant solution? For example associate view file as MVC pattern?
    – ClodClod91
    Jan 10, 2019 at 12:53

1 Answer 1


Within Joomla you could use JLayout.

$body = JLayoutHelper::render('path.to.layout');

If you have any variables you need to use in the layout, pass them as second argument:

$body = JLayoutHelper::render('path.to.layout', $displayData);

But because this is done for emails, you probably don't need JLayout features like template overrides. So you can use PHP's native ob_get_contents() to get output from PHP file. Internally JLayout works the same way.

include 'path/to/layout.php';
$body = ob_get_contents();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.