How to Send Email in Laravel 2022
Laravel provides a convenient way to send emails. In this article, we gonna learn how to send emails.
1. Let's create a new laravel project.
composer create-project laravel/laravel laravel-mail
2. Open .env and update your mail credentials.
MAIL_MAILER=smtp
MAIL_HOST= //Email Host
MAIL_PORT=587
MAIL_USERNAME= //Email Address
MAIL_PASSWORD= //Email Password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS= //Email Address
MAIL_FROM_NAME="${APP_NAME}"
3. Create a mailable class.
php artisan make:mail NewsletterEmail
app/Mail/NewsletterEmail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NewsletterEmail extends Mailable
{
use Queueable, SerializesModels;
protected $article;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($article)
{
$this->article = $article;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.newsletter')
->subject('Weekly Newsletter')
->with('article', $this->article);
}
}
4. Next, create newsletter.blade.php inside resources/views/emails directory.
resources/views/emails/newsletter.blade.php
<!DOCTYPE html>
<html>
<head>
<title>ultimateakash.com</title>
</head>
<body>
<h2>{{ $article['title'] }}</h2>
<p>{{ $article['description'] }}</p>
</body>
</html>
5. Create a Controller.
php artisan make:controller NotificationController
app/Http/Controllers/NotificationController.php
<?php
namespace App\Http\Controllers;
use App\Mail\NewsletterEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class NotificationController extends Controller
{
public function example1()
{
$email = 'akashmjp@gmail.com';
$article = [
'title' => 'Lorem Ipsum',
'description' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
];
Mail::to($email)->send(new NewsletterEmail($article));
die('Email Sent. - Example1');
}
public function example2()
{
$email = 'akashmjp@gmail.com';
$article = [
'title' => 'Lorem Ipsum',
'description' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
];
Mail::send('emails.newsletter', ['article' => $article], function ($message) use ($email) {
$message->to($email)->subject('Weekly Newsletter');
});
die('Email Sent. - Example2');
}
public function example3()
{
$email = 'akashmjp@gmail.com';
$text = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.';
Mail::raw($text, function ($message) use ($email) {
$message->to($email)->subject('Weekly Newsletter');
});
die('Email Sent. - Example3');
}
}
Here is a list of the available methods on the $message builder instance:
$message->from($address, $name = null);
$message->sender($address, $name = null);
$message->to($address, $name = null);
$message->cc($address, $name = null);
$message->bcc($address, $name = null);
$message->replyTo($address, $name = null);
$message->subject($subject);
$message->priority($level);
$message->attach($pathToFile, array $options = []);
$name is an optional argument.
You can also pass an array inside the to() method.
$emails = ['user1@gmail.com', 'user1@gmail.com'];
Mail::send('emails.newsletter', ['article' => $article], function ($message) use ($emails) {
$message->to($emails)->subject('Weekly Newsletter');
});
Email with Attachment
$message->to($email)
->subject('Weekly Newsletter')
->attach('https://www.ultimateakash.com/assets/img/logo/logo.png');
you can also pass name for the attachment.
$message->to($email)
->subject('Weekly Newsletter')
->attach('https://www.ultimateakash.com/assets/img/logo/logo.png', ['as' => 'My Logo']);
6. Create routes
routes/web.php
<?php
use App\Http\Controllers\NotificationController;
use Illuminate\Support\Facades\Route;
Route::get('/example1', [NotificationController::class, 'example1']);
Route::get('/example2', [NotificationController::class, 'example2']);
Route::get('/example3', [NotificationController::class, 'example3']);
7. Finally open http://localhost/laravel-mail/public/example1 in the browser.
Github Repo: https://github.com/ultimateakash/laravel-mail
Leave Your Comment