Settings Configuration
When to Use
Use this right after enabling the Mailgun module, before configuring the API key. Two
settings.phpchanges are required regardless of how you store the key.
Decision
| Setting | Purpose | Required? |
|---|---|---|
$settings['config_exclude_modules'][] |
Prevents mailgun.settings.yml from being exported to config/sync |
Yes |
$config['mailgun.settings']['api_key'] |
Injects the API key from environment variable | Recommended |
$config['mailgun.settings']['api_endpoint'] |
Forces a specific region per environment | Optional |
Pattern
Exclude Mailgun config from export (always)
// web/sites/default/settings.php
$settings['config_exclude_modules'] = ['mailgun'];
// If you already exclude other modules:
$settings['config_exclude_modules'] = ['brevo', 'brevo_mailer', 'mailsystem', 'mailgun'];
Inject API key from environment (recommended)
// settings.php (or settings.local.php for dev)
if ($key = getenv('MAILGUN_API_KEY')) {
$config['mailgun.settings']['api_key'] = $key;
}
if ($endpoint = getenv('MAILGUN_API_ENDPOINT')) {
$config['mailgun.settings']['api_endpoint'] = $endpoint;
}
DDEV example (.ddev/config.yaml)
web_environment:
- MAILGUN_API_KEY=key-abc123def456...
- MAILGUN_API_ENDPOINT=https://api.eu.mailgun.net
After editing: ddev restart.
| Hosting | Where to set |
|---|---|
| DDEV (local) | .ddev/config.yaml web_environment: block |
| Pantheon | Pantheon Secrets API or pantheon.yml |
| Acquia Cloud | Cloud UI → Secrets, or settings.acquia.php |
| Platform.sh | .platform.app.yaml variables: or CLI |
| Lando | .lando.yml services.appserver.overrides.environment |
Common Mistakes
- Wrong: Forgetting
config_exclude_modulesand committing the API key to git → Right: Always addmailgunto the exclusion list before saving the API key in the UI. - Wrong: Using
Settings::get()to read the API key in custom code → Right: Read via\Drupal::config('mailgun.settings')->get('api_key')so the env override layer applies. - Wrong: Setting
MAILGUN_API_KEYonly in.envand expecting Drupal to read it → Right:getenv()reads OS environment;.envfiles require separate loaders or DDEV'sweb_environment.