1. Go to this page and download the library: Download lettr/lettr-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lettr / lettr-laravel example snippets
use Illuminate\Support\Facades\Mail;
use App\Mail\Lettr\WelcomeEmail;
// Using a generated Mailable
Mail::to('[email protected]')->send(new WelcomeEmail($data));
// Or send templates inline
Mail::lettr()->to('[email protected]')->sendTemplate('welcome-email', substitutionData: $data);
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;
Mail::to('[email protected]')->send(new WelcomeEmail());
use Lettr\Laravel\Facades\Lettr;
$response = Lettr::emails()->send(
Lettr::emails()->create()
->from('[email protected]', 'Sender Name')
->to(['[email protected]'])
->subject('Hello from Lettr')
->html('<h1>Hello!</h1><p>This is a test email.</p>')
);
echo $response->requestId; // Request ID for tracking
echo $response->accepted; // Number of accepted recipients
use Illuminate\Support\Facades\Mail;
use App\Mail\OrderConfirmation;
// Send using Mailable
Mail::to('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->send(new OrderConfirmation($order));
Mail::raw('Plain text email content', function ($message) {
$message->to('[email protected]')
->subject('Quick Update');
});
// Use Lettr for this specific email
Mail::mailer('lettr')
->to('[email protected]')
->send(new TransactionalEmail());
// Uses default mailer
Mail::to('[email protected]')
->send(new MarketingEmail());
namespace App\Mail;
use Lettr\Laravel\Mail\LettrMailable;
use Illuminate\Mail\Mailables\Envelope;
class WelcomeEmail extends LettrMailable
{
public function __construct(
public string $userName,
public string $activationUrl,
) {}
public function build(): static
{
return $this
->template('welcome-email', version: 2)
->substitutionData([
'user_name' => $this->userName,
'activation_url' => $this->activationUrl,
]);
}
}
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;
Mail::to('[email protected]')
->send(new WelcomeEmail(
userName: 'John',
activationUrl: 'https://example.com/activate/abc123'
));
class OrderConfirmation extends LettrMailable
{
public function __construct(
public Order $order,
) {}
public function envelope(): Envelope
{
return new Envelope(
subject: "Order #{$this->order->id} Confirmed",
);
}
public function build(): static
{
return $this
->template('order-confirmation')
->substitutionData([
'order_id' => $this->order->id,
'customer_name' => $this->order->customer->name,
'items' => $this->order->items->map(fn ($item) => [
'name' => $item->name,
'quantity' => $item->quantity,
'price' => $item->formatted_price,
])->toArray(),
'total' => $this->order->formatted_total,
'shipping_address' => $this->order->shipping_address,
]);
}
}
use Illuminate\Support\Facades\Mail;
// Simple usage — subject comes from the template
Mail::lettr()
->to('[email protected]')
->sendTemplate('welcome-email', substitutionData: ['name' => 'John']);
// Override the template's subject
Mail::lettr()
->to('[email protected]')
->sendTemplate('welcome-email', subject: 'Hey John!', substitutionData: ['name' => 'John']);
// With specific template version
Mail::lettr()
->to('[email protected]')
->sendTemplate('order-confirmation', substitutionData: [
'order_id' => 123,
'items' => $items,
], version: 2);
// With a custom from address
Mail::lettr()
->from('[email protected]', 'Marketing Team')
->to('[email protected]')
->sendTemplate('promo-campaign', substitutionData: $promoData);
// With CC and BCC
Mail::lettr()
->to('[email protected]')
->cc('[email protected]')
->bcc('[email protected]')
->sendTemplate('invoice', substitutionData: $invoiceData);
// With a generated DTO (implements Arrayable)
Mail::lettr()
->to('[email protected]')
->sendTemplate('welcome-email', substitutionData: new WelcomeEmailData(
userName: 'John',
activationUrl: 'https://example.com/activate/abc123',
));
class MarketingEmail extends LettrMailable
{
public function envelope(): Envelope
{
return new Envelope(
from: new Address('[email protected]', 'Marketing Team'),
subject: 'Special Offer',
);
}
}