Tinkerwell 4 is out now! Get the most popular PHP Scratchpad application. Learn more

Laravel Mailbox

Handling Inbound Emails

Once one of your mailboxes matches an incoming email, you have access to the InboundEmail object that references this email. This class has a lot of convenience methods for you, to access the email content.

Under the hood, this class uses the Mail Mime Parser package to parse the incoming email MIME and access it.

Available methods#


If your incoming email has a Message-Id header field, you can access the unique message ID using $email->id(). If no such header field exists, a random id will be generated.


Returns a Carbon object of the emails Date header value.


This method will return the emails HTML content.


This method will return the emails text content.


Returns the subject of the email.


Returns the senders email address.


Returns the name of the sender.


Returns an array containing all recipient emails and names.


Returns an array containing all cc emails and names.


Returns an array containing all attachments.

For example, to store all attachments to a file on a Laravel storage:

foreach ($email->attachments() as $attachment) {
  $filename = $attachment->getFilename();



Returns the raw ZBateson\MailMimeParser\Message message object, if you want to do additional tasks with the mail. Please refer to the Mail Mime Parser documentation.

Responding to emails#

The InboundEmail class also has two methods to make it easy for you to either reply or forward the incoming email in your application.


To reply to the inbound email, you can use the reply method. It receives a Laravel Mailable as a parameter, where you can customize the email that will be sent as a response.

Mailbox::from('[email protected]', function (InboundEmail $email) {
  $email->reply(new FeedbackReceived);


In addition to replying the incoming emails, you may also want to forward the email to other recipients after your automatic processing is done:

Mailbox::from('[email protected]', function (InboundEmail $email) {
  $email->forward('[email protected]');

This method accepts either an array of email addresses, a user object or an email string.

Storing emails#

By default, this package stores all matched incoming emails in your database. You can define the number of days, these emails will remain in your application in the config/mailbox.php file.

To automatically remove older emails, this package provides an artisan command mailbox:clean.

Running this command will result in the deletion of all inbound emails that are older than the number of days specified in the store_incoming_emails_for_days setting of the config file.

You can leverage Laravel's scheduler to run the clean up command now and then.


protected function schedule(Schedule $schedule)

Tinkerwell: The PHP Scratchpad

Write and debug your applications in an editor designed for fast iterations.

Learn more