'Laravel 5.5, Queued mail with attachments
I want to send queued mail with attachments. When I test without queueing it works fine. But when I put mail in the queue the mail contains no attached files. I see the file-paths in serialized string in the job
I get error Illegal string offset 'file'.
Any clues about whats wrong?
$attachments = [
storage_path( 'app/pdf.pdf' ),
storage_path( 'app/pdf-2.pdf' ),
];
dispatch( new ProcessReports( $this->report, $attachments ) )->delay( now()->addMinutes( 1 ) );
I use a mailable to setup mail:
$mailable = $this->subject( 'Your reports' )
->view( 'email' );
if ( count( $this->attachments ) > 0 ) {
foreach ( $this->attachments as $attachment ) {
$mailable->attach( $attachment );
}
}
return $mailable;
and finally the job handler:
public function __construct( Report $report, $attachments = array() ) {
$this->report = $report;
$this->attachments = $attachments;
}
/**
* Execute the job.
*
* @return void
*/
public function handle() {
$email = new ReportSent( $this->report, $this->attachments );
Mail::to( $this->report->user->email )
->send( $email );
}
Solution 1:[1]
Controller
$files = [
public_path('files/160031367318.pdf'),
public_path('files/1599882252.png'),
];
$details = [
'title' => 'Mail from abc',
'body' => 'hello',
'email' => '[email protected]',
'files' => $files
];
dispatch(new SendEmailJob($details));
Job Handler
public $details;
public function __construct($details)
{
$this->details = $details;
}
public function handle()
{
$details = [
'title' => $this->details['title'],
'body' =>$this->details['body'],
'files' =>$this->details['files'],
'email' =>$this->details['email'],
];
Mail::to($this->details['email'])->send(new NotificationMail($details));
}
Mailable
public $details;
public function __construct($details)
{
$this->details = $details;
}
public function build()
{
$this->subject('Mail from abc')->markdown('mail.notification-mail');
foreach ($this->details['files'] as $file){
$this->attach($file);
}
return $this;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karl Hill |
