How to access Email Document dynamic params


#1

Hi,

I have an email document created and now I want to send a mail with params set.

$mail = new \Pimcore\Mail();
$mail->addTo($userEmail);
$mail->setDocument('/Foresight/AccessRequestApproved');
$mail->setParams(["countryCode" => $countryCode, "userGivenName" => $userGivenName]);
$mail->send();

How I can access these set parameters in the view (I am using php templating, not twig) and inside the linked controller’s action?

In the view the placeholders work but not the variables

  • This works:
%Text(countryCode);
  • This doesn’t
<?= $countryCode; ?>

Thanks a lot


#2

Hi @mertaivan,

First set your param as you did already:

$mail->setParams(['myVariable' => $data]);

Then you can get that param from the request inside your controller like so:

// src/AppBundle/Controller/EmailController.php

use Symfony\Component\HttpFoundation\Request;

public function renderEmailAction(Request $request)
{
    $myVariable = $request->get('myVariable');

    return $this->renderTemplate('Email/my-email-template.html.php', [
        'myVariable' => $myVariable,
    ]);
}

Now you can use it inside your email template:

<?= $myVariable; ?>

Hope this helps.


#3

And don’t use php template engine :wink:


#4

@aarongerig - thanks a lot, I’ll try that as soon as I get home.
@dpfaffenbauer - fair point :slight_smile:


#5

Yes, works as expected. Thanks again.