Skip to content

Instantly share code, notes, and snippets.

@nutch31
Created May 27, 2019 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nutch31/2674a776453a000eabb15c840cbc2f94 to your computer and use it in GitHub Desktop.
Save nutch31/2674a776453a000eabb15c840cbc2f94 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Events\UserRegistered;
class AlphaController extends Controller
{
public function testEmail()
{
// call our event here
event(new UserRegistered());
}
}
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
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\UserRegistered' => [
'App\Listeners\SendWelcomeEmail'
],
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
Route::get('/testEmail', 'AlphaController@testEmail');
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendFormalEmail extends Mailable
{
use Queueable, SerializesModels;
public $email_from;
public $name;
public $subject_name;
public $content;
public $file_name;
public $raw_data;
public $file_mime_type;
/**
* SendFormalEmail constructor.
* @param $email_from
* @param $name
* @param $subject_name
* @param $content
* @param $file_name
* @param $raw_data
* @param $file_mime_type
*/
public function __construct($email_from, $name, $subject_name, $content, $file_name, $raw_data, $file_mime_type)
{
$this->email_from = $email_from;
$this->name = $name;
$this->subject_name = $subject_name;
$this->content = $content;
$this->file_name = $file_name;
$this->raw_data = $raw_data;
$this->file_mime_type = $file_mime_type;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$mail = $this->from($this->email_from)
->view('email.sendCreateFormalEmail')
->subject($this->subject_name)
->with([
"name" => $this->name,
"subject_name" => $this->subject_name,
"content" => $this->content
]);
if(!empty($this->raw_data))
{
$mail->attachData($this->raw_data, $this->file_name, [
'mime' => $this->file_mime_type,
]);
}
return $mail;
}
}
<?php
namespace App\Listeners;
use App\Events\UserRegistered;
use App\Mail\SendFormalEmail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use DB;
class SendWelcomeEmail
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param UserRegistered $event
* @return void
*/
public function handle()
{
// Mail
$mail = Mail::to('nut@heroleads.com');
$mail->queue(new SendFormalEmail(
'alpha-noreply@heroleads.com',
'Nut Chantathab',
'TEST ACCOUNT REPORT',
'Message Content',
NULL,
NULL,
NULL
));
DB::table('formal_email')
->where('id', 161)
->update(['status' => 33]);
}
}
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class UserRegistered
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment