Web Technologies Week 16 (Email Notification)
Web Technologies Week 16 (Email Notification)
Maaz Rehan (by Fawad SP20-BCS-122) CSC336 Web Technologies Dated: 02-Jan-23
Method 1:
Sending Email Directly by Code
D. Generating Mailable:
When building Laravel applications, each type of email sent by your application is
represented as a "mailable" class. These classes are stored in the app/Mail directory.
Don't worry if you don't see this directory in your application, since it will be generated
for you when you create your first mailable class using
In the mailable file, there will be functions like: _construct(), envelope(), content().
In the content function, we just need to give the path of our blade file in which our
content is written, and it will directly read it
E. Adding Route.
use Illuminate\Support\Facades\Mail;
use App\Mail\form_mail;
Route::get('/send_mail', function(){
Mail::to('maazrehan@ciitwah.edu.pk')
->send(new form_mail());
});
Now start your server using php artisan serve command and go into your browser type
http://localhost:8000/send_mail
Method 2:
Sending Email Via Form
C. Creating Form:
<body>
<div class="container">
<div class="form_body">
<form action="{{route('send_mail')}}" method="post">
@csrf
<label for="">cc</label><br>
<input type="email" name="cc"
placeholder="Example@email.com"><br><br>
<label for="">bcc</label><br>
<input type="email" name="bcc"
placeholder="Example@email.com"><br><br>
<label for="">Subject</label><br>
<input type="text" name="subject" placeholder="Subject"><br><br>
<label for="">Body</label><br>
<textarea name="details" id="" cols="30"
rows="5"></textarea><br><br>
The interface will look like this. You can add style to it using Internal CSS or External CSS
NOTE: Here sender and receiver both emails are same
▪ When user clicks the submit button (shown in above form), data on the form
should send to the email or emails you entered.
Here /send_mail is the user-defined URL and is associated with the click action of the
submit button on the form that we created in Mail_form.blade.php.
▪ This route will run the function named “send_mail” that we create now in the
mailController.
$details = [
'subject' => $req->get('subject'),
'body' => $req->get('details')
];
G. Generating Mailables
When building Laravel applications, each type of email sent by your application is
represented as a "mailable" class. These classes are stored in the app/Mail directory.
Don't worry if you don't see this directory in your application, since it will be generated
for you when you create your first mailable class using
• Go to app\Mail\form_mail
{
use Queueable, SerializesModels;
public $details;
/**
* Create a new message instance.
* @return void
*/
public function __construct($details)
{
$this->details = $details;
}
/**
* Get the message envelope.
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope() {
return new Envelope(
subject: $this->details['subject'],
);
}
public function content()
{
return new Content(
view: 'Mails.mail',
);
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>MAIL PAGE</title>
</head>
<body>
<p>{{$details['body']}}</p>
<br>
<p>Regards:<br>
Labour Lobby PVT (LTD)
</p>
</body>
</html>
Now start your server using php artisan serve command and go into your browser type
http://localhost:8000/mail_form
Subject
Body