1. Go to this page and download the library: Download teracrafts/huefy-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/ */
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
class WelcomeMail extends Mailable
{
public function __construct(
public User $user
) {}
public function build()
{
return $this->view('emails.welcome')
->with([
'name' => $this->user->name,
'login_url' => route('login')
])
->subject('Welcome to our platform!');
}
public function envelope()
{
return new Envelope(
subject: 'Welcome!',
using: [
fn (Message $message) => $message
->getHeaders()
->addTextHeader('X-Template-Key', 'welcome-email')
->addTextHeader('X-Template-Data', json_encode([
'name' => $this->user->name,
'login_url' => route('login')
]))
->addTextHeader('X-Email-Provider', 'sendgrid')
]
);
}
}
// Send the email
Mail::to('[email protected]')->send(new WelcomeMail($user));
use Illuminate\Notifications\Notification;
use TeraCrafts\HuefyLaravel\Notifications\HuefyChannel;
use TeraCrafts\HuefyLaravel\Notifications\HuefyMessage;
class WelcomeNotification extends Notification
{
public function __construct(
private User $user
) {}
public function via($notifiable): array
{
return [HuefyChannel::class];
}
public function toHuefy($notifiable): HuefyMessage
{
return HuefyMessage::create('welcome-email')
->data([
'name' => $this->user->name,
'email' => $this->user->email,
'activation_url' => route('activation', $this->user->id)
])
->provider('sendgrid');
}
}
// Send the notification
$user->notify(new WelcomeNotification($user));
// In your User model
public function routeNotificationForHuefy()
{
return $this->email;
}
// Or return an array for more complex routing
public function routeNotificationForHuefy()
{
return [
'email' => $this->email,
'name' => $this->name
];
}
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Mail\Mailables\Headers;
class CustomHuefyMail extends Mailable
{
public function __construct(
private string $templateKey,
private array $templateData,
private ?string $provider = null
) {}
public function envelope(): Envelope
{
return new Envelope(
subject: 'Custom Huefy Email',
);
}
public function content(): Content
{
return new Content(
text: 'emails.empty', // Dummy view since we're using templates
);
}
public function headers(): Headers
{
return new Headers(
text: [
'X-Template-Key' => $this->templateKey,
'X-Template-Data' => json_encode($this->templateData),
'X-Email-Provider' => $this->provider ?: config('huefy.default_provider'),
],
);
}
}
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use TeraCrafts\HuefyLaravel\Facades\Huefy;
class SendHuefyEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
private string $templateKey,
private array $data,
private string $recipient,
private ?string $provider = null
) {}
public function handle(): void
{
Huefy::sendEmail(
$this->templateKey,
$this->data,
$this->recipient,
$this->provider
);
}
}
// Dispatch the job
SendHuefyEmailJob::dispatch('welcome-email', $data, $email)->onQueue('emails');