Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Die Dateien /htdocs_symfony/.env oder /htdocs_symfony/.env.test local um folgende Zeile erweitern

...

Code Block
<?php

namespace Oc\Controller\Backend;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;

/**
 *
 */
class MailerController extends AbstractController
{
    /**
     * @param MailerInterface $mailer
     * @param LoggerInterface $logger
     *
     * @return Response
     * @throws TransportExceptionInterface
     * @Route("/email", name="email_sendmail")
     *
     * https://symfony.com/doc/4.1/email/dev_environment.html#viewing-from-the-web-debug-toolbar
     */
    public function sendEmail(MailerInterface $mailer, LoggerInterface $logger)
    : Response {
        $email = (new Email())
            ->from('hello@example.com')
            ->to('you@example.com')
            //->cc('cc@example.com')
            //->bcc('bcc@example.com')
            //->replyTo('fabien@example.com')
            //->priority(Email::PRIORITY_HIGH)
            ->subject('Time for Symfony Mailer!')
            ->text('Sending emails is fun again!')
            ->html('<p>See Twig integration for better HTML integration!</p>');

        $mailer->send($email);
        $logger->info('### Email gesendet ### ');

        return $this->redirectToRoute('backend_index_index');
    }
}

Mail senden und anzeigen

Die Route aus dem Controller aufrufen mittels

Code Block
http://try.docker.team-opencaching.de/backend/email

Die Funktion ‘sendEmail’ wird aufgerufen, die Mail versendet und der anschließende Redirect nicht ausgeführt. In der Symfonystatusleiste erscheint ein Briefsymbol, welches zur versendeten Email weiterleitet.

...