2016-10-03 61 views
3

使用mailgun作为邮件驱动程序我面临的问题是,密件抄送不能正常工作,因为它向所有收件人显示所有地址。我发现了一个可以解决问题的修复程序,但它需要编辑vendor/laravel/framework/src/Illuminate/Mail/Transport/MailgunTransport.php中的MailgunTransport.php文件。我不想改变文件的文件夹的供应商,所以我想延长该MailgunTransport类...在Laravel 4.2中覆盖邮件MailgunTransport类

我创建了一个文件夹,名为app /自定义/扩展有两个文件,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 { 

} 

...和CustomMailgunTransport.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), 
      ], 
     ]); 
    } 
} 

CustomMailServiceProvider.php不重新定义任何原始的方法,但改变从原来的MailgunTransport定制\分机\ CustomMailgunTransport呼叫。

我已经装在composer.json类映射的新文件的应用程序/自定义/扩展目录...

{ 
    ... 
    "autoload": { 
     "classmap": [ 
      ... 
      "app/custom/extensions"     
     ],  
    }, 
    ... 
} 

而且我已经换了原来的“照亮\邮件\ MailServiceProvider”到“自定义\ extensions \ CustomMailServiceProvider'...

'providers' => array(
    ... 
    //'Illuminate\Mail\MailServiceProvider', 
    'custom\extensions\CustomMailServiceProvider', 
    ... 
), 

但是,在这一点上我不知道如何调用邮件功能。如果我尝试使用Mail facade,它使用来自MailgunTransport.php的原始代码

是否需要创建自定义外观?如果是的话......我怎么能做到这一点?或者上面的代码有问题吗?有没有什么办法可以在不创建CustomMailServiceProvider的情况下只扩展MailgunTransport.php?

回答

0

我通过在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']); 
     }); 
    } 
} 

CustomMailgunTransport

<?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), 
      ], 
     ]); 
    } 
}