In Laravel, you can send email using Gmail by using the built-in `mail` function or by using a library like SwiftMailer. To use the `mail` function, you will need to configure your Gmail account details in your Laravel application's configuration file.
Here is an example of how to send an email using Gmail in Laravel:
- First, you will need to configure your Gmail account details in the `.env` file of your Laravel application:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail_email_address
MAIL_PASSWORD=your_gmail_email_password
MAIL_ENCRYPTION=tls
2. Then, you can use the `mail` function to send an email. You can call the `mail` function from anywhere in your application. Here is an example of how to use the `mail` function:
use Illuminate\Support\Facades\Mail;
$to = 'recipient@example.com';
$subject = 'Example Subject';
$message = 'Example Message';
Mail::send([], [], function ($message) use ($to, $subject, $message) {
$message->to($to)
->subject($subject)
->setBody($message, 'text/html'); // for HTML email
});
In this example, the `mail` function takes three arguments: an array of attachments, an array of options, and a closure that defines the message to be sent. The closure takes a `$message` object, which you can use to set the recipient email address, the subject, and the body of the email.
Tags:
laravel