Email
Steps to work with Email
• 1.Open .env file for setting
• 2. Create a controller for email-
• Php artisan make:controller EmailController
• 3. Create a mail file ---
• Php artisan make:mail Welcomemail
• 4. Create a blade file--- mail.blade.php
• 5.Create a route
Step1.open .env file and change the setting
• MAIL_MAILER=smtp
• MAIL_SCHEME=null
• MAIL_HOST=smtp.gmail.com
• MAIL_PORT=587
• MAIL_ENCRYPTION=tls
• [email protected]
• MAIL_PASSWORD=lfxljbkddkteyomt
• [email protected]
m
• MAIL_FROM_NAME="${APP_NAME}"
Step2. Create a controller for mail
Create a mail file
Open the mail file->app-mail-file
EmailController.php code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\Welcomemail;
class EmailController extends Controller
{
public function sendmail(Request $req)
{
$toemail="
[email protected]";
$message="Hi, how are you";
$subject= "greetings";
Mail::to($toemail)->send(new welcomemail($subject,$message));
return "Email send successfully";
}
}
<?php
Welcomemail.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class Welcomemail extends Mailable
{
use Queueable, SerializesModels;
public $msg;
public $sub;
/**
* Create a new message instance.
*/
public function __construct($message,$subject)
{
$this->msg=$message;
$this->sub=$subject;
}
• /**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: $this->sub,
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: "mail",
//$text="hello ,how are you"
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Mail.blade.php
• <title> Email</title>
• <h1>{{$sub}}</h1>
• <p>{{$msg}}</p>
Web.php
• Route::get('/mail',
[EmailController::class,'sendmail']);
File Uploading in Laravel
• Upload and display file: Steps
• Make Small form to select file
• Make a controller for uploading file
• Store file with auto generated name
• Store file with the provided name
• Make upload file public
• Display file after upload
Methods to upload the file
Steps :Create a form and controller
Local-no read,public-read
• Php artisan storage:link
• Storage->app->public
• public