【问题标题】:Swiftmailer : The mailer "default" does not existSwiftmailer:邮件“默认”不存在
【发布时间】:2019-03-10 16:41:25
【问题描述】:

我有一个 Symfony 4.1 项目,我希望能够在开发环境中发送邮件,但是当我执行命令 php bin/console swiftmailer:email:send 时出现此错误:

在 NewEmailCommand.php 第 76 行:邮件程序“默认”不存在。

我认为这是我的配置有问题,因为我的印象是它在 services.yaml 中找不到我的默认邮件程序的密钥。

packages/dev/swiftmailer.yaml:

swiftmailer:
    mailers:
        no_reply:
            delivery_address: test@yopmail.com

packages/swiftmailer.yaml:

swiftmailer:
    default_mailer: no_reply
    mailers:
        no_reply:
            url: '%env(MAILER_URL)%'
            spool: { type: memory }

BaseController.php:

<?php

declare(strict_types = 1);

namespace App\Controller;

use Swift_Mailer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class BaseController extends AbstractController
{
    /**
     * @var Swift_Mailer
     */
    protected $swiftMailer;

    public function __construct(Swift_Mailer $swiftMailer)
    {
        $this->swiftMailer           = $swiftMailer;
    }
}

我所有的控制器都从 BaseController.php 扩展


更新

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Bundle\SwiftmailerBundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * A console command for creating and sending simple emails.
 *
 * @author Gusakov Nikita <dev@nkt.me>
 */
class NewEmailCommand extends AbstractSwiftMailerCommand
{
    protected static $defaultName = 'swiftmailer:email:send';

    /** @var SymfonyStyle */
    private $io;

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName(static::$defaultName) // BC with 2.7
            ->setDescription('Send simple email message')
            ->addOption('from', null, InputOption::VALUE_REQUIRED, 'The from address of the message')
            ->addOption('to', null, InputOption::VALUE_REQUIRED, 'The to address of the message')
            ->addOption('subject', null, InputOption::VALUE_REQUIRED, 'The subject of the message')
            ->addOption('body', null, InputOption::VALUE_REQUIRED, 'The body of the message')
            ->addOption('mailer', null, InputOption::VALUE_REQUIRED, 'The mailer name', 'default')
            ->addOption('content-type', null, InputOption::VALUE_REQUIRED, 'The body content type of the message', 'text/html')
            ->addOption('charset', null, InputOption::VALUE_REQUIRED, 'The body charset of the message', 'UTF8')
            ->addOption('body-source', null, InputOption::VALUE_REQUIRED, 'The source where body come from [stdin|file]', 'stdin')
            ->setHelp(
                <<<EOF
The <info>%command.name%</info> command creates and sends a simple email message.

<info>php %command.full_name% --mailer=custom_mailer --content-type=text/xml</info>

You can get body of message from a file:
<info>php %command.full_name% --body-source=file --body=/path/to/file</info>

EOF
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->io = new SymfonyStyle($input, $output);
        $this->io->title('SwiftMailer\'s Interactive Email Sender');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $mailerServiceName = sprintf('swiftmailer.mailer.%s', $input->getOption('mailer'));
        if (!$this->getContainer()->has($mailerServiceName)) {
            throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $input->getOption('mailer')));
        }

        switch ($input->getOption('body-source')) {
            case 'file':
                $filename = $input->getOption('body');
                $content = file_get_contents($filename);
                if (false === $content) {
                    throw new \Exception(sprintf('Could not get contents from "%s".', $filename));
                }
                $input->setOption('body', $content);
                break;
            case 'stdin':
                break;
            default:
                throw new \InvalidArgumentException('Body-input option should be "stdin" or "file".');
        }

        $message = $this->createMessage($input);
        $mailer = $this->getContainer()->get($mailerServiceName);
        $sentMessages = $mailer->send($message);

        $this->io->success(sprintf('%s emails were successfully sent.', $sentMessages));
    }

    /**
     * {@inheritdoc}
     */
    protected function interact(InputInterface $input, OutputInterface $output)
    {
        foreach ($input->getOptions() as $option => $value) {
            if (null === $value) {
                $input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option))));
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function isEnabled()
    {
        return $this->getContainer()->has('mailer');
    }

    /**
     * Creates new message from input options.
     *
     * @param InputInterface $input An InputInterface instance
     *
     * @return \Swift_Message New message
     */
    private function createMessage(InputInterface $input)
    {
        $message = new \Swift_Message(
            $input->getOption('subject'),
            $input->getOption('body'),
            $input->getOption('content-type'),
            $input->getOption('charset')
        );
        $message->setFrom($input->getOption('from'));
        $message->setTo($input->getOption('to'));

        return $message;
    }
}

第 76 行:

throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $input->getOption('mailer')));

输出

php bin/控制台调试:容器

你知道我的问题可能来自哪里吗?

【问题讨论】:

  • 能否更新问题并添加NewEmailCommand.php代码?
  • 我更新了我的问题
  • bin/console debug:container 的输出是什么?
  • 我更新了我的问题
  • NewEmailCommand 的哪一行是第 76 行?如果它检查容器中的某些东西,显然没有配置在容器中,那么在这里询问而不是重新配置您的应用程序有什么意义?

标签: php symfony swiftmailer


【解决方案1】:

查看php bin/console debug:container列出的服务列表,没有名为swiftmailer.mailer.default的服务,请使用swiftmailer.mailerswiftmailer.mailer.no_reply

【讨论】:

    【解决方案2】:

    我解决了这个错误

    swiftmailer:
      default_mailer: default
      mailers:
        default:
          url: '%env(MAILER_URL)%'
          spool: { type: memory }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2012-05-23
      • 1970-01-01
      相关资源
      最近更新 更多