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()))
Related
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);
// ...
I am trying to send a email in Laravel. It works finicky to say at least. got couple of classes for sending email for different conditions. Only difference would be markup of sent email and blade file which data is sent to before sending. I tried using use Mail; in controller from whom i'm sending it, with \Mail::(... and use Illuminate\Support\Facades\Mail; with Mail::(... and vice-versa. So it ain't it.
Here are the mail files:
ToHR file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class toHR extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.toHR');
}
}
ToHRFirst file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class toHRFirst extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.toHRFirst');
}
}
And my test file JobCreated:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\User;
class JobCreated extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $job;
public $jobCount;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user,$job,$jobCount)
{
$this->user = $user;
$this->job = $job;
$this->jobCount = $jobCount;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('mail.job-created');
}
}
They'll all the same, yet error occurs. What gives? Did i miss something? Any help will be appreciated, just point me in the right direction.
Code which i use to send email:
Variables used get values from queries which are rather long, but they are not an issue anyways.
Ver1:
Mail::to($user->email)->send(
new JobCreated($user,$job,$jobCount)
);
Ver2:
Mail::to($user->email)->send(
new toHRFirst($user,$job,$jobCount)
);
Ver3:
Mail::to($user->email)->send(
new toHR($user,$job,$jobCount)
);
Sending works if it's used first version. Other cannot be found and that's the error.
Try renaming your files to follow Laravel convention;
You can see that the file JobCreated has a class JobCreated. You need to respect the uppercases:
Rename the ToHR.php file into ToHr.php, then:
Rename the class class toHRinto class ToHr
Rename the ToHRFirst.php file into ToHrfirst.php, then:
Rename the class class toHRFirst into class ToHrfirst
Or if you want to keep the uppercases, just rename the classes like so:
class toHRinto class ToHR
class toHRFirstinto class ToHRFirst
Finally, don't forget to import your job class at the top of your controller file (or wherever you use the Mail method:
use App\Mail\ToHR;
and
use App\Mail\ToHRFirst;
First time using queue in laravel i have a simple contact form page that user submit information and i am trying to use queue to receive that information. I am pretty sure that my setup is incorrect because when i run the queue it saying processing. My question is how come my data isn't sending and what the correct way of sending the data in the array.
AskEmailController.php
<?php
namespace App\Http\Controllers;
use App\Jobs\HelpEmailJob;
use Illuminate\Support\Facades\Mail;
use Illuminate\Http\Request;
use Carbon\Carbon;
class AskEmailController extends Controller
{
public function askemail()
{
return view('help.question');
}
public function store(Request $request)
{
HelpEmailJob::dispatch()
->delay(Carbon::now()->addSeconds(5));
}
}
HelpEmailJob
namespace App\Jobs;
use App\Mail\HelpEmailMailable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class HelpEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
Mail::to('mikeshasaco#gmail.com')->queue(new HelpEmailMailable());
}
}
HelpEmailMailiable
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class HelpEmailMailable extends Mailable
{
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->$data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$data = array(
'sirname' => $this->data['sirname'],
'email' => $this->data['email'],
'bodymessage' => $this->data['bodymessage'],
);
return $this->view('help.contactinfo')
->with([
'data' =>$data,
]);
}
}
You can directly send the mail from your controller like this:
class AskEmailController extends Controller
{
public function store(Request $request)
{
Mail::to('mikeshasaco#gmail.com')->send(new HelpEmailMailable);
}
}
instead of using a job. And to make the email queue by default by implementing the ShouldQueue interface like this:
class HelpEmailMailable extends Mailable implements ShouldQueue
{
...
}
Make sure your queue is running when you test this and always restart it after you make a change in the code.
I am trying to set a subject line for an email but Laravel keeps using the class name "FormCompleted" as the subject, here's the code I've got:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class FormCompleted extends Mailable
{
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* #param $data
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('Form submitted')
->view('emails.form-completed')
->with('data', $this->data);
}
}
EDIT 1: The problem is only with my local server, it works on my production server
We may need to see the code that is used to implement the FormCompleted Mailable class and send the email, and maybe even your email Blade view as well. That would help debug further.
However, I tested out your code, and I got the email sent correctly with the appropriate subject as expected.
The only change I did was to use ->with($this->data) instead of ->with('data', $this->data); (how you have it), because, the properties defined within the $data object, are what the view uses.
public function build()
{
return $this->subject('Testing Email 2')
->view('foo.mail2')
->with($this->data);
}
I would have gotten an error otherwise (with the view trying to use unavailable properties, since the view would have been looking for a data property).
I'm trying to create job to register users and tried to follow Jeffrey's video but looks like dispatchfrom is removed for some reason. This is what i'm trying to do now:
This is my controller:
public function PostSignUp(Request $request)
{
dispatch(new RegisterUser($request->all()));
return 'done';
}
This is my job:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RegisterUser implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
protected $request;
/**
* Create a new job instance.
* #param $request
* #return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = $this->request->email;
var_dump('I should register user with email:' . $email);
}
}
I also tried to put
just $request instead of $request->all()
but then i get
Serialization of 'Closure' is not allowed
And now I'm getting Trying to get property of non-object error. Is this good way to pass whole request to job ? Should i do it some other way ?
try with input()
$request->input()
When you do $request->all(), that means you are passing an array to the job and not the whole $request. Therefore, you can simply do this in your Job Handler.
public function handle()
{
$email = $this->request['email'];
var_dump('I should register user with email:' . $email);
}
Let me know if something else is required :)