有没有办法在Laravel 4中使用邮件功能(或任何其他功能)从Mandrill获得响应?
使用下面的代码,邮件发送正常,但只返回null:
$response = Mail::send('emails.test', [], function($message)
{
$message->to('test@email.com')->subject('test email');
});
dd($response);
我尝试使用smtp驱动程序和Mandrill驱动程序,但它没有任何区别
答案 0 :(得分:1)
在记录从Mandrill发送的事件中提取_id
时也遇到了一些问题。
为Laravel 5.7创建了一种解决方法;
创建CustomMailServiceProvider
<?php
// app/Providers/CustomMailServiceProvider.php
namespace App\Providers;
use App\Misc\Transport\CustomMandrillTransport;
use Swift_Mailer;
use Illuminate\Support\Arr;
use GuzzleHttp\Client as HttpClient;
class CustomMailServiceProvider extends \Illuminate\Mail\MailServiceProvider {
public function register(){
parent::register();
$this->registerMandrillMailer();
}
/**
* Register the CustomMandrill Swift Transport instance.
*
* @param array $config
* @return void
*/
protected function registerMandrillMailer()
{
if ($this->app['config']['mail.driver'] == 'mandrill') {
$this->app->singleton('swift.mailer', function ($app) {
$mandrillConfig = $app['config']->get('services.mandrill', []);
return new Swift_Mailer(new CustomMandrillTransport( $this->guzzle($mandrillConfig), $mandrillConfig['secret']));
});
}
}
/**
* Get a fresh Guzzle HTTP client instance.
*
* @param array $config
* @return \GuzzleHttp\Client
*/
protected function guzzle($config)
{
return new HttpClient(Arr::add(
$config['guzzle'] ?? [], 'connect_timeout', 60
));
}
}
创建CustomMandrillTransport
<?php
// app/Misc/Transport/CustomMandrillTransport.php
namespace App\Misc\Transport;
use Swift_Mime_SimpleMessage;
class CustomMandrillTransport extends \Illuminate\Mail\Transport\MandrillTransport {
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$response = $this->client->request('POST', 'https://mandrillapp.com/api/1.0/messages/send-raw.json', [
'form_params' => [
'key' => $this->key,
'to' => $this->getTo($message),
'raw_message' => $message->toString(),
'async' => true,
],
]);
// Lets replace body by actually something useful -_-
$message->setBody((string)$response->getBody());
$this->sendPerformed($message);
return $this->numberOfRecipients($message);
}
}
创建电子邮件侦听器
<?php
// app/Listeners/EmailSentListener.php
namespace App\Listeners;
use Illuminate\Mail\Events\MessageSent;
use Mail;
class EmailSentListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param \App\Events\OrderShipped $event
* @return void
*/
public function handle(MessageSent $event)
{
$mandrillBody = $event->message->getBody(); // [{"email":"xxxxx@gmail.com","status":"queued","_id":"19219cfd3e0e4133aed48214ebb4ed71"}]
}
}
在config / app.php中注释原始
// Illuminate\Mail\MailServiceProvider::class,
并添加您自己的:
App\Providers\CustomMailServiceProvider::class,
并确保收听发送的事件
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
'Illuminate\Mail\Events\MessageSent' => [
'App\Listeners\EmailSentListener',
]
];
}
希望这可以帮助更多人
答案 1 :(得分:0)
似乎没有。 MandrillTransport :: send不返回任何内容,它不会公开HttpClient。