Let your Laravel app receive emails.
composer require notifiablehq/receive-emailPublish the configuration and migration files:
php artisan vendor:publish --provider="Notifiable\\ReceiveEmail\\ReceiveEmailServiceProvider" --tag=receive-emailThen run the migrations:
php artisan migrateWhenever an email is received, the package will dispatch the Notifiable\\ReceiveEmail\\Events\\EmailReceived event. On Laravel 11 and above, you should use a listener class:
Generate a listener class:
php artisan make:listener HandleIncomingEmailThen implement the handle method:
namespace App\Listeners;
use Notifiable\ReceiveEmail\Events\EmailReceived;
class HandleIncomingEmail
{
public function handle(EmailReceived $event): void
{
$email = $event->email;
\Log::info('Received email with subject: ' . $email->parsedMail()->subject());
}
}The Email model gives you access to sender, recipients, subject, and body through the parsedMail method. Example:
/** @var \Notifiable\ReceiveEmail\Contracts\ParsedMailContract $mail */
$mail = $email->parsedMail();
$subject = $mail->subject();
$textBody = $mail->text();
$htmlBody = $mail->html();
$recipients = $mail->recipients();- Add this to your recipes, you can name it
Install Mailparse. Make sure the user isroot.
apt-get update
apt-get install -y php-cli php-mailparse-
If you already have an existing server, run this recipe on that server. Otherwise, create a new server and make sure to select this recipe as a
Post-Provision Recipe. You'll have to showAdvance Settingsto select this. -
Once you have the server ready, open up
Port 25, add your site, and deploy your Laravel app. -
SSH into your Forge server and go to your site directory. Then run the setup command as a
super user:
sudo php artisan notifiable:setup-postfix domain-that-receives-email.com-
Add the following DNS records to your domain:
Type Host Value A your-application-domain.com your.forge.ip.address Type Host Value Priority MX domain-that-receives-email.com your-application-domain.com 10
The solutions in this package are inspired by the following projects: