I was able to send an email but when I put the attachData() in the UserMail there was an error. I think because of the parameter $this->pdf that should be declared in UserEmailJob, and I don't know how to fix it.
UserEmailJob
public $details;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new UserEmail();
Mail::to($this->details['email'])->send($email);
}
UserEmail - mail class
I'm having error with this line $this->pdf->output()
public function __construct()
{
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('[email protected]', 'Mailtrap')
->subject('Stock Report - Laravel Tutorial')
->view('emails.userEmail')
->attachData($this->pdf->output(), 'stock_report.pdf');
}
UserController
public function mailClass()
{
$users = User::all();
$data['name'] = 'Hi';
$pdf = app('dompdf.wrapper');
$pdf->loadView('cert', $data);
$pdf->setPaper('A4', 'landscape');
$details = ['email' => '[email protected]'];
UserEmailJob::dispatch($details);
return response()->json(['status' => 'success', 'message' => 'Report has been sent successfully.']);
}
CodePudding user response:
Because $this->pdf is not defined in your class.
try this:
private $pdf;
public function __construct($pdf) {
$this->pdf = $pdf;
}
public function build(){
return $this->from('[email protected]', 'Mailtrap')
->subject('Stock Report - Laravel Tutorial')
->view('emails.userEmail')
->attach($this->pdf);
}
and in your UserController
UserEmailJob::dispatch($details, $pdf->output());
So modify it
public $details;
public $pdfStream;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details, $pdfStream)
{
$this->details = $details;
$this->pdfStream = $pdfStream;
}
public function handle()
{
$email = new UserEmail($this->pdfStream);
Mail::to($this->details['email'])->send($email);
}
EDIT: it is probably not recommended to stream the pdf via json
UserEmailJob
public $details;
public $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($details, $data)
{
$this->details = $details;
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email = new UserEmail($this->data);
Mail::to($this->details['email'])->send($email);
}
UserEmail
private $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$pdf = app('dompdf.wrapper');
$pdf->loadView('cert', $this->data);
$pdf->setPaper('A4', 'landscape');
return $this->from('[email protected]', 'Mailtrap')
->subject('Stock Report - Laravel Tutorial')
->view('emails.userEmail')
->attachData($pdf->output(), 'stock_report.pdf');
}
UserController
public function mailClass()
{
$users = User::all();
$data['name'] = 'Hi';
$details = ['email' => '[email protected]'];
UserEmailJob::dispatch($details, $data);
return response()->json(['status' => 'success', 'message' => 'Report has been sent successfully.']);
}