【问题标题】:How to send smtp email from Command using Swiftmailer with Symfony如何使用 Swiftmailer 和 Symfony 从 Command 发送 smtp 电子邮件
【发布时间】:2020-02-27 13:59:58
【问题描述】:

我在使用 Symfony 发送电子邮件时遇到问题。

swiftmailer.yaml

swiftmailer: 
  default_mailer: gmail_api 
  mailers: 
   gmail_api: 
     transport: gmail_api

如你所见,我没有使用假脱机。

我的电子邮件是从 Controller 正确发送的,但不是从 Command 发送的。

从 Controller,send() 返回 1,我收到了电子邮件。

namespace App\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;

class CRUDController extends Controller
{

private $mailer;
...

public function __construct(\Swift_Mailer $mailer)
{
    $this->mailer = $mailer;
    ...
}

private function sendEmail($object, $identifiant, $type)
{
    $message = (new \Swift_Message('xxx ' . $identifiant))
                ->setFrom('xxx@xxx.xx')
                ->setTo('xxx@xxx.xx')
                ->setBody(
                    $this->renderView(
                        ...
                    ),
                    'text/html'
                )
                ->attach(\Swift_Attachment::fromPath('xxx');
    try {
        $logger = new \Swift_Plugins_Loggers_ArrayLogger();
        $this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));

        $numSent = $this->mailer->send($message);

        if ($numSent < 1) {
            $this->addFlash('sonata_flash_error', 'Fail to send e-mail');
        } else {
            if (method_exists($object, 'getMailing')
                && method_exists($object, 'setMailing')) {
                $object->setMailing($object->getMailing() + 1);
                $this->em->persist($object);
                $this->em->flush();
            }
        }
    } catch (\Swift_TransportException $e) {
        $this->addFlash('sonata_flash_error', $e->getMessage());
    }
    dump($logger->dump());
    exit();
}

\Swift_Plugins_Loggers_ArrayLogger 给我:

++ 启动 Swift_Transport_EsmtpTransport ...

从命令,send() 返回 0,我没有收到电子邮件。

class ExempleCommand extends Command
{
    private $mailer;
    ...   

    public function __construct(\Swift_Mailer $mailer)
    {
        parent::__construct();
        $this->mailer = $mailer;
        ...
    }



    protected function sendNotificationMail(OutputInterface $output, $subject, $body)
    {
        $logger = new \Swift_Plugins_Loggers_ArrayLogger();
        $this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));

        $message = (new \Swift_Message($subject))
            ->setFrom('xxx@xxx.xx')
            ->setTo('xxx@xxx.xx')
            ->setBody(
                $this->twig->render(
                ...
                ),
                'text/html'
            );
        try {
            $output->write("\n" . $this->mailer->send($message));
        } catch (\Swift_TransportException $e) {
            echo $e->getMessage();
        }
        $output->write($logger->dump());
    }

\Swift_Plugins_Loggers_ArrayLogger 给我:

++ 启动 App\Service\Email\GmailApiTransport ++ App\Service\Email\GmailApiTransport 已启动

为什么它不在 Command 中以 Swift_Transport_EsmtpTransport 开头?

【问题讨论】:

    标签: symfony smtp symfony4 swiftmailer


    【解决方案1】:

    我不确定它在 Symfony 4 上是如何工作的,有一段时间以来,我从来不用通过命令发送邮件,但是在 Symfony 2 上,当使用命令时,你必须启动一个新的邮件程序实例。因此,也许您可​​以尝试将构造函数更改为

    public function __construct(\Swift_SmtpTransport $transport)
    {
        parent::__construct();
        $this->mailer = new \Swift_Mailer($transport);
    }
    

    (对不起,我还没有权限简单地发表评论)

    【讨论】:

    • 对不起,错误的线索。
    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    相关资源
    最近更新 更多