I'm trying to generate a notification email to the webmaster when there's a new registration on the site I'm building.
I have a mail class called SignedUp at App\Mail\SignedUp.php.
In the Illuminate/Foundation/Auth/RegistersUser file, I have the following:
namespace Illuminate\Foundation\Auth;
use App\Organization;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
use App\Mail\SignedUp;
trait RegistersUsers
{
use RedirectsUsers;
/**
* Show the application registration form.
*
* #return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
$organizations = Organization::all();
return view('auth.register', compact('organizations'));
}
/**
* Handle a registration request for the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
Mail::to('example#example.com')->send(new SignedUp($user));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
When I register a new user, I get this message:
Class 'Illuminate\Foundation\Auth\Mail' not found
if I change use App\Mail\SignedUp; to use App\Mail;, the error is still: Class 'Illuminate\Foundation\Auth\Mail' not found
use Mail; gives me Illuminate\Foundation\Auth\SignedUp' not found
use App\Mail\SignedUp; gives me Class 'Illuminate\Foundation\Auth\Mail' not found
Not attempting to import the class at all, and instead changing my statement to: \Mail::to('example#example.com')->send(new SignedUp($user));, I get Class 'Illuminate\Foundation\Auth\SignedUp' not found
A cannot figure out how to get it to find my mail class.
I'm just guessing and shooting in the dark - can anyone see what I'm doing wrong?
Here is my mail class file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SignedUp extends Mailable
{
use Queueable, SerializesModels;
public $url = 'https://av-cuauhtemoc.org/users';
public $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('example#example.com')->view('emails.new-registration');
}
}
I finally stumbled upon the answer.
I also needed to import the use Illuminate\Support\Facades\Mail; class.
So this works:
use App\Mail\Signedup;
use Illuminate\Support\Facades\Mail;
first of all you shouldn't change Illuminate/Foundation/Auth/RegistersUser file, instead you have to overwrite this method in your RegisterController:
use App\Mail\SignedUp;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Mail;
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
Mail::to('example#example.com')->send(new SignedUp($user));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
Related
I have a problem to solve. However, this problem came to me from sentry.io. So I didn't see the problem directly.
The problem is as follows: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.
enter image description here
The problem with sentry.io is in the picture. However, as far as I understand, the problem is not directly caused by this file. In other words, the problem is in sending mail, so since email is used here, it shows the problem here. I have not used services such as laravel mailing before, so I have no idea right now.
I can share with you the necessary codes, files, etc., so that we can better understand the problem.
Send Mail
Mail::to($user->email)->send(new NothingListenedFor3Days($user));
Mail Class
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NothingListenedFor3Days extends Mailable
{
use Queueable, SerializesModels;
protected $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('Your Subject')
->view('Your blade file path')->with('user',$this->user);
}
}
For reference link
#Ali Raza
I made both files as I shared below. Are there any mistakes I've made?
EmailCronJobController.php
Mail::to($user->email)->send(new NothingListenedFor3Days($user));
NothingListenedFor3Days.php
namespace App\Mail\Listener;
use App\Course;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NothingListenedFor3Days extends Mailable
{
use Queueable, SerializesModels;
public $listener;
public $random_contents;
public $fromEmail = 'listener#omnicourse.io';
public $fromName = 'Omnicourse';
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $listener)
{
$this->listener = $listener;
$this->random_contents = Course::all()->random(10);
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from($this->fromEmail,$this->fromName)
->view('email.listener.nothing_listened_for_3_days')->with('user',$this->listener);
->text('email.listener.nothing_listened_for_3_days_plain')
->subject("Let's move ahead together");
}
}
i am using design patterns repository and i wrote every thing coorect but i have an error returns
Target [App\Repository\categoryRepositoryInterface] is not instantiable while building [App\Http\Controllers\CategoryController]. i dont know why although i wrote every thing correct please help
here is my code
my config/app
App\Providers\RepositoryServiceProvider::class,
and my categorycontroller
<?php
namespace App\Http\Controllers;
use App\Repository\categoryRepositoryInterface;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
private categoryRepositoryInterface $categoryRepository;
public function __construct(categoryRepositoryInterface $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
and my repositoryinterface
<?php
namespace App\Repository;
use Illuminate\Http\Request;
interface categoryRepositoryInterface
{
public function createCategory(Request $request);
public function validation(Request $request);
}
and my category repository
<?php
namespace App\Repository;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class categoryRepository implements categoryRepositoryInterface{
public function validation(Request $request){
$validation = Validator::make($request->all(),[
'name' => 'required',
]);
if($validation->fails()){
return response()->json([
'status' => 400,
'errors' => $validation->errors(),
]);
}
}
public function createCategory(Request $request)
{
$category = new Category();
$category->name = $request->name;
$category->save();
}
}
and my servicerepositoryprovider
<?php
namespace App\Providers;
use App\Repository\CategoryRepository;
use App\Repository\CategoryRepositoryInterface;
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(CategoryRepositoryInterface::class, CategoryRepository::class);
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
You have typos in your RepositoryServiceProvier
use App\Repository\categoryRepository; //First alphabet should be lower case
use App\Repository\categoryRepositoryInterface; //First alphabet should be lower case
use Illuminate\Support\ServiceProvider;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//First alphabet should be lower case as per your class and interface names
$this->app->bind(categoryRepositoryInterface::class, categoryRepository::class);
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
Would like to give you a piece of advice. When developing with Laravel it would be easier for you to follow Laravel's naming conventions - Laravel conventions overall. Following conventions will help you better understand examples on the internet as well.
Whatever you decide remember to follow them with consistency. In this particular case you are naming classes in camel case starting with small alphabet so when importing via use statements you need to stick to that.
I am able to fire dummy events from the Pusher debug console and my client side is able to pick them up. But when I try to fire the event from my UserController nothing seems to happen.
Here is my Event class
<?php
namespace App\Events;
use App\Events\Event;
use App\Player;
use App\Product;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewPurchase extends Event implements ShouldBroadcast
{
use SerializesModels;
public $product;
/**
* Create a new event instance.
*
* #param Product $product
* #return void
*/
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return [Player::where('user_id', $this->product->seller_id)->first()->group_id];
}
}
Here is my listener, which doesn't have anything because I want everything to be process client side
<?php
namespace App\Listeners;
use App\Events\NewPurchase;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class NewPurchaseListener implements ShouldQueue
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param NewPurchase $event
* #return void
*/
public function handle(NewPurchase $event)
{
//
}
}
Here is my .env
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=858577
PUSHER_APP_KEY=ec160cc0a1ca15e463f4
PUSHER_APP_SECRET=
QUEUE_DRIVER=sync
Here is my event service provider
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\NewPurchase' => [
'App\Listeners\NewPurchaseListener',
],
];
And here is where the event is fired
Event::fire(new NewPurchase($product));
My issue was that my version of Laravel hadn't been updated from the previous developers. Therefore the version of Pusher I had wasn't compatible at first with the version of Laravel I was using. I have tweaked this now and it works.
I Am sending emails in Laravel Queue. While using the send method, as shown here
Mail::to($userSocial->getEmail())->send(new WelcomeEmail('1234567', "haha", "Makamu"));
my email is delivered to my inbox. However when i switch to queue like below
Mail::to($userSocial->getEmail())->queue(new WelcomeEmail('1234567', "haha", "Makamu"));
I also used this method
SendEmailSocialReg::dispatch('12345678', "haha", "Makamu");
and monitor via queue:listen i am get the processing. then processed message. No error however.
What could be wrong?
my WelcomeEmail
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $password;
public $client_name;
public $client_email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($password, $email, $name)
{
$this->password = $password;
$this->client_name = $name;
$this->client_email = $email;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('email.auto');
}
}
Your WelcomeEmail class must return markdown() at build() function like this:
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('email.auto');
}
Then the queue worker must be executed with this command:
php artisan queue:work --queue=default
I'm getting the following error:
FatalErrorException in EloquentVehicle.php line 30: Call to a member
function groupBy() on null
I have the following code:
<?php
namespace App\Project\Frontend\Repo\Vehicle;
use Illuminate\Database\Eloquent\Model;
class EloquentVehicle implements VehicleInterface
{
protected $vehicle;
/**
* EloquentVehicle constructor.
*
* #param Model $vehicle
*/
public function __construct(
Model $vehicle
)
{
$this->$vehicle = $vehicle;
}
/**
* Fetch unique makes
*
* #return mixed
*/
public function fetchMakes()
{
return $this->vehicle->groupBy(array('make'))
->orderBy('make', 'asc')
->get();
}
}
I've checked Illuminate\Database\Eloquent\Model for the method which is obviously not there, but I don't know what I should be adding to my class so that I can use the groupBy method. The laravel docs say the method exists.
UPDATE: Apparently I can't typehint an abstract class. I don't know how else I should be going about using Eloquent to retrieve records. If it helps, below is the code I have for registering the classes to the service container
<?php
namespace App\Providers;
use App\Vehicle;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Support\ServiceProvider;
class RepoServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
$this->app->bind('App\Project\Frontend\Repo\Vehicle\VehicleInterface', function($app)
{
return new EloquentVehicle(
new Vehicle
);
});
}
}
I just found my mistake and quite literally lay my face in my palms.
This
$this->$vehicle = $vehicle;
should be this
$this->vehicle = $vehicle;