首页 > 解决方案 > 在 Symfony 4 中使用 swift mailer 发送邮件

问题描述

我正在尝试使用 symfony 4 通过 swift mail 发送电子邮件。下面的代码是我迄今为止尝试过的。但是电子邮件没有发送。

.env

MAILER_URL=smtp://localhost:25?encryption=&auth_mode=

控制器

/**
     * @Route("/registration", name="new_registration")
     * Method({"GET","POST"})
     */

    public function newRegistration(Request $request, \Swift_Mailer $mailer)
    {

        if ($form->isSubmitted() && $form->isValid()){

            $message = (new \Swift_Message('Hello Mail'))
                ->setFrom('noreply@cccc.org')
                ->setTo('abcccc@gmail.com')
                ->setBody(
                    $this->renderView(
                        'main/registrationEmail.html.twig',
                        ['name' => $name]
                    ),
                    'text/html'
                );
            try {
                $mailer->send($message);
            } catch (\Swift_TransportException $exception) {
                echo $exception->getMessage();
            } catch (\Exception $exception) {
                echo $exception->getMessage();
            }

            return $this->render('main/registrationSuccess.html.twig');
        }
        return $this->render('main/registration.html.twig');
    }

没有显示任何错误。但是邮件没有发送。请帮助我。

更新

我尝试通过控制台发送并使用php bin/console swiftmailer:email:send. 从那我得到以下错误。

2019-03-14T11:40:32+01:00 [error] Exception occurred while flushing email queue: Connection could not be established with host localhost [No connection could be made because the target machine actively refused it.
 #10061]

标签: phpsymfony4swiftmailer

解决方案


推荐阅读