Create mail with new configuration laravel
Here is an example of how to create a new mail configuration in Laravel:
Step 1: Create a new mail configuration file
In the config
directory of your Laravel project, create a new file called mail.php
. This file will contain the configuration settings for your mail system.
Step 2: Define the mail configuration
In the mail.php
file, add the following code:
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'Your Name'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
This configuration file defines the mail driver, host, port, from address, encryption, username, and password. You can adjust these settings to match your mail server configuration.
Step 3: Set environment variables
In your .env
file, set the following environment variables:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_FROM_NAME=Your Name
[email protected]
MAIL_PASSWORD=your-password
Replace [email protected]
and your-password
with your actual email address and password.
Step 4: Update the mail facade
In your config/mail.php
file, update the facade
section to point to the new mail configuration file:
'mail' => [
'driver' => 'mail',
'facade' => \Illuminate\Mail\MailFacade::class,
],
Step 5: Test the mail configuration
To test the mail configuration, you can use the Mail::send
method in a controller or a test. For example:
use Illuminate\Support\Facades\Mail;
public function sendMail()
{
Mail::send('emails.welcome', [], function ($message) {
$message->to('[email protected]', 'Recipient Name');
$message->subject('Welcome!');
});
}
This code sends a welcome email to the recipient with the subject "Welcome!".
That's it! You have now created a new mail configuration in Laravel.