In Laravel 5.2, integrating Mailgun for sending emails requires proper configuration. Typically, you would set up your Mailgun credentials in the config/services.php file. Here’s how you can do it:

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN', 'mydomain.com'),
    'secret' => env('MAILGUN_SECRET', 'my-secret-key-132152345423'),
],

However, if you need to change these settings at runtime-perhaps because the Mailgun credentials are stored in a database rather than in the .env file-you can achieve this by modifying the configuration before sending an email. This approach allows you to switch between different Mailgun accounts or domains as needed.

Here’s an example of how to dynamically set the Mailgun configuration:

use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Config;

// Fetch Mailgun credentials from the database
$mailgunDomain = 'user-specific-domain.com'; // Replace with actual database call
$mailgunSecret = 'user-specific-secret'; // Replace with actual database call

// Update Mailgun configuration at runtime
Config::set('services.mailgun.domain', $mailgunDomain);
Config::set('services.mailgun.secret', $mailgunSecret);

// Now you can send an email using the updated Mailgun settings
Mail::send('emails.template', $data, function ($message) {
    $message->to('recipient@example.com')->subject('Subject Here');
});

This method allows you to customize the Mailgun settings based on user-specific data, ensuring that the correct credentials are used for each email sent.