Laravel + Resend: Kirim Email Transactional di Helipod
この記事はインドネシア語で書かれています。
Laravel punya fitur mail yang powerful, tapi setup SMTP sendiri itu ribet. Dengan Resend, kamu bisa kirim email transactional dari Laravel tanpa setup SMTP.
Artikel ini panduan lengkapnya.
Kenapa Laravel + Resend?#
| Fitur | SMTP Traditional | Resend API |
|---|---|---|
| Setup | Config SMTP server | ✅ API-based |
| Deliverability | Tergantung server | ✅ Infrastructure tinggi |
| Monitoring | Ga ada | ✅ Dashboard analytics |
| Rate limiting | Manual | ✅ Built-in |
| Free tier | Ga ada | ✅ 100 email/hari |
Langkah 1: Setup Resend#
Buat Akun Resend#
- Buka resend.com
- Daftar akun gratis
- Buka API Keys dan buat API key baru
- Catat API key-nya
Connect Resend ke Helipod#
- Buka project Helipod
- Masuk ke Settings → Integrations
- Cari Resend dan klik Connect
- Paste API key
Langkah 2: Install Resend PHP SDK#
composer require resend/resend-php
Langkah 3: Konfigurasi Laravel#
Environment Variables#
# .env
RESEND_API_KEY=re_xxxxxxxxxxxxxxxx
RESEND_FROM=noreply@helipod.io
Service Provider (Laravel 11+)#
// config/services.php
'resend' => [
'key' => env('RESEND_API_KEY'),
'from' => env('RESEND_FROM'),
],
Langkah 4: Buat Mail Class#
// app/Mail/WelcomeMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public function __construct(
public string $name,
public string $email
) {}
public function envelope(): Envelope
{
return new Envelope(
subject: 'Selamat datang di ' . config('app.name'),
);
}
public function content(): Content
{
return new Content(
view: 'emails.welcome',
with: [
'name' => $this->name,
'email' => $this->email,
],
);
}
}
Langkah 5: Buat Email Template#
{{-- resources/views/emails/welcome.blade.php --}}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: Arial, sans-serif; color: #333; line-height: 1.6; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: #10b981; color: white; padding: 20px; text-align: center; }
.content { padding: 20px; background: #f9fafb; }
.button { display: inline-block; background: #10b981; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Selamat datang!</h1>
</div>
<div class="content">
<p>Halo {{ $name }},</p>
<p>Akun kamu di {{ config('app.name') }} sudah aktif.</p>
<p>Berikut detail akun kamu:</p>
<ul>
<li><strong>Email:</strong> {{ $email }}</li>
<li><strong>Status:</strong> Aktif</li>
</ul>
<p>
<a href="{{ url('/dashboard') }}" class="button">
Buka Dashboard
</a>
</p>
<p>Terima kasih sudah bergabung!</p>
</div>
</div>
</body>
</html>
Langkah 6: Kirim Email#
Cara 1: Menggunakan Mail Facade#
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
// Kirim email
Mail::to('user@example.com')->send(new WelcomeMail(
name: 'Budi',
email: 'user@example.com'
));
Cara 2: Menggunakan Resend PHP SDK Langsung#
use Resend\Client;
$client = new Client(env('RESEND_API_KEY'));
$client->emails->send([
'from' => env('RESEND_FROM'),
'to' => ['user@example.com'],
'subject' => 'Selamat datang di Helipod',
'html' => '<h1>Halo Budi!</h1><p>Akun kamu sudah aktif.</p>',
]);
Cara 3: Queue Email (Recommended)#
// Kirim email via queue
Mail::to('user@example.com')->queue(new WelcomeMail(
name: 'Budi',
email: 'user@example.com'
));
// Atau dengan delay
Mail::to('user@example.com')->later(
now()->addMinutes(5),
new WelcomeMail(
name: 'Budi',
email: 'user@example.com'
)
);
Langkah 7: Setup Queue di Helipod#
Konfigurasi Queue Driver#
# .env
QUEUE_CONNECTION=redis
Jalankan Queue Worker#
# Di Helipod terminal
php artisan queue:work
# Atau dengan sleep delay
php artisan queue:work --sleep=3 --tries=3
Use Cases#
1. Welcome Email#
// app/Http/Controllers/AuthController.php
public function register(Request $request)
{
// ... validasi dan create user
// Kirim welcome email
Mail::to($user->email)->send(new WelcomeMail(
name: $user->name,
email: $user->email
));
return redirect('/dashboard');
}
2. Password Reset#
// Gunakan built-in Laravel
use Illuminate\Auth\Events\PasswordReset;
public function resetPassword(Request $request)
{
// ... reset password logic
event(new PasswordReset($user));
// Kirim email reset
Mail::to($user->email)->send(new PasswordResetMail(
token: $token,
email: $user->email
));
}
3. Order Confirmation#
// app/Mail/OrderConfirmation.php
class OrderConfirmation extends Mailable
{
public function __construct(
public Order $order,
public User $user
) {}
public function content(): Content
{
return new Content(
view: 'emails.order-confirmation',
with: [
'order' => $this->order,
'items' => $this->order->items,
'total' => $this->order->total,
],
);
}
}
4. Invoice Email#
// app/Mail/InvoiceMail.php
class InvoiceMail extends Mailable
{
public function __construct(
public Invoice $invoice,
public User $user
) {}
public function content(): Content
{
return new Content(
view: 'emails.invoice',
with: [
'invoice' => $this->invoice,
'user' => $this->user,
],
);
}
}
Best Practices#
1. Gunakan Queue#
// ❌ Slow - synchronous
Mail::to($email)->send(new WelcomeMail($user));
// ✅ Fast - via queue
Mail::to($email)->queue(new WelcomeMail($user));
2. Template Reusable#
// Buat base email template
abstract class BaseEmail extends Mailable
{
public function build()
{
return $this->view('emails.base')
->subject($this->getSubject());
}
abstract protected function getSubject(): string;
}
3. Error Handling#
try {
Mail::to($email)->send(new WelcomeMail($user));
} catch (\Exception $e) {
Log::error('Failed to send email: ' . $e->getMessage());
// Handle error
}
4. Monitoring#
// Log email sent
Mail::to($email)->send(new WelcomeMail($user));
// Log ke database
EmailLog::create([
'to' => $email,
'subject' => 'Welcome',
'status' => 'sent',
'sent_at' => now(),
]);
Troubleshooting#
Email Tidak Terkirim#
# Cek queue worker
php artisan queue:work
# Cek log
tail -f storage/logs/laravel.log
Email Masuk Spam#
1. Setup domain di Resend (SPF, DKIM, DMARC)
2. Jangan gunakan generic content
3. Sertakan unsubscribe link
Rate Limit#
// Resend free tier: 100 email/hari
// Gunakan queue dan batching
Mail::to($emails)->queue(new NewsletterMail($data));
Checklist#
- Buat akun Resend
- Install Resend PHP SDK
- Konfigurasi Laravel
- Buat Mail class
- Buat email template
- Setup queue di Helipod
- Test kirim email
- Setup monitoring
Kesimpulan#
Integrasi Laravel dengan Resend di Helipod memudahkan kamu untuk kirim email transactional:
- ✅ Setup mudah tanpa SMTP
- ✅ Deliverability tinggi
- ✅ Monitoring analytics
- ✅ Queue support
- ✅ Free tier 100 email/hari
Butuh bantuan? Hubungi support@helipod.io
Baca juga: