Email Send with attachment tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use App\Mail\welcomeemail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class EmailController extends Controller
{
public function sendEmail()
{
$toEmail = "findjquery@gmail.com";
$moreUser = "heernayakpara997@gmail.com";
$message = "Hello, Welcome to our website";
$subject = "Welcome to YahooBaba";
$details = [
"name" => "John Doe",
"product" => "Test Product",
"price" => 250
];
// send same email to multiple users as shown below :
// $emails = [
// "user1@gmail.com",
// "user2@gmail.com",
// "user3@gmail.com",
// ];
// foreach($emails as $recipient) {
// Mail::to($recipient)->send(new welcomeemail($message, $subject));
// }
// Below cc means "carbon copy" for send mail to another user
$request = Mail::to($toEmail)->cc($moreUser)->send(new welcomeemail($message, $subject, $details));
dd($request);
}
public function contactForm()
{
return view("contact_form");
}
public function sendContactEmail(Request $request)
{
$request->validate([
"name" => "required",
"email" => "required|email",
"subject" => "required|min:5|max:100",
"message" => "required|min:10|max:255",
"attachment" => "required|mimes:pdf,doc,docx,xls,xlsx|max:2048"
]);
$fileName = time() . "." . $request->file("attachment")->extension();
$request->file("attachment")->move("uploads", $fileName);
$adminEmail = "heernayakpara997@gmail.com";
$response = Mail::to($adminEmail)->send(new welcomeemail($request->all(), $fileName));
if ($response) {
return back()->with("success", "Thank you for contacting us.");
} else {
return back()->with("error", "Unable to submit form, Please try again.");
}
dd($fileName);
}
}
Above File is app\Http\Controllers\EmailController.php FileBelow File is app\Mail\welcomeemail.php File
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class welcomeemail extends Mailable
{
use Queueable, SerializesModels;
public $request;
public $fileName;
// public $mailmessage;
// public $subject;
// private $details;
// If we take above variable private or protected then we have to declare that variable in "content()" method
// as shown below :
/**
* Create a new message instance.
*/
public function __construct($request, $fileName)
{
$this->request = $request;
$this->fileName = $fileName;
// $this->mailmessage = $message;
// $this->subject = $subject;
// $this->details = $details;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: "Contact Form",
// subject: $this->subject,
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.welcome_mail',
// text: "mail.welcome_mail",
// Here above, we have declared "details" variable as private(or protected)
// Hence, we have to declare below "with" method as shown below :
// with: [
// "name" => $this->details["name"],
// "product" => $this->details["product"],
// "price" => $this->details["price"]
// ]
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
$attachments = [];
if ($this->fileName) {
$attachments = [
Attachment::fromPath(public_path("/uploads/" . $this->fileName))
];
}
return $attachments;
}
}
Below File is resources\views\mail\welcome_mail.blade.php File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
{{-- <title>{{ $subject }}</title> --}}
<title>{{ $request["subject"] }}</title>
</head>
<body>
{{-- <h3>{{ $subject }}</h3>
<p>{{ $mailmessage }}</p> --}}
{{-- <p>{{ $details["name"] }}</p>
<p>{{ $details["product"] }}</p>
<p>{{ $details["price"] }}</p> --}}
{{-- If we have declared variable as private or protected in Mailable class then we have to declared that
variable in blade file as shown below : --}}
{{-- <p>{{ $name }}</p>
<p>{{ $product }}</p>
<p>{{ $price }}</p> --}}
<h3>Hello, Admin</h3>
<p>Name : {{ $request["name"] }}</p>
<p>Email : {{ $request["email"] }}</p>
<p>Subject : {{ $request["subject"] }}</p>
<p>Message : {{ $request["message"] }}</p>
</body>
</html>
Below File is resources\views\contact_form.blade.php File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Contact Form</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6 mt-2">
<form action="{{ route("contact") }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="card">
@if(session("success"))
<div class="alert alert-success m-2" role="alert">
{{ session("success") }}
</div>
@elseif(session("error"))
<div class="alert alert-danger m-2" role="alert">
{{ session("error") }}
</div>
@endif
<div class="card-header">
<div class="card-title">
<h3>Contact Form</h3>
</div>
</div>
<div class="card-body">
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" value="{{ old("name") }}" class="form-control @error("name") is-invalid @enderror" name="name" id="name" placeholder="Enter Name"/>
@error("name")
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" value="{{ old("email") }}" class="form-control @error("email") is-invalid @enderror" name="email" id="email" placeholder="Enter Email"/>
@error("email")
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="mb-3">
<label for="subject" class="form-label">Subject</label>
<input type="text" class="form-control @error("subject") is-invalid @enderror" name="subject" id="subject" placeholder="Enter Subject"/>
@error("subject")
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" name="message" id="message" placeholder="Enter message"></textarea>
@error("message")
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
<div class="mb-3">
<label for="attachment" class="form-label">Attach File</label>
<input type="file" class="form-control" name="attachment" id="attachment"/>
@error("attachment")
<p class="text-danger">{{ $message }}</p>
@enderror
</div>
</div>
<div class="card-footer">
<input type="submit" class="btn btn-primary">
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Below File is routes\web File
<?php
use App\Http\Controllers\EmailController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::controller(EmailController::class)->group(function () {
Route::get("send-email", "sendEmail");
Route::get("contact", "contactForm");
Route::post("contact", "sendContactEmail")->name("contact");
});
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment