Symfony:如何使用多个邮件程序?

Symfony : How to use multiple mailers?

几天来我尝试配置两个邮件程序的使用,尽管文档我不能。我目前有这段代码: 在 swiftmailer.yaml 中:

swiftmailer:
    default_mailer: first_mailer
    mailers:
        first_mailer:
            url: '%env(MAILER_URL)%'
        second_mailer:
            url: '%env(SECOND_MAILER_URL)%'

在 .env 中:

MAILER_URL=gmail://user:mdp@localhost
SECOND_MAILER_URL=gmail://user:mdp@localhost

我无法在我的控制器中使用它们。我应该怎么做? 有了这个 ? :

$container->get('swiftmailer.mailer.second_mailer');

如果可以,我该如何实现?谢谢。

根据 Swiftmailer 文档:

Using Multiple Mailers

当使用自动装配时,只有当 type-hinting 某些参数与 \Swift_Mailer class 时才会注入默认邮件程序。

如果您需要在某些服务中注入不同的邮件程序,您还应该使用绑定功能:

# config/services.yaml
services:
    _defaults:
        bind:
            # this injects the second mailer when type-hinting constructor arguments with \Swift_Mailer
            \Swift_Mailer: '@swiftmailer.mailer.second_mailer'
            # this injects the second mailer when a service constructor argument is called $specialMailer
            $specialMailer: '@swiftmailer.mailer.second_mailer'

    App\Some\Service:
        # this injects the second mailer only for this argument of this service
        $differentMailer: '@swiftmailer.mailer.second_mailer'

好的,抱歉,这是我的代码:

#config/service.yaml

services:
# default configuration for services in *this* file
_defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    bind:
        # this injects the second mailer when type-hinting constructor arguments with \Swift_Mailer
        \Swift_Mailer: '@swiftmailer.mailer.second_mailer'
        # this injects the second mailer when a service constructor argument is called $specialMailer
        $specialMailer: '@swiftmailer.mailer.second_mailer'

    App\Some\Service:
    # this injects the second mailer only for this argument of this service
    $differentMailer: '@swiftmailer.mailer.second_mailer'

#.env

MAILER_URL=gmail://user:password@localhost
SECOND_MAILER_URL=gmail://user:password@localhost

这是 swiftmailer :

swiftmailer:
   default_mailer: first_mailer
   mailers:
       first_mailer:
           url: '%env(MAILER_URL)%'
       second_mailer:
           url: '%env(SECOND_MAILER_URL)%'

$mailer = $container->get('swiftmailer.mailer.second_mailer'); 在我的控制器中。

错误:Invalid service "App\Some\Service": class "App\Some\Service" does not exist.

Connection could not be established with host localhost [Aucune connexion n�a pu �tre �tablie car l�ordinateur cible l�a express�ment refus�e. #10061]

邮件发送的很好

我在尝试使用 SwiftMailer 在 Symfony 4 中设置多个邮件帐户时遇到了类似的问题。奇怪的是,当我切换到 "prod" 模式时,问题就消失了。

为了让它在所有环境中工作,我是这样设置的

swiftmailer.yaml

swiftmailer:
default_mailer: mailer
mailers:
    mailer_alerts:
        url: '%env(MAILER_ALERTS_URL)%'
    mailer_reports:
        url: '%env(MAILER_ALERTS_URL)%'

services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
            $AlertsMailer: '@swiftmailer.mailer.mailer_alerts'
            $ReportsMailer: '@swiftmailer.mailer.mailer_reports'

设置您的服务 class 以注入依赖项,如下所示:

<?php
namespace App\Service;

class MessagingService{
    private $mailer_alerts;
    private $mailer_reports;

    public function __construct($AlertsMailer, $ReportsMailer){
        $this->mailer_alerts  = $AlertsMailer;
        $this->mailer_reports = $ReportsMailer;
    }

    public function sendAlertEmail(...){
        $message = (new \Swift_Message($emailSubject))
        ...

        $this->mailer_alerts  ->send($message);
    }

    public function sendReportEmail(...){
        $message = (new \Swift_Message($emailSubject))
        ...
        this->mailer_reports->send($message);
    }
}

控制器:

public function compute(Request $request, MessagingService $msgServ){
    ...
    $mailResponse = $msgServ->sendAlertEmail(...);
    ...
    return new APIResponse($statusCode, $statusMsg, $mailResponse);
}

希望对您有所帮助。我仍然不确定为什么将邮件服务与 \Swift_Mailer 绑定在开发环境中不起作用。好像不管怎么用默认的localhost:25

 \Swift_Mailer: '@swiftmailer.mailer.second_mailer'