Send emails with Laravel
In this quickstart, you'll learn how to send transactional emails from your Laravel 10 or 11 app with Sidemail. You'll set up the SDK, send your first email, and see examples for common use cases like welcome emails, password resets, and scheduled reports.
Before you start
- Create a Sidemail account → get your API key
- Add a sending domain → set up your domain for sending
1. Install
composer require sidemail/sidemail
2. Add your API key
Add your Sidemail API key to .env:
SIDEMAIL_API_KEY=your-api-key
3. Configure the service
Register the Sidemail client in AppServiceProvider to enable dependency injection.
// app/Providers/AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Sidemail\Sidemail;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(Sidemail::class, function () {
return new Sidemail();
});
}
}
4. Send a welcome email with Laravel
Inject Sidemail\Sidemail into your controller method.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sidemail\Sidemail;
class RegistrationController extends Controller
{
public function store(Request $request, Sidemail $sidemail)
{
// ... create user ...
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'templateName' => 'Welcome',
'templateProps' => ['firstName' => 'Alex'],
]);
return response('User registered!');
}
}
5. Send a password reset email with Laravel
// app/Http/Controllers/SecurityController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sidemail\Sidemail;
class SecurityController extends Controller
{
public function requestReset(Request $request, Sidemail $sidemail)
{
$validated = $request->validate(['email' => 'required|email']);
$email = $validated['email'];
// ... find user, generate token ...
$sidemail->sendEmail([
'toAddress' => $email,
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'templateName' => 'Password Reset',
'templateProps' => [
'actionUrl' => 'https://myapp.com/reset/token123',
],
]);
return response('Reset link sent!');
}
}
6. Send a weekly report (Artisan Command) with Laravel
Create a command to send emails on a schedule.
// app/Console/Commands/SendWeeklyReport.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Sidemail\Sidemail;
class SendWeeklyReport extends Command
{
protected $signature = 'app:send-weekly-report';
protected $description = 'Send weekly report email';
public function handle(Sidemail $sidemail)
{
// In a real app, you would calculate this from the database
$newSignups = 150;
$chartData = [100, 200, 300, 400, 200, 300, 200, 500];
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'My App',
'templateName' => 'Weekly Report',
'templateProps' => [
'signups' => $newSignups,
'chart' => $chartData,
],
]);
$this->info('Weekly report sent!');
}
}
You can now run this command manually or schedule it in routes/console.php:
// routes/console.php
use Illuminate\Support\Facades\Schedule;
Schedule::command('app:send-weekly-report')->weeklyOn(1, '8:00');
7. Send email via Event Listener with Laravel
Decouple email sending from your controllers by using an event listener.
// app/Listeners/SendWelcomeEmail.php
namespace App\Listeners;
use App\Events\UserRegistered;
use Sidemail\Sidemail;
class SendWelcomeEmail
{
public function __construct(
private Sidemail $sidemail
) {}
public function handle(UserRegistered $event): void
{
$this->sidemail->sendEmail([
'toAddress' => $event->user->email,
'fromAddress' => '[email protected]',
'fromName' => 'My App',
'templateName' => 'Welcome',
]);
}
}
8. Send HTML email (with Blade)
Use view()->render() to generate HTML from a Blade template.
// app/Http/Controllers/InvoiceController.php
public function sendInvoice(Sidemail $sidemail)
{
$html = view('emails.invoice', [
'amount' => 99,
])->render();
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'subject' => 'Your Invoice',
'html' => $html,
]);
return response('Invoice sent!');
}
9. Send Markdown email
Store your markdown content in a file (e.g. resources/views/emails/welcome.md) and load it.
$markdown = file_get_contents(resource_path('views/emails/welcome.md'));
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'subject' => 'Testing markdown emails 😊',
'markdown' => $markdown,
'templateProps' => [
'name' => 'John',
'link' => 'https://example.com',
],
]);
10. Send plain text email
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'subject' => 'Hello',
'text' => 'Hello! 👋',
]);
11. Schedule email
Send email later. Set scheduledAt to an ISO date string.
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'templateName' => 'Welcome',
'templateProps' => ['firstName' => 'Alex'],
'scheduledAt' => now()->addHour()->toIso8601String(),
]);
12. Send with attachment
Use the Sidemail::fileToAttachment helper to attach files.
use Sidemail\Sidemail;
$pdfData = file_get_contents(storage_path('app/invoices/invoice.pdf'));
$attachment = Sidemail::fileToAttachment('invoice.pdf', $pdfData);
$sidemail->sendEmail([
'toAddress' => '[email protected]',
'fromAddress' => '[email protected]',
'fromName' => 'Your App',
'subject' => 'Your invoice',
'text' => 'See attached.',
'attachments' => [$attachment],
]);
13. Handle errors
use Sidemail\SidemailException;
use Illuminate\Support\Facades\Log;
public function send(Sidemail $sidemail)
{
try {
$sidemail->sendEmail([/* ... */]);
} catch (SidemailException $e) {
Log::error('Sidemail error', [
'message' => $e->getMessage(),
'httpStatus' => $e->getHttpStatus(),
'errorCode' => $e->getErrorCode(),
]);
return response('Error sending email', 500);
}
return response('Email sent!');
}