Installation & Configuration
composer require doitcloud/email-service
php artisan vendor:publish --tag=emailservice-config
config/emailservice.php with your Office365 API credentials (tenant_id, client_id, client_secret, etc.).First Use Case: Sending an Email
SendEmail controller into a route or service:
use Doitcloud\EmailService\Http\Controllers\SendEmail;
Route::post('/send-email', function () {
$sender = new SendEmail();
$response = $sender->send([
'to' => 'recipient@example.com',
'subject' => 'Test Email',
'body' => 'Hello from Laravel!'
]);
return $response;
});
Reusable Email Service Class
SendEmail controller in a service class for cleaner dependency injection:
namespace App\Services;
use Doitcloud\EmailService\Http\Controllers\SendEmail;
class EmailService {
protected $sender;
public function __construct(SendEmail $sender) {
$this->sender = $sender;
}
public function sendTo($email, $subject, $body) {
return $this->sender->send([
'to' => $email,
'subject' => $subject,
'body' => $body
]);
}
}
AppServiceProvider:
$this->app->bind(EmailService::class, function ($app) {
return new EmailService(new SendEmail());
});
Queueing Emails for Async Processing
use Illuminate\Support\Facades\Queue;
Queue::push(function () {
$sender = new SendEmail();
$sender->send([...]);
});
Dynamic Configuration per Environment
config(['emailservice.tenant_id' => env('OFFICE365_TENANT_ID')]);
Logging & Error Handling
send() call in a try-catch to log failures:
try {
$response = $sender->send([...]);
} catch (\Exception $e) {
\Log::error("Email failed: " . $e->getMessage());
throw $e; // Or return a user-friendly message
}
Authentication Failures
401 Unauthorized or empty responses.tenant_id, client_id, and client_secret in config/emailservice.php.https://graph.microsoft.com/.default for OAuth2.yourcompany.onmicrosoft.com).CORS or Endpoint Issues
404 Not Found or CORS errors when testing locally.send_email_url in config points to the correct Office365 Graph API endpoint (e.g., https://graph.microsoft.com/v1.0/sendMail).Rate Limiting
use Symfony\Component\HttpClient\Retry\RetryStrategy;
$client = \Http::withOptions([
'retry' => RetryStrategy::fromCalls(3, function () {
return true; // Retry on any failure
})
]);
Missing Dependencies
Class 'Doitcloud\EmailService\Http\Controllers\SendEmail' not found.composer dump-autoload).Custom Email Templates
send() method to support Markdown/HTML templates:
$sender->send([
'to' => 'user@example.com',
'subject' => 'Welcome!',
'body' => view('emails.welcome', ['name' => 'John'])->render()
]);
Attachment Support
SendEmail controller to accept an attachments array:
$sender->send([
'to' => 'user@example.com',
'attachments' => [storage_path('file.pdf')]
]);
Event Dispatching
EmailSent) after successful sends:
event(new \App\Events\EmailSent($response));
Testing
SendEmail controller in PHPUnit:
$mockSender = Mockery::mock(SendEmail::class);
$mockSender->shouldReceive('send')->andReturn(['success' => true]);
$this->app->instance(SendEmail::class, $mockSender);
How can I help you explore Laravel packages today?