Laravel mail::
The Mail
facade in Laravel!
The Mail
facade is a convenient way to send emails in Laravel. It provides a simple and easy-to-use interface for sending emails, making it a popular choice among developers.
Here are some common methods and examples of using the Mail
facade in Laravel:
Sending a simple email
use Illuminate\Support\Facades\Mail;
Mail::send('emails.welcome', ['user' => $user], function($message) {
$message->from('[email protected]', 'Hello');
$message->to('[email protected]', 'User');
$message->subject('Welcome!');
});
In this example, we're sending a simple email using a view named emails.welcome
. We're passing an array of data ($user
) to the view, and specifying the from, to, and subject of the email.
Sending an email with attachments
use Illuminate\Support\Facades\Mail;
Mail::send('emails.welcome', ['user' => $user], function($message) {
$message->from('[email protected]', 'Hello');
$message->to('[email protected]', 'User');
$message->subject('Welcome!');
$message->attach('path/to/file.pdf');
});
In this example, we're sending an email with an attachment named file.pdf
located at path/to/file.pdf
.
Sending an email with HTML content
use Illuminate\Support\Facades\Mail;
Mail::send('emails.welcome', ['user' => $user], function($message) {
$message->from('[email protected]', 'Hello');
$message->to('[email protected]', 'User');
$message->subject('Welcome!');
$message->html('<p>Welcome to our website!</p>');
});
In this example, we're sending an email with HTML content. The html
method allows us to specify the HTML content of the email.
Sending an email with a CC and BCC
use Illuminate\Support\Facades\Mail;
Mail::send('emails.welcome', ['user' => $user], function($message) {
$message->from('[email protected]', 'Hello');
$message->to('[email protected]', 'User');
$message->subject('Welcome!');
$message->cc('[email protected]', 'CC User');
$message->bcc('[email protected]', 'BCC User');
});
In this example, we're sending an email with a CC and BCC. The cc
method specifies the CC recipients, and the bcc
method specifies the BCC recipients.
These are just a few examples of what you can do with the Mail
facade in Laravel. For more information, you can check out the official Laravel documentation on Sending Mail.