【问题标题】:swiftmailer configuration for more than one accounts in Symfony 2Symfony 2 中多个帐户的 swiftmailer 配置
【发布时间】:2013-05-04 16:46:40
【问题描述】:

我使用gmail发送邮件,所以我像这样配置'config.yml'

swiftmailer:
transport: %mailer_transport%
encryption: %mailer_encryption%
auth_mode: %mailer_auth%
host:      %mailer_host%
username:  %mailer_user%
password:  %mailer_password%

‘parameters.yml’这样的

mailer_transport:  smtp
mailer_encryption: ssl
mailer_auth:       login
mailer_host:       smtp.gmail.com
mailer_user:       lee@gmail.com
mailer_password:   ******

现在我想使用更多的邮件帐户来发送不同目标的邮件。 eg:使用lee@gmail.com 发送欢迎邮件;使用lee1@gmail.com 发送邮件重置密码。

我应该如何配置swiftmailer?

【问题讨论】:

    标签: symfony smtp swiftmailer


    【解决方案1】:

    管理邮件配置的 SwiftmailerBundle 只允许您设置一个默认配置。但是,设置其他人非常简单。只需直接使用 Swiftmailer 或使用其他配置定义您自己的邮件程序类。

    /**
     * Gets the 'mailer' service.
     *
     * This service is shared.
     * This method always returns the same instance of the service.
     *
     * @return Swift_Mailer A Swift_Mailer instance.
     */
    protected function getMailerService()
    {
        return $this->services['mailer'] = new \Swift_Mailer($this->get('swiftmailer.transport'));
    }
    

    您可以根据需要使用不同的配置定义许多服务。例如,看下面的例子。

    <service id="mysecond.transport.smtp" class="%swiftmailer.transport.smtp.class%" public="false">
        <argument type="service" id="swiftmailer.transport.buffer" />
        <argument type="collection">
            <argument type="service" id="swiftmailer.transport.authhandler" />
        </argument>
        <argument type="service" id="swiftmailer.transport.eventdispatcher" />
    
        <call method="setHost"><argument>%mysecond.transport.smtp.host%</argument></call>
        <call method="setPort"><argument>%mysecond.transport.smtp.port%</argument></call>
        <call method="setEncryption"><argument>%mysecond.transport.smtp.encryption%</argument></call>
        <call method="setUsername"><argument>%mysecond.transport.smtp.username%</argument></call>
        <call method="setPassword"><argument>%mysecond.transport.smtp.password%</argument></call>
        <call method="setAuthMode"><argument>%mysecond.transport.smtp.auth_mode%</argument></call>
        <call method="setTimeout"><argument>%mysecond.transport.smtp.timeout%</argument></call>
        <call method="setSourceIp"><argument>%mysecond.transport.smtp.source_ip%</argument></call>
    </service>
    

    然后,在您的代码中,您会执行类似的操作。

    $mySecondMailer = new \Swift_Mailer($this->get('mysecond.transport.smtp'));
    

    这应该可以解决问题。

    【讨论】:

      【解决方案2】:

      如果您使用的是 Swiftmailer 2.3.3,您可以让一切变得简单:

      在parameters.yml中添加:

      mailer2_transport: smtp
      mailer2_encryption: ssl
      mailer2_auth_mode: login
      mailer2_host: smtp.gmail.com
      mailer2_user: your@gmail.com
      mailer2_password: *******
      

      在 config.yml 中进行更改:

      swiftmailer:
          default_mailer: mailer
          mailers:
              mailer:
                  transport: %mailer_transport%
                  host:      %mailer_host%
                  username:  %mailer_user%
                  password:  %mailer_password%
                  encryption: %mailer_encryption%
                  auth_mode: %mailer_auth_mode%
              mailer2:
                  transport: %mailer2_transport%
                  host:      %mailer2_host%
                  username:  %mailer2_user%
                  password:  %mailer2_password%
                  encryption: %mailer2_encryption%
                  auth_mode: %mailer2_auth_mode%
      

      如果你写在代码中:

      $mailer = $this->get('swiftmailer.mailer.mailer2');
      

      您将从您的部分获得设置;

      如果你写:

      $mailer = $this->get('swiftmailer.mailer.default');
      

      $mailer = $this->get('mailer'); // default configuration
      

      您将使用默认部分的设置;

      【讨论】:

      • 请不要共享身份​​验证处理程序。如果您在此设置中使用两个邮件程序,使用不同的用户名/密码,这将不起作用。见github.com/symfony/SwiftmailerBundle/pull/70github.com/symfony/SwiftmailerBundle/issues/73
      • 只是想指出你必须有一个名为'default'的'default'邮件,否则会为你创建一个虚拟的'default'邮件,它将被用作默认值。我相信这是一个错误。
      • 由 rolandow 识别的身份验证处理程序问题已在 Swiftmailer 中得到修复。
      • 请问如何使用 symfony 4 应用此响应?
      【解决方案3】:

      这将是您的配置文件的样子。

      app/config/config.yml

       swiftmailer:
          default_mailer: second_mailer
          mailers:
              first_mailer:
                  # ...
              second_mailer:
                  # ...
      

      现在,您可以按以下方式使用任何邮件程序:

      // returns the first mailer
      $container->get('swiftmailer.mailer.first_mailer');
      
      // also returns the second mailer since it is the default mailer
      $container->get('swiftmailer.mailer');
      
      // returns the second mailer
      $container->get('swiftmailer.mailer.second_mailer');
      

      更多详情见:symfony documentry

      【讨论】:

        【解决方案4】:

        这应该可以:from the documentation

        require_once 'lib/swift_required.php';
        
        // Create the Transport
        $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
          ->setUsername('your username')
          ->setPassword('your password')
          ;
        
        /*
        You could alternatively use a different transport such as Sendmail or Mail:
        
        // Sendmail
        $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
        
        // Mail
        $transport = Swift_MailTransport::newInstance();
        */
        
        // Create the Mailer using your created Transport
        $mailer = Swift_Mailer::newInstance($transport);
        
        // Create a message
        $message = Swift_Message::newInstance('Wonderful Subject')
          ->setFrom(array('john@doe.com' => 'John Doe'))
          ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
          ->setBody('Here is the message itself')
          ;
        
        // Send the message
        $result = $mailer->send($message);
        

        【讨论】:

          猜你喜欢
          • 2019-11-02
          • 2023-03-21
          • 2012-03-08
          • 2013-06-28
          • 2019-09-17
          • 2011-03-14
          • 2012-04-18
          相关资源
          最近更新 更多