Merhaba mgsmus çok teşekkür ederim öncelikle
sorunumu çözdüm bunu belirtmek istiyorum vede tabiki sayenizde 🙂 oldu ve sizlerle bunu paylaşmak istiyorum belki benim gibi bu kurguyu zor anlayan veya yapmak isteyen olursa yardımcı olur umarım.
şimdi öncelikle sorunum şu şekildeydi:
projemde bir form doldurulduğunda, bunu hem mail hemde sms atmam gerekiyordu
sizlere de sormuştum daha önce https://laravel.gen.tr/d/3505-mail-gondermek-icin-event-listeners-queue-veya-jobs-queue sizde beni Notification olayına yönlendirdiniz tekrar teşekkür ederim. 🙂
gerçekten notification çok güzel ve sade bir sekilde yapılıyormuş event-listeners ile de yapılıyor ama bence gerek yok en azından benim bu kurgum için notification daha clear ve anlaşılır bir yapı oldu kesinlikle
şimci çözümü yazıyorum
- (çok sms ve mail olduğundan dolayı env QUEUE_CONNECTION=database kullandım)
- mailgun mail ayarlarımı yaptım
3 yeni bir Notification oluşturdum
app/Notifications/InvoicePaid.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\Channels\SmsChannel;
class InvoicePaid extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', SmsChannel::class];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
/**
* Get the sms representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toSms($notifiable)
{
}
}
- kendi sms sağlayıcımı ekledim notification custom channel olarak
app/Channels/SmsChannel.php
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class SmsChannel {
public function send($notifiable, Notification $notification)
{
$xml_body = '<sms>
<username>salih</username>
<password>ganifeoglu</password>
<header>Maho</header>
<validity>2880</validity>
<message>
<gsm>
<no>5325326322</no>
</gsm>
<msg><![CDATA[Hello Salih]]></msg>
</message>
</sms>';
$client = new \GuzzleHttp\Client();
$request_uri = 'http://api.mysmsprovider.com.tr:8080/api/v1';
$response = $client->request('POST', $request_uri, [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
'accept-encoding' => 'gzip, deflate'
],
'decode_content' => false,
'body' => $xml_body
]);
}
}
- Son adım app/Http/Controllers/HomeController.php
içerisine bunu ekledim
public function mail(Request $request)
{
$when = now()->addSecond(10);
User::find(1)->notify((new InvoicePaid)->delay($when));
}
bunu da route ekledim
Route::get('/mail', 'HomeController@mail');
htttp://mytest.lcl/mail dediğim zaman sms ve mail geliyor
var eksikler ama gercek projeme eklediğim de refactor edeceğim ama bu gercek çalışan bir örnek
selamlar 🙂