dsimon24cx's avatar

Receiving emails in a Laravel application?

Hello all,

I am looking for a way to receive and process (open, move, delete) eMails in a Laravel application. Additionally, I would like to move attachments which are resumes to another folder in the Laravel website.

Do you have any idea how I can best implement this.

Greetings.

0 likes
6 replies
LaryAI's avatar
Level 58

One solution to receive and process emails in a Laravel application is to use a package like Laravel Mailbox. This package allows you to receive incoming emails and process them using Laravel's built-in mail handling functionality.

To use Laravel Mailbox, you'll need to configure your email server to forward incoming emails to your Laravel application. Once you've done that, you can create a new mailbox using the php artisan make:mailbox command. This will generate a new class that extends the Mailbox class and allows you to define how incoming emails should be processed.

For example, if you want to move attachments to a specific folder, you could define a method in your mailbox class that looks for emails with attachments and moves them to the desired folder:

use Illuminate\Mail\Markdown;
use BeyondCode\Mailbox\InboundEmail;

class MyMailbox extends Mailbox
{
    public function handle(InboundEmail $email)
    {
        // Check if the email has attachments
        if ($email->hasAttachments()) {
            // Loop through each attachment
            foreach ($email->attachments() as $attachment) {
                // Move the attachment to the desired folder
                $attachment->store('path/to/folder');
            }
        }
    }
}

You can then register your mailbox in your routes/mailbox.php file:

use App\Mailboxes\MyMailbox;

Route::mailbox('my-mailbox', MyMailbox::class);

This will tell Laravel Mailbox to use your MyMailbox class to handle incoming emails for the my-mailbox route.

For more information on how to use Laravel Mailbox, check out the documentation: https://laravel-mailbox.readthedocs.io/en/latest/

1 like
dsimon24cx's avatar

Sounds like a plan. I have also seen this package. Is it possible to connect any eMail client to it? Bspw. the eMails go in basically via IMAP. Is there something going on?

yatsenkolesh's avatar

Hey. I do recommend this library for receiving emails - github.com/proxied-mail/laravel-receive-email You can create a proxy email and receive emails that were sent to it via webhook or just via rest API. It doesn't work with IMAP, but it provides you email address and shows you what was received.

Please or to participate in this conversation.