Php 更改Laravel 4中的邮件传输

Php 更改Laravel 4中的邮件传输,php,ubuntu,laravel,swiftmailer,laravel-4,Php,Ubuntu,Laravel,Swiftmailer,Laravel 4,似乎L4已硬编码为使用SMTP和Swiftmailer。如何将传输类型更改为sendmail?这并不像将一个传输换成另一个传输那样简单。正如您所说,传输是在illumb\Mail\MailServiceProvider中硬编码的 您可能希望使用自己的扩展L4邮件实现,在config/app.php Providers数组中交换它: 替换邮件提供程序的提供程序阵列: config/app.php ... 'providers' => array( 'SendmailServiceProv

似乎L4已硬编码为使用SMTP和Swiftmailer。如何将传输类型更改为sendmail?

这并不像将一个传输换成另一个传输那样简单。正如您所说,传输是在illumb\Mail\MailServiceProvider中硬编码的

您可能希望使用自己的扩展L4邮件实现,在config/app.php Providers数组中交换它:

  • 替换邮件提供程序的提供程序阵列:
config/app.php

...
'providers' => array(
'SendmailServiceProvider',
  • 将sendmail路径添加到二进制文件:
config/mail.php

'sendmail_path' => '/usr/bin/sendmail -bs',
  • 确保composer.json文件自动加载“来自应用程序/库的类映射:”
composer.json:

....    
"autoload": {
    "classmap": [
        "app/libraries",
....
  • 添加Sendmail服务提供商:
app/libraries/SendmailServiceProvider.php

<?php

use \Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Illuminate\Mail\Mailer as Mailer;
use Swift_SendmailTransport as SendmailTransport;

class SendmailServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerSwiftMailer();

        $this->app['mailer'] = $this->app->share(function($app)
        {
            // Once we have create the mailer instance, we will set a container instance
            // on the mailer. This allows us to resolve mailer classes via containers
            // for maximum testability on said classes instead of passing Closures.
            $mailer = new Mailer($app['view'], $app['swift.mailer']);

            $mailer->setLogger($app['log']);

            $mailer->setContainer($app);

            $from = $app['config']['mail.from'];

            // If a "from" address is set, we will set it on the mailer so that all mail
            // messages sent by the applications will utilize the same "from" address
            // on each one, which makes the developer's life a lot more convenient.
            if (is_array($from) and isset($from['address']))
            {
                $mailer->alwaysFrom($from['address'], $from['name']);
            }

            return $mailer;
        });
    }

    /**
     * Register the Swift Mailer instance.
     *
     * @return void
     */
    protected function registerSwiftMailer()
    {
        $config = $this->app['config']['mail'];

        $this->registerSwiftTransport($config);

        // Once we have the transporter registered, we will register the actual Swift
        // mailer instance, passing in the transport instances, which allows us to
        // override this transporter instances during app start-up if necessary.
        $this->app['swift.mailer'] = $this->app->share(function($app)
        {
            return new Swift_Mailer($app['swift.transport']);
        });
    }

    /**
     * Register the Swift Transport instance.
     *
     * @param  array  $config
     * @return void
     */
    protected function registerSwiftTransport($config)
    {
        $this->app['swift.transport'] = $this->app->share(function($app) use ($config)
        {
            extract($config);

            // The Swift SMTP transport instance will allow us to use any SMTP backend
            // for delivering mail such as Sendgrid, Amazon SMS, or a custom server
            // a developer has available. We will just pass this configured host.
            $transport = SendmailTransport::newInstance($sendmail_path);

            // Once we have the transport we will check for the presence of a username
            // and password. If we have it we will set the credentials on the Swift
            // transporter instance so that we'll properly authenticate delivery.
            if (isset($username))
            {
                $transport->setUsername($username);

                $transport->setPassword($password);
            }

            return $transport;
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('mailer', 'swift.mailer', 'swift.transport');
    }

}

这可不像换一个运输工具那么容易。正如您所说,传输是在illumb\Mail\MailServiceProvider中硬编码的

您可能希望使用自己的扩展L4邮件实现,在config/app.php Providers数组中交换它:

  • 替换邮件提供程序的提供程序阵列:
config/app.php

...
'providers' => array(
'SendmailServiceProvider',
  • 将sendmail路径添加到二进制文件:
config/mail.php

'sendmail_path' => '/usr/bin/sendmail -bs',
  • 确保composer.json文件自动加载“来自应用程序/库的类映射:”
composer.json:

....    
"autoload": {
    "classmap": [
        "app/libraries",
....
  • 添加Sendmail服务提供商:
app/libraries/SendmailServiceProvider.php

<?php

use \Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Illuminate\Mail\Mailer as Mailer;
use Swift_SendmailTransport as SendmailTransport;

class SendmailServiceProvider extends ServiceProvider {

    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerSwiftMailer();

        $this->app['mailer'] = $this->app->share(function($app)
        {
            // Once we have create the mailer instance, we will set a container instance
            // on the mailer. This allows us to resolve mailer classes via containers
            // for maximum testability on said classes instead of passing Closures.
            $mailer = new Mailer($app['view'], $app['swift.mailer']);

            $mailer->setLogger($app['log']);

            $mailer->setContainer($app);

            $from = $app['config']['mail.from'];

            // If a "from" address is set, we will set it on the mailer so that all mail
            // messages sent by the applications will utilize the same "from" address
            // on each one, which makes the developer's life a lot more convenient.
            if (is_array($from) and isset($from['address']))
            {
                $mailer->alwaysFrom($from['address'], $from['name']);
            }

            return $mailer;
        });
    }

    /**
     * Register the Swift Mailer instance.
     *
     * @return void
     */
    protected function registerSwiftMailer()
    {
        $config = $this->app['config']['mail'];

        $this->registerSwiftTransport($config);

        // Once we have the transporter registered, we will register the actual Swift
        // mailer instance, passing in the transport instances, which allows us to
        // override this transporter instances during app start-up if necessary.
        $this->app['swift.mailer'] = $this->app->share(function($app)
        {
            return new Swift_Mailer($app['swift.transport']);
        });
    }

    /**
     * Register the Swift Transport instance.
     *
     * @param  array  $config
     * @return void
     */
    protected function registerSwiftTransport($config)
    {
        $this->app['swift.transport'] = $this->app->share(function($app) use ($config)
        {
            extract($config);

            // The Swift SMTP transport instance will allow us to use any SMTP backend
            // for delivering mail such as Sendgrid, Amazon SMS, or a custom server
            // a developer has available. We will just pass this configured host.
            $transport = SendmailTransport::newInstance($sendmail_path);

            // Once we have the transport we will check for the presence of a username
            // and password. If we have it we will set the credentials on the Swift
            // transporter instance so that we'll properly authenticate delivery.
            if (isset($username))
            {
                $transport->setUsername($username);

                $transport->setPassword($password);
            }

            return $transport;
        });
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides()
    {
        return array('mailer', 'swift.mailer', 'swift.transport');
    }

}

不确定,但Taylor似乎在核心中插入了PHP邮件函数作为替代传输,如果您的用例感兴趣:不确定,但Taylor似乎在核心中插入了PHP邮件函数作为替代传输,如果您的用例感兴趣: