0

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-email@test.com');
$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? – mickmackusa Jan 10 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 at 12:53
1

Within Joomla you could use JLayout.

$body = JLayoutHelper::render('path.to.layout');
$mailer->setBody($body);

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.

ob_start();
include 'path/to/layout.php';
$body = ob_get_contents();
ob_end_clean();
$mailer->setBody($body);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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