Laravel Mail Class Swift_Message does not exist - php

I'm trying to send email in laravel 5.5. I did everything as in https://laravel.com/docs/5.5/mail, but I get this error:
Class Swift_Message does not exist
I created mailable using
php artisan make:mail OrderShipped
and edited the mailable
public function build()
{
return $this->from('example#example.com')
->view('emails.orders.shipped');
}
anc created nearly empty view
<div>Hi </div>
and sending like this
<?php
namespace App\Http\Controllers;
use App\Order;
use App\Mail\OrderShipped;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
/**
* Ship the given order.
*
* #param Request $request
* #param int $orderId
* #return Response
*/
public function ship(Request $request, $orderId)
{
$order = Order::findOrFail($orderId);
// Ship order...
Mail::to($request->user())->send(new OrderShipped($order));
}
}

Related

Too few arguments to function, 0 passed in and exactly 1 expected when using Mail

I'm trying to send a simple email using the Mailable class. I've followed this tutorial online to the tee and I'm still getting this error. I have no idea what the problem could be because everything seems fine and I've followed the tutorial step for step.
What am I doing wrong?
TestMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('Test Mail from Duke')->view('emails.TestMail');
}
}
MailController
<?php
namespace App\Http\Controllers;
use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class MailController extends Controller
{
//
public function sendEmail()
{
$details = [
'title' => 'The Title',
'body' => 'The body should be fine',
];
Mail::to("sirduketylo#gmail.com")->send(new TestMail());
return "Email Sent";
}
}
The route
<?php
use App\Http\Controllers\MailController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/send', [MailController::class, 'sendEmail']);
The TestMail object expects a $details parameter passed in its constructor, yet none was supplied.
(MailController)
Instead of:❌
Mail::to("sirduketylo#gmail.com")->send(new TestMail());
Use this:✅
Mail::to("sirduketylo#gmail.com")->send(new TestMail($details));
Addendum
I suppose you intended to pass the $details to the View. Hence:
(TestMail.php)
// ...
return $this
->subject('Test Mail from Duke')
->view('emails.TestMail')
->with("details", $this->details);
// ...

RateLimited middleware not working in Laravel 9 Job

I'm trying to send email notifications to my app users, limiting the number of emails to only 2 per minute, so as not to breach my email provider's allowed sending rate. So in MailingController.php I have:
<?php
namespace App\Http\Controllers;
use App\Jobs\SendMessages;
use Illuminate\Http\Request;
class MailingController extends Controller
{
public function send(Request $request) {
SendMessages::dispatch();
return redirect()->back()->withSuccess('Successful operation.');
}
}
SendMessages.php content is:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Jobs\Middleware\RateLimited;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Redis;
class SendMessages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 7200;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$users = User::all();
foreach ($users as $user) {
Log::debug('SendMessages#handle() ' . $user->email);
$user->notify(new VerifyEmail);
}
}
public function middleware()
{
return [new RateLimited];
}
}
And finally, RateLimited.php content is:
<?php
namespace App\Jobs\Middleware;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class RateLimited
{
/**
* Process the queued job.
*
* #param mixed $job
* #param callable $next
* #return mixed
*/
public function handle($job, $next)
{
Redis::throttle('key')
->block(0)->allow(1)->every(30)
->then(function () use ($job, $next) {
// Lock obtained...
Log::debug('RateLimited');
$next($job);
}, function () use ($job) {
// Could not obtain lock...
$job->release(16);
});
}
}
All these are copied and pasted from a Laravel 8 application that works perfectly fine, sending one email every 30 seconds as expected, but in my brand new Laravel 9 application the same code no longer works, and all email notifications are sent at once. I know I must be missing something, but have ran out of ideas. Any help would be greatly appreciated.
Thank you.

Too few arguments to function App\Mail\OrderShipped::__construct() - Laravel 9

