Install the package:
composer require mahrdanial/shwanix-mailer
Configure the mailer:
php artisan vendor:publish --tag=shwanix-mailer-config
config/mail.php to set MAIL_MAILER=shwanix.config/shwanix-mail.php with your Shwanix API endpoint, API key, and optional settings (timeouts, SSL verification).First use case:
Send a test email using Laravel’s built-in Mail facade:
use Illuminate\Support\Facades\Mail;
use App\Mail\TestMail;
Mail::to('user@example.com')->send(new TestMail());
The email will be sent via Shwanix’s HTTP API instead of SMTP.
App\Mail\* classes without modification.
Mail::to('recipient@example.com')->send(new WelcomeEmail($user));
Mail::to('user@example.com')->queue(new OrderConfirmation($order));
Mail::markdown('emails.welcome')->to('user@example.com')->send();
withSwiftMessage method:
Mail::to('user@example.com')->withSwiftMessage(function ($message) {
$message->getHeaders()->addTextHeader('X-Custom-Header', 'value');
})->send(new TestMail());
Mail::to('user@example.com')->attach($pathToFile)->send(new TestMail());
php artisan queue:work
config/shwanix-mail.php dynamically (e.g., via environment variables or a config service).staging.shwanix-api.com vs. api.shwanix.com).GuzzleException for API failures:
try {
Mail::to('user@example.com')->send(new TestMail());
} catch (\GuzzleHttp\Exception\RequestException $e) {
Log::error('Shwanix API error: ' . $e->getMessage());
// Fallback to another mailer (e.g., SMTP)
}
Laravel Version Mismatch:
7.30.x). Installing on older Laravel 7 versions will fail.SwiftMailer vs. Symfony Mailer:
SwiftShwanixTransport (SwiftMailer).ApiTransport (Symfony Mailer).API Rate Limits:
config/shwanix-mail.php for timeout/retries.$client->setDefaultOption('timeout', 30);
$client->setDefaultOption('connect_timeout', 5);
Missing Config:
config/shwanix-mail.php will throw Configuration not found errors.php artisan vendor:publish --tag=shwanix-mailer-config and update .env:
SHWANIX_API_KEY=your_key_here
SHWANIX_API_URL=https://api.shwanix.com/send
Enable Guzzle Logging:
Add to config/shwanix-mail.php:
'debug' => env('APP_DEBUG', false),
Logs will appear in Laravel’s log channel.
Test with MAIL_MAILER=log:
Temporarily switch to the log mailer to verify email content before sending:
MAIL_MAILER=log
Custom Transport:
Extend ShwanixTransport for additional API features:
namespace App\Mail\Transports;
use Mahrdanial\ShwanixMailer\Transports\ShwanixTransport;
class CustomShwanixTransport extends ShwanixTransport {
public function sendSwiftMessage(Swift_Message $message) {
// Add custom logic (e.g., pre-process attachments)
return parent::sendSwiftMessage($message);
}
}
Register in AppServiceProvider:
Mail::extend('custom_shwanix', function () {
return new CustomShwanixTransport();
});
Fallback to SMTP:
Implement a fallback mailer in AppServiceProvider:
Mail::extend('shwanix_fallback', function () {
$shwanix = new \Mahrdanial\ShwanixMailer\Transports\ShwanixTransport();
return function ($message) use ($shwanix) {
try {
$shwanix->sendSwiftMessage($message);
} catch (\Exception $e) {
// Fallback to SMTP
$smtp = new \Illuminate\Mail\Transport\SendmailTransport();
$smtp->send($message);
}
};
});
Environment-Specific Config:
Use Laravel’s config() helper to dynamically load settings:
$apiKey = config('services.shwanix.key', env('SHWANIX_API_KEY'));
Mail::batch():
Mail::batch([])->to('user@example.com')->send(new Newsletter());
Mail::to('user@example.com')->later(now()->addMinutes(5), new Reminder())->onQueue('high');
How can I help you explore Laravel packages today?