# Variablen an E-Mail-Template übergeben

**URL:** <https://forum.shopware.com/t/variablen-an-e-mail-template-uebergeben/85833>\
**Category:** Shopware 5\
**Tags:** templates--design\
**Created:** [9. März 2021 um 18:35 UTC](https://forum.shopware.com/t/variablen-an-e-mail-template-uebergeben/85833 "2021-03-09T18:35:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mewcrazy](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/mewcrazy/32/10044_2.png) [@mewcrazy](https://forum.shopware.com/u/mewcrazy)\
**Post date:** [9. März 2021 um 18:35 UTC](https://forum.shopware.com/t/variablen-an-e-mail-template-uebergeben/85833/1 "2021-03-09T18:35:22Z")

</div>

Wie lassen sich bei Shopware 5 in einem Plugin Variablen an ein E-Mail-Template übergeben? Ich habe den context an die createMail()-Funktion übergeben, aber diese Variablen werden nicht im E-Mail-Template ausgegeben.

Mein Code sieht aktuell so aus:

```
public static function getSubscribedEvents() {
    return array(
        'Shopware_CronJob_CheckCustomerGroup' => 'onCronjobExecute'
    );
}

/**
 * Checks the eligible customer discount group for each customer
 *
 * @param Shopware_Components_Cron_CronJob $job
 */
public function onCronjobExecute(\Shopware_Components_Cron_CronJob $job) {
                    /** send a mail with shopware mail template */
                    $mailTemplate = $this->container->get('models')->getRepository(Mail::class)->findOneBy(['name' => "CustomerGroupChange"]);

                    if (!$mailTemplate instanceof Mail) {
                        return;
                    }

                    try {

                        $mail = Shopware()->TemplateMail()->createMail(
                            $mailTemplate->getName(),
                            [
                                'sTotal' => $total,
                                'sTotallyNot' => 'lol',
                            ],
                            Shopware()->Shop()
                        );
                        $mail->addTo("lorem@ipsum.do");
                        $mail->send();
                    } catch (Exception $e) {
                        $this->logger->addError($e->getMessage());
                    }
}

```

Im E-Mail-Template „CustomerGroupChange“ verwende ich die o.g. Variablen so: {$sTotallyNot} {$sTotal} - Leider erscheint da bei mir gar nichts, selbst der lol-String nicht.

Wie wende ich den context richtig an?

---

<div class="post-metadata">

**Author:** ![stevevomwege](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/stevevomwege/32/21643_2.png) [@stevevomwege](https://forum.shopware.com/u/stevevomwege)\
**Post date:** [11. März 2021 um 08:13 UTC](https://forum.shopware.com/t/variablen-an-e-mail-template-uebergeben/85833/2 "2021-03-11T08:13:41Z")

</div>

Moin 🙂

$mailTemplate-\>getContext() statt Shopware()-\>Shop()

Quelle: [shopware/Mail.php at b206fe23b16f8a4eaa91bf825a1f61bba1c3a469 · shopware/shopware · GitHub](https://github.com/shopware/shopware/blob/b206fe23b16f8a4eaa91bf825a1f61bba1c3a469/engine/Shopware/Controllers/Backend/Mail.php)

Zeile 326

---

<div class="post-metadata">

**Author:** ![mewcrazy](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/mewcrazy/32/10044_2.png) [@mewcrazy](https://forum.shopware.com/u/mewcrazy)\
**Post date:** [11. März 2021 um 08:38 UTC](https://forum.shopware.com/t/variablen-an-e-mail-template-uebergeben/85833/3 "2021-03-11T08:38:16Z")

</div>

Moin Steve,  
Danke! Mit deiner Hilfe habe ich es hinbekommen.

Falls jemand das selbe Problem hat, mit folgendem Code habe ich es hinbekommen das die Variablen im E-Mail-Template ankommen:

```
                    $shop = Shopware()->Models()->getRepository(Shop::class)->getActiveDefault();
                    
                    /** send a mail with shopware mail template */
                    $mailTemplate = $this->container->get('models')->getRepository(Mail::class)->findOneBy(['name' => "CustomerGroupChange"]);

                    if (!$mailTemplate instanceof Mail) {
                        return;
                    }

                    try {

                        $mail = Shopware()->TemplateMail()->createMail(
                            $mailTemplate->getName(),
                            array_merge(
                                [
                                    'sTotal' => $total,
                                    'sTotallyNot' => 'lol',
                                ],
                                $mailTemplate->getContext()
                            ),
                            $shop
                        );
                        $mail->addTo("db@web-labels.de");
                        $mail->send();
                    } catch (Exception $e) {
                        $this->logger->addError($e->getMessage());
                    }

```

Wichtig war wohl den E-Mail Context mit dem vorhanden Context zu mergen. Und der Dritte Parameter muss die aktive Shop-Instanz sein.

Thema kann geschlossen werden.
