Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 覆盖Laravel 4.2中的邮件MailgunTransport类_Php_Laravel_Email_Laravel 4 - Fatal编程技术网

Php 覆盖Laravel 4.2中的邮件MailgunTransport类

Php 覆盖Laravel 4.2中的邮件MailgunTransport类,php,laravel,email,laravel-4,Php,Laravel,Email,Laravel 4,使用邮枪作为邮件驱动程序,我面临的问题是,盲目复写无法正常工作,因为它向所有收件人显示了所有地址。我找到了一个可以解决问题的修复程序,但它需要在vendor/laravel/framework/src/illighte/Mail/Transport/MailgunTransport.php处编辑MailgunTransport.php文件。我不想更改供应商文件夹中的文件,所以我正在尝试扩展MailgunTransport类 我创建了一个名为app/custom/extensions的文件夹,其中

使用邮枪作为邮件驱动程序,我面临的问题是,盲目复写无法正常工作,因为它向所有收件人显示了所有地址。我找到了一个可以解决问题的修复程序,但它需要在vendor/laravel/framework/src/illighte/Mail/Transport/MailgunTransport.php处编辑MailgunTransport.php文件。我不想更改供应商文件夹中的文件,所以我正在尝试扩展MailgunTransport类

我创建了一个名为app/custom/extensions的文件夹,其中包含两个文件CustomMailServiceProvider

<?php namespace custom\extensions;

use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use custom\extensions\CustomMailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;

class CustomMailServiceProvider extends \Illuminate\Mail\MailServiceProvider {

}
我已经将原来的“Illumb\Mail\MailServiceProvider”替换为“custom\extensions\CustomMailServiceProvider”

'providers' => array(
    ...
    //'Illuminate\Mail\MailServiceProvider',
    'custom\extensions\CustomMailServiceProvider',
    ...
),
然而,在这一点上,我不知道如何调用邮件功能。如果我尝试使用邮件外观,它将使用MailgunTransport.php中的原始代码

<?php namespace custom\extensions;

class CustomMailgunTransport extends Illuminate\Mail\Transport\MailgunTransport {
    /**
     * {@inheritdoc}
     */
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {        
        $client = $this->getHttpClient();
        $to = $this->getTo($message);
        $message->setBcc([]);

        $client->post($this->url, ['auth' => ['api', $this->key],
            'body' => [
                'to' => $to,
                'message' => new PostFile('message', (string) $message),
            ],
        ]);
    }
}

我需要创建自定义外观吗?如果是的话。。。我怎样才能做到?或者上面的代码有问题吗?有没有办法只扩展MailgunTransport.php而不创建CustomMailServiceProvider?

我通过在CustomMailServiceProvider中包含registerMailgunTransport方法并引用新的CustomMailgunTransport解决了这个问题

CustomMailServiceProvider

<?php namespace custom\extensions;

use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use custom\extensions\CustomMailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;

class CustomMailServiceProvider extends \Illuminate\Mail\MailServiceProvider {
/**
 * Register the Mailgun Swift Transport instance.
 *
 * @param  array  $config
 * @return void
 */
    protected function registerMailgunTransport($config)
    {
        $mailgun = $this->app['config']->get('services.mailgun', array());

        $this->app->bindShared('swift.transport', function() use ($mailgun)
        {
            return new CustomMailgunTransport($mailgun['secret'], $mailgun['domain']);
        });
    }
}

<?php namespace custom\extensions;

use Swift_Mailer;
use Illuminate\Support\ServiceProvider;
use Swift_SmtpTransport as SmtpTransport;
use Swift_MailTransport as MailTransport;
use Illuminate\Mail\Transport\LogTransport;
use custom\extensions\CustomMailgunTransport;
use Illuminate\Mail\Transport\MandrillTransport;
use Swift_SendmailTransport as SendmailTransport;

class CustomMailServiceProvider extends \Illuminate\Mail\MailServiceProvider {
/**
 * Register the Mailgun Swift Transport instance.
 *
 * @param  array  $config
 * @return void
 */
    protected function registerMailgunTransport($config)
    {
        $mailgun = $this->app['config']->get('services.mailgun', array());

        $this->app->bindShared('swift.transport', function() use ($mailgun)
        {
            return new CustomMailgunTransport($mailgun['secret'], $mailgun['domain']);
        });
    }
}
<?php namespace custom\extensions;

use Swift_Transport;
use GuzzleHttp\Client;
use Swift_Mime_Message;
use GuzzleHttp\Post\PostFile;
use Swift_Events_EventListener;

class CustomMailgunTransport extends \Illuminate\Mail\Transport\MailgunTransport {
    /**
     * {@inheritdoc}
    */
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {        
        $client = $this->getHttpClient();
        $to = $this->getTo($message);
        $message->setBcc([]);

        $client->post($this->url, ['auth' => ['api', $this->key],
            'body' => [
                'to' => $to,
                'message' => new PostFile('message', (string) $message),
            ],
        ]);
     }
}