Send Emails Using Laravel 6 Mailable Class?
Step 1: Change Simple Send Mail Configuration
.env
add send Email configuration with mail driver, mail host Name, mail port, mail username, mail security password
- MAIL_DRIVER=smtp
- MAIL_HOST=smtp.gmail.com
- MAIL_PORT=587
- MAIL_USERNAME=example@gmail.com
- MAIL_PASSWORD=Example#4549865
- MAIL_ENCRYPTION=tls
Step 2: Create Mail
- php artisan make:mail SendMail
app/Mail/SendMail.php
- <?php
- namespace App\Mail;
- use Illuminate\Bus\Queueable;
- use Illuminate\Mail\Mailable;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Contracts\Queue\ShouldQueue;
- class SendMail extends Mailable
- {
- use Queueable, SerializesModels;
- public $userInfo;
- /**
- * Create a new message instance.
- *
- * @return void
- */
- public function __construct($userInfo)
- {
- $this->userInfo = $userInfo;
- }
- /**
- * Build the message.
- *
- * @return $this
- */
- public function build()
- {
- return $this->subject('Good Latest Testing for Mail from tamilRokersDemo.com')
- ->view('emails.sendEmail');
- }
- }
Step 3: Create Blade View
resources/views/emails/sendEmail.blade.php
- <!DOCTYPE html>
- <html>
- <head>
- <title>tamilRokersDemo.com</title>
- </head>
- <body>
- <h1>{{ $userInfo['title'] }}</h1>
- <p>{{ $userInfo['body'] }}</p>
- <p>Thank you, {{ $userInfo->user_name }}</p>
- </body>
- </html>
Step 4: Define A Laravel 6 Route
routes/web.php
- Route::get('sendmail', function () {
- $userInfo = [
- 'title' => 'PHP Laravel 6 Send Email Example - for Testing Purpose',
- 'body' => 'This is a email demo from TamilRokers jio 2020 to 2025',
- 'user_name' => 'Virat Kohali'
- ];
- \Mail::to('mailreciveraddress@gmail.com')->send(new \App\Mail\SendMail($userInfo));
- Session::flash('success', trans('messages.MailSendSuccess'));
- return redirect('mail');
- });
No comments :
Post a Comment