Friday, April 3, 2020
How To PHP Laravel 6 Send Email Tutorial
on
April 03, 2020
Laravel 6 Send Email Tutorial And Example
In this post we will show you Laravel 6 Send Email Tutorial And Example, hear for Laravel 6 Send Email Tutorial And Example we will give you demo and example for implement.
Do you want to send email using smtp in laravel 6? if yes then i will guide you to laravel 6 send mail example using smtp driver. i will give you simple example of how to send mail in laravel 6 using Mail class. you can also use google gmail driver for sending email in laravel 6.
Laravel 6 provide mail class to send email. you can use several drivers for sending email in laravel 6. you can use smtp, Mailgun, Postmark, Amazon SES, and sendmail. you have to configure on env file what driver you want to use.
In this tutorial, i will give you step by step instruction to send email in laravel 6. you can create blade file design and also with dynamic information for mail layout. so let’s see step by step guide and send email to your requirement.
Step 1: Make Configuration
In first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 6 will use those sender details on email. So you can simply add as like following.
.env
1
2
3
4
5
6
| MAIL_DRIVER=smtpMAIL_HOST=smtp.gmail.comMAIL_PORT=587MAIL_USERNAME=example@gmail.comMAIL_PASSWORD=12345678995MAIL_ENCRYPTION=tls |
Step 2: Create Mail
In this step we will create mail class MyTestMail for email sending. Here we will write code for which view will call and object of user. So let’s run bellow command.
php artisan make:mail MyTestMail
app/Mail/MyTestMail.php
app/Mail/MyTestMail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class MyTestMail extends Mailable { use Queueable, SerializesModels; public $details; /** * Create a new message instance. * * @return void */ public function __construct($details) { $this->details = $details; } /** * Build the message. * * @return $this */ public function build() { return $this->subject('Mail from onlinecode.org') ->view('emails.myTestMail'); }} |
Step 3: Create Blade View
In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create bellow files on “emails” folder.
resources/views/emails/myTestMail.blade.php
1
2
3
4
5
6
7
8
9
10
11
| <!DOCTYPE html><html><head> <title>onlinecode.org</title></head><body> <h1>{{ $details['title'] }}</h1> {{ $details['body'] }} Thank you</body></html> |
Step 4: Add Route
Now at last we will create “MyTestMail” for sending our test email. so let’s create bellow web route for testing send email.
routes/web.php
1
2
3
4
5
6
7
8
| Route::get('send-mail', function () { $details = [ 'title' => 'Mail from onlinecode.org', 'body' => 'This is for testing email using smtp' ]; \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMail($details)); dd("Email is Sent.");}); |
Now you can run and check example.
Laravel 6 Send Email Tutorial And Example Conclusion
Hope this code and post will help you to implement Laravel 6 Send Email. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us to help you more and improve pakainfo. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
How To Laravel 6 Send Email Tutorial And Example
on
April 03, 2020
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');
- });
Subscribe to:
Comments
(
Atom
)