如何在Shopware自定义插件中获取「Shop settings > Mailers > Mail host」的值?
Absolutely! You can easily pull the SMTP host value from the Shop settings > Mailers > Mail host field in your custom Shopware plugin. The approach varies slightly depending on whether you're using Shopware 6 (the current stable version) or Shopware 5. Here's a breakdown:
Shopware 6 Implementation
Shopware 6 uses the SystemConfigService to manage all system settings, including mailer configurations. Here's how to use it:
Step 1: Inject the SystemConfigService
First, inject the SystemConfigService into your plugin's service, controller, or subscriber. For example, in a custom service:
use Shopware\Core\System\SystemConfig\SystemConfigService; class MyCustomPluginService { private SystemConfigService $systemConfigService; public function __construct(SystemConfigService $systemConfigService) { $this->systemConfigService = $systemConfigService; } // Your custom logic here }
Make sure to register this service in your plugin's services.xml (or services.yaml) if you're using a custom service class.
Step 2: Fetch the SMTP Host Value
Use the get() method to retrieve the configuration. The key for the default mailer's host is core.mailer.transport.default.host:
// Get the global SMTP host (applies to all sales channels unless overridden) $smtpHost = $this->systemConfigService->get('core.mailer.transport.default.host'); // If you need the value for a specific sales channel, pass its ID as the second parameter $specificSalesChannelId = 'your-sales-channel-uuid-here'; $smtpHostForChannel = $this->systemConfigService->get('core.mailer.transport.default.host', $specificSalesChannelId);
- If you've created a custom mailer (not the default one), replace
defaultin the config key with your mailer's name (e.g.,core.mailer.transport.my_custom_mailer.host).
Shopware 5 Implementation
For older Shopware 5 installations, you can access the config directly via the Shopware container:
// Fetch the global mail config $shopConfig = Shopware()->Container()->get('shopware.config'); $smtpHost = $shopConfig['mail']['host']; // Or for a specific shop (in multi-shop setups) $shopId = 1; // Replace with your shop ID $shopConfig = Shopware()->Models()->getRepository(\Shopware\Models\Config\Config::class)->getShopConfig($shopId); $smtpHost = $shopConfig['mail']['host'];
Notes
- Ensure your plugin has the necessary permissions to access system configuration (this is enabled by default for most plugins).
- If the mailer configuration is overridden at the sales channel level, always pass the sales channel ID to get the correct value for that channel.
内容的提问来源于stack exchange,提问作者Nem Par

