Swift Mailer on Symfony 3.4,配置

时间:2018-12-28 03:18:21

标签: php symfony swiftmailer

这是我在php bin/console debug:config swiftmailer中看到的内容:

enter image description here

我可以使用php bin/console swiftmailer:email:send发送电子邮件,它们可以正常发送:

enter image description here

这是我帐户中的电子邮件地址:

enter image description here

但是我无法使用Symfony上的控制器来做到这一点。我已经测试了许多示例,但没有一个有效。我已经在dev和prod环境中进行了测试(主要是在这里,当然是我需要的地方)。这是我的代码的示例:

public function sendEmail(Request $request ) {

    $message = \Swift_Message::newInstance()
    ->setSubject('Some Subject')
    ->setFrom('example@gmail.com')
    ->setTo('myemail@gmail.com')
    ->setBody('asdas');

    $this->get('mailer')->send($message);

    dump($message);
    die();

我可以在输出中看到完整的消息,但没有发送电子邮件。

我也通过这种方式进行了测试:

$message = (new \Swift_Message('Wonderful Subject'))
          ->setFrom(['john@doe.com' => 'John Doe'])
          ->setTo(['myemail@gmail.com' => 'A name'])
          ->setBody('Here is the message itself');

$this->get('mailer')->send($message);

同样,什么也没发生。怎么了?

1 个答案:

答案 0 :(得分:2)

您正在使用假脱机选项,这会延迟发送电子邮件。禁用线轴或删除die()。

https://symfony.com/doc/current/email/spool.html

当您使用Swiftmail假脱机时,它会保存电子邮件以在终止事件(https://symfony.com/doc/current/components/http_kernel.html#the-kernel-terminate-event)中发送该电子邮件,在您的情况下这不会发生,因为您在最后一行中用die();杀死了脚本

  

kernel.terminate在Symfony框架中

     

如果您使用默认Symfony邮件程序的内存假脱机选项,则会激活EmailSenderListener,它实际上会发送您计划在请求期间发送的所有电子邮件。