shafqatjan / laravel-amp-mail
Send interactive AMP emails (Gmail, Yahoo Mail, Mail.ru) from Laravel mailables
Requires
- php: ^8.2
- illuminate/contracts: ^12.0|^13.0
- illuminate/mail: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- symfony/mime: ^7.2
Requires (Dev)
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Send interactive AMP emails from ordinary Laravel mailables. Readers in Gmail, Yahoo Mail and Mail.ru can fill in forms, answer questions and see live content inside the email. Everyone else gets your normal HTML version.
Mail::to($user)->send(new OrderShipped($order));
The message goes out with three versions, in the order mail apps expect:
multipart/alternative
├── text/plain (optional)
├── text/x-amp-html ← added by this package
└── text/html
Attachments and inline images keep working. Queued mail and Mail::fake() work too.
Requirements
- PHP 8.2+
- Laravel 12 or 13
Install
composer require shafqatjan/laravel-amp-mail
The service provider registers itself.
Usage
Extend AmpMailable and add an amp() method next to content():
use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use ShafqatJan\AmpMail\Amp; use ShafqatJan\AmpMail\AmpMailable; class OrderShipped extends AmpMailable { public function __construct(public Order $order) {} public function envelope(): Envelope { return new Envelope(subject: 'Your order has shipped'); } public function content(): Content { return new Content(view: 'mail.order', text: 'mail.order-text'); } public function amp(): ?Amp { return new Amp(view: 'mail.order-amp'); // or: new Amp(view: 'mail.order-amp', with: ['extra' => 'data']) // or: new Amp(html: $readyMadeAmpHtml) } }
The AMP view gets the same data as your HTML view (public properties plus with).
Existing mailables
Add the trait and set the AMP version in build():
use ShafqatJan\AmpMail\Concerns\HasAmp; class Welcome extends Mailable { use HasAmp; public function build() { return $this->view('mail.welcome')->ampView('mail.welcome-amp'); } }
ampHtml($html) takes ready-made AMP HTML.
An AMP template
<!doctype html> <html ⚡4email data-css-strict> <head> <meta charset="utf-8"> <script async src="https://cdn.ampproject.org/v0.js"></script> <style amp4email-boilerplate>body{visibility:hidden}</style> <style amp-custom>h1 { color: #5a67d8; }</style> </head> <body> <h1>Hi {{ $order->customer_name }}</h1> <p>Order {{ $order->number }} is on its way.</p> </body> </html>
The HTML version is still required: it's what most readers see.
Checking the AMP part
Mail apps don't report a broken AMP part. They quietly show the HTML version, so mistakes go unnoticed. Before sending, the package checks that the three required pieces are there:
- the
⚡4emailattribute - the AMP script
- the boilerplate style
If one is missing, it throws ShafqatJan\AmpMail\Exceptions\InvalidAmpException. The exception's problems property lists what is missing.
This quick check doesn't replace the full validator. Use the Gmail AMP playground for that. To turn the check off:
AMP_MAIL_VALIDATE=false
or publish the config:
php artisan vendor:publish --tag=amp-mail-config
Previews and tests
$amp = (new OrderShipped($order))->renderAmp(); // the AMP HTML, or null Mail::fake(); // ... Mail::assertSent(OrderShipped::class, fn ($mail) => str_contains($mail->renderAmp(), $order->number));
Getting AMP shown in Gmail
The package builds the email correctly. Whether Gmail shows the AMP version depends on the sender:
- Register your sender with Google. Until then, AMP only shows for mail you send to yourself. See Register with Google.
- SPF, DKIM and DMARC must pass for the sending domain.
- Send through a mail service that keeps the
text/x-amp-htmlpart. Plain SMTP to Gmail does, and so do most providers. Check yours before relying on it. - Expect some cases to show HTML instead: forwarded emails always do, and so do emails opened after the AMP version expires (30 days in Gmail).
Yahoo Mail and Mail.ru have their own sender registration.
Testing the package
composer install
composer test
License
MIT