Send Mail With Attachments using php mail direct laravel code example
Example: send mail with attachment in the controller laravel 8
Reference link
https://thecodelearners.com/laravel-sending-emails-with-multiple-attachments/
Use Mail;
use App\Mail\PurchaseInvoiceInformation;
public function sendEmailWithMultipleAttachments(Request $request){
$data = [
"to" => "[email protected]",
"attachments" => [
[
"path" => public_path('uploads/inv-005.pdf'),
"as" => "Purchase Invoice NO 005.pdf",
"mime" => "application/pdf",
],
[
"path" => public_path('uploads/inv-007.pdf'),
"as" => "Purchase Invoice NO 007.pdf",
"mime" => "application/pdf",
],
[
"path" => public_path('uploads/inv-009.pdf'),
"as" => "Purchase Invoice NO 009.pdf",
"mime" => "application/pdf",
],
],
];
return Mail::to($data['to'])->send(new PurchaseInvoiceInformation($data));
}
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class PurchaseInvoiceInformation extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$mail = $this->from('[email protected]')
->view('emails.purchase-invoice-information');
if(!empty($this->data["attachments"])){
foreach($this->data["attachments"] as $k => $v){
$mail = $mail->attach($v["path"], [
'as' => $v["as"],
'mime' => $v["mime"],
]);
}
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Purchase Invoice</title>
</head>
<body>
<h1>Invoice Information</h1>
<p>Dear User,</p>
<p>Find invoices for your purchase order INV005, INV007, INV009.</p>
</body>
</html>