I searched for sending email, and I have no idea why that code isn't working. So I tried to create a theme for auto-send emails for users with password reset or other stuff.
I found out that I can create in mail.php all secrets for the email account, and that would work. And all things are working correctly to the moment that I was trying to add some variables to the email thru Mailable.
MailController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\OrderShipped;
use App\Models\User;
class MailController extends Controller
{
public function test(Request $request)
{
$user = User::find(Auth::user()->id);
Mail::to($user->email)->send(new OrderShipped($user));
return 'tak';
}
}
web.php
Route::get('/test', [MailController::class, 'test']);
OrderShipped.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
class OrderShipped extends Mailable
{
use Queueable, SerializesModels;
public $username;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($username)
{
// $this->username = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('snkrsdeals.pl#gmail.com', 'Test')
->view('emails.basic');
}
}
After all I've got an error like:
Too few arguments to function App\Mail\OrderShipped::__construct(), 0 passed in laravel->serializable-closure://function () { \Illuminate\Support\Facades\Mail::to(\Auth::user())->send(new \App\Mail\OrderShipped); return \view('welcome'); } on line 3 and exactly 1 expected
and laravel made this line red in Exeption Handler:
public function __construct($username)
Any ideas what's wrong? In Laravel 9 docks was a very similiar example.
Try this in MailController.php
change the code $user to $username in MailController.php test class
public function test(Request $request)
{
$user = User::find(Auth::user()->id);
Mail::to($user->email)->send(new OrderShipped($username));
return 'tak';
}

Laravel job ModelNotFoundException when dispatching from controller and running

I'm working with one of my Laravel 8 API project's that I've built where a user is able to add a domain to their account. The domain needs to be crawled to obtain expiration dates etc and is available in the user's account.
This process takes some time and so I've extracted the logic that fetches the domain data into a job called DomainExpiryChecker.
When a new domain is added, a job is dispatched, but may not execute straight away, and in some cases the user may even delete the domain before the job runs.
For some reason, even after dispatching my job after a record has been made, I'm getting the following failed job error and I'm not sure what I'm missing:
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Domains]
My controller where the job is dispatched resembles the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use App\Models\Domains;
use Carbon\Carbon;
use App\Jobs\DomainExpiryChecker;
class DomainsController extends Controller
{
/**
* Instantiate a new DomainsController instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Add a new domain
*
* #param Request $request
* #return Response
*/
public function add(Request $request)
{
$domain = new Domains();
$domain->domain = '';
$domain->crawled = 'pending';
$domain->has_valid_ssl = false;
$domain->user_id = Auth::id();
$domain->save();
// on-demand
if ($domain) {
DomainExpiryChecker::dispatch($domain)->onQueue('on-demand-runs-now');
}
// everything went okay!
return response()->json(['success' => true, 'message' => "Added $domain_name successfully, please allow between 24 and 48 hours for crawling"], 201);
}
}
Whilst my job looks like the following:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Client;
use App\Models\Settings;
use App\Models\Domains;
use Carbon\Carbon;
class DomainExpiryChecker implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The domain instance.
*/
protected $domain;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($domain)
{
$this->domain = $domain;
}
/**
* Get single domain
*
* #return object
*/
public function getDomain($domain)
{
$domain = Domains::where('id', $domain['id'])
->first();
return $domain;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$updatableDomain = $this->getDomain($this->domain);
if (!$updatableDomain) {
return;
}
// stuff happens here
}
}
What am I missing, I'm sure it's just one thing.

Using form request data in Laravel 5.3 mailable views

Looking around I cant find much information on how to do this. Plenty of the same examples just explaining the basic implementation of the mailable class but thats it.
I am sending an email from a contact form and have a route set up for testing that my form posts to:
Route::post('/sendmail', function() {
Mail::to("my email.com")->send(new MyTestMail());
});
I then have my mailable class which I have passed a test variable as a test string to:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class MyTestMail extends Mailable
{
use Queueable, SerializesModels;
public $test;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
$this->test = "test";
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('emails.myTestMail');
}
}
What I wish to do is have access to my post values from the contact from and use them in the email view.
You can try passing the data you need to the constructor of that mailable.
public $postdata;
public function __construct($data)
{
$this->postdata = $data;
}
Now 'postdata' will be available to your view.
Mail::to("my email.com")->send(new MyTestMail($request->all()))

Categories