How to receive email gmail with laravel?

by darrion.kuhn , in category: Third Party Scripts , 5 months ago

How to receive email gmail with laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by adan , 5 months ago

@darrion.kuhn 

To receive emails in your Laravel application using Gmail, you can use Laravel's built-in Mail and Notification features along with Gmail's SMTP settings. Here's how you can set it up:

  1. Add Gmail SMTP settings to your .env file:
1
2
3
4
5
6
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls


  1. Configure the mail driver in your config/mail.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
'driver' => env('MAIL_MAILER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),


  1. Create a new Mailable class to handle incoming emails:
1
php artisan make:mail IncomingMail


  1. In your Mailable class, implement the build() method to process incoming emails:
1
2
3
4
5
6
7
8
public function build()
{
    $email = $this->text('emails.incoming-mail');
    
    $email->from($this->from, $this->fromName);
    
    return $email;
}


  1. Configure routes/web.php to handle incoming email notifications:
1
2
3
4
Route::get('/incoming-email', function () {
    // Process incoming email
    event(new AppEventsIncomingMailEvent());
});


  1. Create a new Event class to handle incoming email events:
1
php artisan make:event IncomingMailEvent


  1. Implement the handle() method in your Event class to process incoming emails:
1
2
3
4
public function handle()
{
    // Process incoming email
}


  1. Set up Gmail to allow less secure apps access: log in to your Gmail account, go to settings, and allow less secure apps to access your account.


Now, your Laravel application is configured to receive emails using Gmail. You can test it by sending an email to the configured Gmail account and checking if your application processes the incoming email correctly.